Given a matrix mat[][], we have to check if the sum of i-th row is equal to the sum of i-th column or not.
Examples:
Input : 1 2 3 4
9 5 3 1
0 3 5 6
0 4 5 6
Output : Yes
Sums of 1st row = 10 and 1st column
are same, i.e., 10
Expected time complexity is O(m x n) where m is a number of rows and n is a number of columns.
The idea is really simple. We use a nested loop to calculate the sum of each row and column and then check whether their sum is equal or not.
The implementation of the above idea is given below.
C++
#include <bits/stdc++.h>
using namespace std;
const int MAX = 100;
bool areSumSame( int a[][MAX], int n, int m)
{
int sum1 = 0, sum2 = 0;
for ( int i = 0; i < min(n, m); i++) {
sum1 = 0, sum2 = 0;
for ( int j = 0; j < min(n, m); j++) {
sum1 += a[i][j];
sum2 += a[j][i];
}
if (sum1 == sum2)
return true ;
}
return false ;
}
int main()
{
int n = 4;
int m = 4;
int M[n][MAX] = { { 1, 2, 3, 4 },
{ 9, 5, 3, 1 },
{ 0, 3, 5, 6 },
{ 0, 4, 5, 6 } };
cout << areSumSame(M, n, m) << "\n" ;
return 0;
}
|
Java
public class GFG {
static boolean areSumSame( int a[][],
int n, int m)
{
int sum1 = 0 , sum2 = 0 ;
for ( int i = 0 ; i < n; i++)
{
sum1 = 0 ;
sum2 = 0 ;
for ( int j = 0 ; j < m; j++)
{
sum1 += a[i][j];
sum2 += a[j][i];
}
if (sum1 == sum2)
return true ;
}
return false ;
}
public static void main(String args[])
{
int n = 4 ;
int m = 4 ;
int M[][] = { { 1 , 2 , 3 , 4 },
{ 9 , 5 , 3 , 1 },
{ 0 , 3 , 5 , 6 },
{ 0 , 4 , 5 , 6 } };
if (areSumSame(M, n, m) == true )
System.out.print( "1\n" );
else
System.out.print( "0\n" );
}
}
|
Python3
MAX = 100 ;
def areSumSame(a, n, m):
sum1 = 0
sum2 = 0
for i in range ( 0 , n):
sum1 = 0
sum2 = 0
for j in range ( 0 , m):
sum1 + = a[i][j]
sum2 + = a[j][i]
if (sum1 = = sum2):
return 1
return 0
n = 4 ;
m = 4 ;
M = [ [ 1 , 2 , 3 , 4 ],
[ 9 , 5 , 3 , 1 ],
[ 0 , 3 , 5 , 6 ],
[ 0 , 4 , 5 , 6 ] ]
print (areSumSame(M, n, m))
|
C#
using System;
class GFG {
static bool areSumSame( int [,]a, int n, int m)
{
int sum1 = 0, sum2 = 0;
for ( int i = 0; i < n; i++)
{
sum1 = 0;
sum2 = 0;
for ( int j = 0; j < m; j++)
{
sum1 += a[i,j];
sum2 += a[j,i];
}
if (sum1 == sum2)
return true ;
}
return false ;
}
public static void Main ()
{
int n = 4;
int m = 4;
int [,] M = { { 1, 2, 3, 4 },
{ 9, 5, 3, 1 },
{ 0, 3, 5, 6 },
{ 0, 4, 5, 6 } };
if (areSumSame(M, n, m) == true )
Console.Write( "1\n" );
else
Console.Write( "0\n" );
}
}
|
PHP
<?php
function areSumSame( $a , $n , $m )
{
$sum1 = 0;
$sum2 = 0;
for ( $i = 0; $i < $n ; $i ++)
{
$sum1 = 0;
$sum2 = 0;
for ( $j = 0; $j < $m ; $j ++)
{
$sum1 += $a [ $i ][ $j ];
$sum2 += $a [ $j ][ $i ];
}
if ( $sum1 == $sum2 )
return true ;
}
return false ;
}
$n = 4 ;
$m = 4 ;
$M = array ( array (1, 2, 3, 4),
array (9, 5, 3, 1),
array (0, 3, 5, 6),
array (0, 4, 5, 6));
echo areSumSame( $M , $n , $m ) ;
?>
|
Javascript
<script>
function areSumSame(a,n,m)
{
let sum1 = 0, sum2 = 0;
for (let i = 0; i < n; i++)
{
sum1 = 0;
sum2 = 0;
for (let j = 0; j < m; j++)
{
sum1 += a[i][j];
sum2 += a[j][i];
}
if (sum1 == sum2)
return true ;
}
return false ;
}
let n = 4;
let m = 4;
let M = [[1, 2, 3, 4 ],
[ 9, 5, 3, 1],
[ 0, 3, 5, 6 ],
[ 0, 4, 5, 6 ]];
if (areSumSame(M, n, m) == true )
document.write( "1\n" );
else
document.write( "0\n" );
</script>
|
Time Complexity: O(min(n, m) * min(n,m))
Auxiliary Space: O(1), since no extra space has been taken.