
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Check if sums of i-th row and i-th column are same in matrix in Python
Suppose we have a 2D matrix. We have to check whether the sum of i-th row is same as the sum of i-th column or not.
So, if the input is like
2 | 3 | 4 | 5 |
10 | 6 | 4 | 2 |
1 | 4 | 6 | 7 |
1 | 5 | 6 | 7 |
then the output will be True, as the sum of first row and column is (2 + 3 + 4 + 5) = 14 and (2 + 10 + 1 + 1) = 14.
To solve this, we will follow these steps −
- row := row count of mat
- col := column count of mat
- total_row := 0, total_col := 0
- for i in range 0 to row - 1, do
- total_row := 0, total_col := 0
- for j in range 0 to col - 1, do
- total_row := total_row + mat[i, j]
- total_col := total_col + mat[j, i]
- if total_row is same as total_col, then
- return True
- return False
Let us see the following implementation to get better understanding −
Example Code
def solve(mat): row = len(mat) col = len(mat[0]) total_row = 0 total_col = 0 for i in range(row): total_row = 0 total_col = 0 for j in range(col): total_row += mat[i][j] total_col += mat[j][i] if total_row == total_col: return True return False matrix = [ [2,3,4,5], [10,6,4,2], [1,4,6,7], [1,5,6,7] ] print(solve(matrix))
Input
[ [1,2,3,4],[9,5,3,1],
[0,3,5,6],[0,4,5,6]
]
Output
True
- Related Articles
- Count of words whose i-th letter is either (i-1)-th, i-th, or (i+1)-th letter of given word in C++
- Program to find valid matrix given row and column sums in Python
- Define column and row names of a square matrix in a single line code if they are same in R.
- Check if Matrix remains unchanged after row reversals in Python
- Maximum path sum that starting with any cell of 0-th row and ending with any cell of (N-1)-th row in C++
- Find trace of matrix formed by adding Row-major and Column-major order of same matrix in C++
- Check whether K-th bit is set or nots in Python
- Find trace of matrix formed by adding Row-major and Column-major order of same matrix in C++ Program
- Check if lowercase and uppercase characters are in same order in Python
- Find the original matrix when largest element in a row and a column are given in Python
- Sort the matrix row-wise and column-wise using Python
- N-th Fibonacci number in Python Program
- The sum of $3^{rd}$ and $15^{th}$ elements of an arithmetic progression is equal to the sum of $6^{th}$, $11^{th}$ and $13^{th}$ elements of the same progression. Then which elements of the series should necessarily be equal to zero?
- Check if sum of divisors of two numbers are same in Python
- If $(m + 1)$th term of an A.P. is twice the $(n + 1)$th term, prove that $(3m + 1)$th term is twice the $(m + n + 1)$th term.

Advertisements