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 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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Find a common element in all rows of a given row-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
- Ways of filling matrix such that product of all rows and all columns are equal to unity
- Maximize sum of diagonal of a matrix by rotating all rows or all columns
- Common elements in all rows of a given matrix
- Find all permuted rows of a given row in a matrix
- Check if all rows of a matrix are circular rotations of each other
- Find distinct elements common to all rows of a matrix
- Count rows in a matrix that consist of same element
- Count rows with sum exceeding sum of the remaining Matrix
- Maximize count of rows consisting of equal elements by flipping columns of a Matrix
- Count all the columns in a matrix which are sorted in descending
- Convert given Matrix into sorted Spiral Matrix
- Maximum difference of sum of elements in two rows in a matrix
- Find pair of rows in a binary matrix that has maximum bit difference
- Find duplicate rows in a binary matrix
- Python | Print unique rows in a given boolean matrix using Set with tuples
- Python Counter| Find duplicate rows in a binary matrix
- Interchange elements of first and last rows in matrix