- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check if Matrix remains unchanged after row reversals in Python
Suppose we have a square matrix. We have to check whether the matrix remains same after performing row reversal operations on each row, or not.
So, if the input is like
6 | 8 | 6 |
2 | 8 | 2 |
3 | 3 | 3 |
then the output will be True
To solve this, we will follow these steps −
- n := row count of matrix
- for i in range 0 to n - 1, do
- left := 0, right := n - 1
- while left <= right, do
- if matrix[i, left] is not same as matrix[i, right], then
- return False
- left := left + 1, right := right - 1
- if matrix[i, left] is not same as matrix[i, right], then
- return True
Example
Let us see the following implementation to get better understanding −
def solve(matrix): n = len(matrix) for i in range(n): left = 0 right = n - 1 while left <= right: if matrix[i][left] != matrix[i][right]: return False left += 1 right -= 1 return True matrix = [ [6,8,6], [2,8,2], [3,3,3]] print(solve(matrix))
Input
[ [6,8,6], [2,8,2], [3,3,3]]
Output
True
- Related Articles
- Check if sums of i-th row and i-th column are same in matrix in Python
- Program to find out the position of a ball after n reversals in Python
- Python – Sort Matrix by Row Median
- Check if matrix can be converted to another matrix by transposing square sub-matrices in Python
- Python - Sort Matrix by Maximum Row element
- Python - Count the frequency of matrix row length
- Check if a Matrix is Invertible in C++
- Check if all enemies are killed with bombs placed in a matrix in Python
- Python – Check Similar elements in Matrix rows
- Check if it is possible to sort the array after rotating it in Python
- Program to check if a matrix is Binary matrix or not in C++
- How to multiply corresponding row values in a matrix with single row matrix in R?
- Check If Word Is Valid After Substitutions in C++
- How To Check If A Row Is Hidden In Excel?
- Check if all rows of a matrix are circular rotations of each other in Python

Advertisements