Minimum elements to be added so that two matrices can be multiplied
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: 4
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 = 4Input: p = 11, q = 5, r = 10, s = 11
Output: 0
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 <bits/stdc++.h> 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 = 2q = 3r = 5s = 6print(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
<?php // Python3 implementation of the above approach // Function to calculate the minimum // number of extra elements is to add function 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 || $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; echo minExtraElements($p, $q, $r, $s); // This code is contributed by Ryuga ?> |
4
Recommended Posts:
- Minimum value to be added to X such that it is at least Y percent of N
- Count of matrices (of different orders) with given number of elements
- Print elements that can be added to form a given sum
- Minimum number of Parentheses to be added to make it valid
- Minimum difference between adjacent elements of array which contain elements from each row of a matrix
- Sum of all minimum frequency elements in Matrix
- Sum of minimum elements of all possible sub-arrays of an array
- Find the first, second and third minimum elements in an array
- Minimum product in a grid of adjacent elements
- Minimum value to be assigned to the elements so that sum becomes greater than initial sum
- Minimum operations required to set all elements of binary matrix
- Program to print N minimum elements from list of integers
- XOR of XORs of all sub-matrices
- Different Operations on Matrices
- Minimum sub-array such that number of 1's in concatenation of binary representation of its elements is at least K
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



