Given a 2D binary matrix of N rows and M columns. The task is to check whether the matrix is horizontal symmetric, vertical symmetric or both. The matrix is said to be horizontal symmetric if the first row is same as the last row, the second row is same as the second last row and so on. And the matrix is said to be vertical symmetric if the first column is same as the last column, the second column is same as the second last column and so on. Print “VERTICAL” if the matrix is vertical symmetric, “HORIZONTAL” if the matrix is vertical symmetric, “BOTH” if the matrix is vertical and horizontal symmetric and “NO” if not symmetric.
Input : N = 3 M = 3 0 1 0 0 0 0 0 1 0 Output : Both First and third row are same and also second row is in middle. So Horizontal Symmetric. Similarly, First and third column are same and also second column is in middle, so Vertical Symmetric. Input : 0 0 1 1 1 0 0 0 1. Output : Horizontal
The idea is to use to pointers indicating two rows (or columns) and compare each cell of both the pointed rows (or columns).
For Horizontal Symmetry, initialize one pointer i = 0 and another pointer j = N – 1.
Now, compare each element of i-th row and j-th row. Increase i by 1 and decrease j by 1 in each loop cycle. If at least one not identical element is found, mark matrix as not horizontal symmetric.
Similarly, for Vertical Symmetry, initialize one pointer i = 0 and another pointer j = M – 1.
Now, compare each element of i-th column and j-th column. Increase i by 1 and decrease j by 1 in each loop cycle. If at least one not identical element is found, mark matrix as not vertical symmetric.
Below is the implementation of above idea :
Time Complexity: O(N*M).
This article is contributed by Anuj Chauhan. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Maximum size square sub-matrix with all 1s
- Print a given matrix in spiral form
- Search in a row wise and column wise sorted matrix
- A Boolean Matrix Question
- Matrix Chain Multiplication | DP-8
- Print unique rows in a given boolean matrix
- Inplace (Fixed space) M x N size matrix transpose | Updated
- Maximum sum rectangle in a 2D matrix | DP-27
- Zigzag (or diagonal) traversal of Matrix
- Divide and Conquer | Set 5 (Strassen's Matrix Multiplication)
- Print all possible paths from top left to bottom right of a mXn matrix
- Count all possible paths from top left to bottom right of a mXn matrix
- Printing brackets in Matrix Chain Multiplication Problem
- Create a matrix with alternating rectangles of O and X
- Given an n x n square matrix, find sum of all sub-squares of size k x k
- Print all elements in sorted order from row and column wise sorted matrix
- Given a matrix of 'O' and 'X', find the largest subsquare surrounded by 'X'
- Given a matrix of ‘O’ and ‘X’, replace 'O' with 'X' if surrounded by 'X'
- Find a common element in all rows of a given row-wise sorted matrix
- Program for Rank of Matrix