Given a NxN matrix. The task is to check if after reversing all of the rows of the given Matrix, the matrix remains same or not.
Input : N = 3 1 2 1 2 2 2 3 4 3 Output : Yes If all the rows are reversed then matrix will become: 1 2 1 2 2 2 3 4 3 which is same. Input : N = 3 1 2 2 2 2 2 3 4 3 Output : No
- A most important observation is for the matrix to be same after row reversals, each single row must be palindromic.
- Now to check if a row is palindromic, maintain two pointers, one pointing to start and other to end of row. Start comparing the values present and do start++ and end–. Repeat the process until all elements are checked till the middle of the row. If at each step elements are same, then row is palindromic otherwise not.
- If any of the Row is not palindromic then answer is No.
Below is the implementation of the above approach:
# Python 3 implementation of the above approach
# Function to check Palindromic Condition
def specialMatrix(matrix, N):
for i in range(N):
# Pointer to start of row
start = 0
# Pointer to end of row
end = N – 1
while (start <= end): # Single Mismatch means row is not palindromic if (matrix[i][start] != matrix[i][end]): print("No") return start += 1 end -= 1 print("Yes") return # Driver Code if __name__ == '__main__': matrix = [[1, 2, 1], [2, 2, 2], [3, 4, 3]] N = 3 specialMatrix(matrix, N) # This code is contributed by # Surendra_Gangwar [tabby title="C#"]
- Check if matrix can be converted to another matrix by transposing square sub-matrices
- Program to check diagonal matrix and scalar matrix
- Check if it is possible to make the given matrix increasing matrix or not
- Program to check if a matrix is Binary matrix or not
- Check if a given matrix is sparse or not
- Check for possible path in 2D matrix
- Check whether a given matrix is orthogonal or not
- Check if a given matrix is Hankel or not
- Check if a Matrix is Invertible
- Program to check if matrix is singular or not
- Check if sums of i-th row and i-th column are same in matrix
- Check given matrix is magic square or not
- Program to check idempotent matrix
- Program to check if a matrix is symmetric
- Program to check Involutory Matrix
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.