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: 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++
#include <bits/stdc++.h>
using namespace std;
int minExtraElements( int p, int q, int r, int s)
{
int num1, num2;
if (q == r || p == s)
return 0;
else {
if (q < r)
num1 = (r - q) * p;
else
num1 = (q - r) * s;
if (p < s)
num2 = (s - p) * r;
else
num2 = (p - s) * q;
}
return min(num1, num2);
}
int main()
{
int p = 2, q = 3, r = 5, s = 6;
cout << minExtraElements(p, q, r, s) << endl;
return 0;
}
|
Java
class GFG
{
static int minExtraElements( int p, int q, int r, int s)
{
int num1, num2;
if (q == r || p == s)
return 0 ;
else {
if (q < r)
num1 = (r - q) * p;
else
num1 = (q - r) * s;
if (p < s)
num2 = (s - p) * r;
else
num2 = (p - s) * q;
}
return Math.min(num1, num2);
}
public static void main(String []rags)
{
int p = 2 , q = 3 , r = 5 , s = 6 ;
System.out.println(minExtraElements(p, q, r, s));
}
}
|
Python
def minExtraElements(p, q, r, s):
if (q = = r or p = = s):
return 0
else :
if (q < r):
num1 = (r - q) * p
else :
num1 = (q - r) * s
if (p < s):
num2 = (s - p) * r
else :
num2 = (p - s) * q
return min (num1, num2)
p = 2
q = 3
r = 5
s = 6
print (minExtraElements(p, q, r, s))
|
C#
using System;
class GFG
{
static int minExtraElements( int p, int q, int r, int s)
{
int num1, num2;
if (q == r || p == s)
return 0;
else {
if (q < r)
num1 = (r - q) * p;
else
num1 = (q - r) * s;
if (p < s)
num2 = (s - p) * r;
else
num2 = (p - s) * q;
}
return Math.Min(num1, num2);
}
public static void Main()
{
int p = 2, q = 3, r = 5, s = 6;
Console.WriteLine(minExtraElements(p, q, r, s));
}
}
|
PHP
<?php
function minExtraElements( $p , $q , $r , $s )
{
if ( $q == $r || $p == $s )
return 0;
else
{
if ( $q < $r )
$num1 = ( $r - $q ) * $p ;
else
$num1 = ( $q - $r ) * $s ;
if ( $p < $s )
$num2 = ( $s - $p ) * $r ;
else
$num2 = ( $p - $s ) * $q ;
}
return min( $num1 , $num2 );
}
$p = 2; $q = 3; $r = 5; $s = 6;
echo minExtraElements( $p , $q , $r , $s );
?>
|
Javascript
<script>
function minExtraElements( p, q, r, s)
{
let num1, num2;
if (q == r || p == s)
return 0;
else {
if (q < r)
num1 = (r - q) * p;
else
num1 = (q - r) * s;
if (p < s)
num2 = (s - p) * r;
else
num2 = (p - s) * q;
}
return Math.min(num1, num2);
}
let p = 2, q = 3, r = 5, s = 6;
document.write(minExtraElements(p, q, r, s) + "</br>" );
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...