Given a matrix of m*n size, the task is to count all the rows in a matrix that are sorted either in strictly increasing order or in strictly decreasing order?
Input : m = 4, n = 5 mat[m][n] = 1 2 3 4 5 4 3 1 2 6 8 7 6 5 4 5 7 8 9 10 Output: 3
The idea is simple and involves two traversals of matrix.
1) Traverse from left side of the matrix to count all the row which are in strictly increasing order
2) Traverse from right side of the matrix to count all the row which are in strictly decreasing order
Below is the implementation of above idea.
Time Complexity : O(m*n)
Auxiliary space : O(1)
If you have another optimized approach to solve this problem then please share in comments.
This article is contributed by Shashank Mishra ( Gullu ). 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 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.
- Find a common element in all rows of a given row-wise sorted matrix
- Count rows in a matrix that consist of same element
- Count all the columns in a matrix which are sorted in descending
- Count elements smaller than or equal to x in a sorted matrix
- Print index of columns sorted by count of zeroes in the Given Matrix
- Count Negative Numbers in a Column-Wise and Row-Wise Sorted Matrix
- Count zeros in a row wise and column wise sorted matrix
- Print all elements in sorted order from row and column wise sorted matrix
- heapq in Python to print all elements in sorted order from row and column wise sorted matrix
- Find all permuted rows of a given row in a matrix
- Interchange elements of first and last rows in matrix
- Remove first X rows and columns from a matrix
- Common elements in all rows of a given matrix
- Check if all rows of a matrix are circular rotations of each other
- Print unique rows in a given boolean matrix
- Remove any corner X rows and columns from a matrix
- Maximum difference of sum of elements in two rows in a matrix
- Find duplicate rows in a binary matrix
- Number of rows and columns in a Matrix that contain repeated values
- Enlarge a Matrix such that each element occurs in R rows and C columns