 Open in App
Not now

# Minimum elements to be added so that two matrices can be multiplied

• Last Updated : 06 Dec, 2022

Given two matrices A and B of order p x q (or q X p) and r x s ( or s X r). The task is to find the minimum number of elements to be added to any of the two matrices to make them multiplicative.
Two matrices are multiplicative if the number of columns in one matrix is equal to the number of rows in the other matrix.

Examples:

Input: p = 2, q = 3, r = 5, s = 6
Output:
Two columns can be added to make q = 5.
To add two columns, the number of elements will be added 2 * no. of rows i.e. 2 * 2 = 4

Input: p = 11, q = 5, r = 10, s = 11
Output:

Approach:

• If the number of columns in one matrix is equal to the number of rows in the other matrix, then no extra elements are required to be added.
• If they are not equal, we have to add either some extra columns in one matrix or some extra rows in the other matrix.
• So, calculate the number of extra elements in both condition and print the minimum one.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the above approach``#include ``using` `namespace` `std;` `// Function to calculate the minimum``// number of extra elements is to add``int` `minExtraElements(``int` `p, ``int` `q, ``int` `r, ``int` `s)``{``    ``// num1 will store minimum number of``    ``// extra elements required``    ``// to make A x B possible` `    ``// num2 will store minimum number of``    ``// extra elements required``    ``// to make B x A possible``    ``int` `num1, num2;` `    ``// if either A x B or B x A is possible,``    ``// it will return 0``    ``if` `(q == r || p == s)``        ``return` `0;` `    ``else` `{``        ``// it will calculate minimum number of``        ``// extra elements required``        ``// to make A x B possible``        ``if` `(q < r)``            ``num1 = (r - q) * p;``        ``else``            ``num1 = (q - r) * s;` `        ``// it will calculate minimum number of``        ``// extra elements required``        ``// to make B x A possible``        ``if` `(p < s)``            ``num2 = (s - p) * r;``        ``else``            ``num2 = (p - s) * q;``    ``}` `    ``// return minimum of both``    ``return` `min(num1, num2);``}` `// Driver code``int` `main()``{` `    ``int` `p = 2, q = 3, r = 5, s = 6;``    ``cout << minExtraElements(p, q, r, s) << endl;` `    ``return` `0;``}`

## Java

 `// Java implementation of the above approach` `class` `GFG``{``    ``// Function to calculate the minimum``    ``// number of extra elements is to add``    ``static` `int` `minExtraElements(``int` `p, ``int` `q, ``int` `r, ``int` `s)``    ``{``        ``// num1 will store minimum number of``        ``// extra elements required``        ``// to make A x B possible``    ` `        ``// num2 will store minimum number of``        ``// extra elements required``        ``// to make B x A possible``        ``int` `num1, num2;``    ` `        ``// if either A x B or B x A is possible,``        ``// it will return 0``        ``if` `(q == r || p == s)``            ``return` `0``;``    ` `        ``else` `{``            ``// it will calculate minimum number of``            ``// extra elements required``            ``// to make A x B possible``            ``if` `(q < r)``                ``num1 = (r - q) * p;``            ``else``                ``num1 = (q - r) * s;``    ` `            ``// it will calculate minimum number of``            ``// extra elements required``            ``// to make B x A possible``            ``if` `(p < s)``                ``num2 = (s - p) * r;``            ``else``                ``num2 = (p - s) * q;``        ``}``    ` `        ``// return minimum of both``        ``return` `Math.min(num1, num2);``    ``}``    ` `    ``// Driver code``    ``public` `static` `void` `main(String []rags)``    ``{``    ` `        ``int` `p = ``2``, q = ``3``, r = ``5``, s = ``6``;``        ``System.out.println(minExtraElements(p, q, r, s));``    ` `        ` `    ``}``}` `// This code is contributed by ihritik`

## Python

 `# Python implementation of the above approach`  `# Function to calculate the minimum``# number of extra elements is to add``def` `minExtraElements(p,  q, r, s):` `    ``# num1 will store minimum number of``    ``# extra elements required``    ``# to make A x B possible` `    ``# num2 will store minimum number of``    ``# extra elements required``    ``# to make B x A possible`  `    ``# if either A x B or B x A is possible,``    ``# it will return 0``    ``if` `(q ``=``=` `r ``or` `p ``=``=` `s):``        ``return` `0` `    ``else` `:``        ``# it will calculate minimum number of``        ``# extra elements required``        ``# to make A x B possible``        ``if` `(q < r):``            ``num1 ``=` `(r ``-` `q) ``*` `p``        ``else``:``            ``num1 ``=` `(q ``-` `r) ``*` `s` `        ``# it will calculate minimum number of``        ``# extra elements required``        ``# to make B x A possible``        ``if` `(p < s):``            ``num2 ``=` `(s ``-` `p) ``*` `r``        ``else``:``            ``num2 ``=` `(p ``-` `s) ``*` `q``    `  `    ``# return minimum of both``    ``return` `min``(num1, num2)`  `# Driver code` `    ` `p ``=` `2``q ``=` `3``r ``=` `5``s ``=` `6``print``(minExtraElements(p, q, r, s))` `# This code is contributed by ihritik`

## C#

 `// C# implementation of the above approach` `using` `System;``class` `GFG``{``    ``// Function to calculate the minimum``    ``// number of extra elements is to add``    ``static` `int` `minExtraElements(``int` `p, ``int` `q, ``int` `r, ``int` `s)``    ``{``        ``// num1 will store minimum number of``        ``// extra elements required``        ``// to make A x B possible``    ` `        ``// num2 will store minimum number of``        ``// extra elements required``        ``// to make B x A possible``        ``int` `num1, num2;``    ` `        ``// if either A x B or B x A is possible,``        ``// it will return 0``        ``if` `(q == r || p == s)``            ``return` `0;``    ` `        ``else` `{``            ``// it will calculate minimum number of``            ``// extra elements required``            ``// to make A x B possible``            ``if` `(q < r)``                ``num1 = (r - q) * p;``            ``else``                ``num1 = (q - r) * s;``    ` `            ``// it will calculate minimum number of``            ``// extra elements required``            ``// to make B x A possible``            ``if` `(p < s)``                ``num2 = (s - p) * r;``            ``else``                ``num2 = (p - s) * q;``        ``}``    ` `        ``// return minimum of both``        ``return` `Math.Min(num1, num2);``    ``}``    ` `    ``// Driver code``    ``public` `static` `void` `Main()``    ``{``    ` `        ``int` `p = 2, q = 3, r = 5, s = 6;``        ``Console.WriteLine(minExtraElements(p, q, r, s));``    ` `        ` `    ``}``}` `// This code is contributed by ihritik`

## PHP

 ``

## Javascript

 ``

Output:

`4`

Time Complexity: O(1)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up