Check if sums of i-th row and i-th column are same in matrix

15 Jun, 2022
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++
Java
Python3
C#
PHP
Javascript
#include <bits/stdc++.h>using namespace std;const int MAX = 100; // Function to check the if sum of a row// is same as corresponding columnbool areSumSame(int a[][MAX], 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;} // Driver Codeint main(){    int n = 4; // number of rows    int m = 4; // number of columns    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 program to check if there are two// adjacent set bits.public class GFG {         // Function to check the if sum of a row    // is same as corresponding column    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;    }         // Driver code    public static void main(String args[])    {         int n = 4; // number of rows        int m = 4; // number of columns                 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");    }} // This code is contributed by Sam007.
# Python3 program to check the if# sum of a row is same as# corresponding columnMAX = 100; # Function to check the if sum# of a row is same as# corresponding columndef 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 # Driver Coden = 4; # number of rowsm = 4; # number of columnsM = [ [ 1, 2, 3, 4 ],      [ 9, 5, 3, 1 ],      [ 0, 3, 5, 6 ],      [ 0, 4, 5, 6 ] ]       print(areSumSame(M, n, m)) # This code is contributed by Sam007.
// C# program to check if there are two// adjacent set bits.using System; class GFG {         // Function to check the if sum of a row    // is same as corresponding column    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;    }         // Driver code       public static void Main ()    {        int n = 4; // number of rows        int m = 4; // number of columns                 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");    }} // This code is contributed by Sam007.
<?php// Function to check the if// sum of a row is same as// corresponding columnfunction 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 ;} // Driver code$n = 4 ; // number of rows$m = 4 ; // number of columns$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) ; // This code is contributed// by ANKITRAI1?>
<script>// Java script program to check if there are two// adjacent set bits.          // Function to check the if sum of a row    // is same as corresponding column    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;    }         // Driver code              let n = 4; // number of rows        let m = 4; // number of columns                 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");      // This code is contributed by Bobby</script>
Output: 
1
Time Complexity: O(n x m) Auxiliary Space: O(1)
This article is contributed by Aarti_Rathi and Sarthak Kohli. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
Sam007
ankthon
dilipkn13082001
gottumukkalabobby
sachinvinod1904
Matrix
Matrix
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Matrix Chain Multiplication | DP-8
Program to find largest element in an array
Rat in a Maze | Backtracking-2
Sudoku | Backtracking-7
The Celebrity Problem
Find the number of islands | Set 1 (Using DFS)
Rotate a matrix by 90 degree in clockwise direction without using any extra space
Count all possible paths from top left to bottom right of a mXn matrix
Unique paths in a Grid with Obstacles
Printing all solutions in N-Queen Problem