![]() |
VOOZH | about |
Given a matrix mat[][] of dimensions n*m, we have to check if the sum of i-th row is equal to the sum of i-th column or not.
Note: Check only up to valid row and column numbers i.e. if the dimensions are 3x5, check only for the first 3 rows and columns, i.e. min(n, m).
Examples:
Input: mat = [[1,2],[2,1]]
Output: 1
Explanation: The sum of 1st row is equal to sum of1st column and also sum of 2nd row is equal to the sum of 2nd column. So, Answer is 1.Input: mat = [[5],[0],[0]]
Output: 1
Explanation: The sum of 1st column is equal to the sum of 1st row.Thus,answer is 1. (We do not check for the 2nd and 3rd rows because there are no 2nd and 3rd columns.)
The idea is to check whether the sum of each row matches the sum of the corresponding column within the valid range of the matrix.
- We iterate through the first min(n, m) rows and columns, computing their sums separately.
- If any row sum differs from the corresponding column sum, we return false; otherwise, we return true.
1
Time Complexity:O(n * m) since we iterate through all valid rows and columns to compute their sums.
Auxiliary Space: O(1), since no extra space has been taken.