VOOZH about

URL: https://www.geeksforgeeks.org/dsa/count-sorted-rows-matrix/

⇱ Count all sorted rows in a matrix - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Count all sorted rows in a matrix

Last Updated : 31 Jul, 2024

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?

Examples :

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. 


Output
3

Time Complexity : O(m*n) 
Auxiliary space :O(1)

If you have another optimized approach to solve this problem then please share in comments.

Comment
Article Tags:
Article Tags: