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 = 4

Input: 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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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
?>

chevron_right


Output:

4


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.



Improved By : ihritik, AnkitRai01



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.