Count all sorted rows in a matrix
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.
- Traverse from left side of the matrix to count all the row which are in strictly increasing order
- 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.
C++
#include <bits/stdc++.h>
#define MAX 100
using namespace std;
int sortedCount( int mat[][MAX], int r, int c)
{
int result = 0;
for ( int i=0; i<r; i++)
{
int j;
for (j=0; j<c-1; j++)
if (mat[i][j+1] <= mat[i][j])
break ;
if (j == c-1)
result++;
}
for ( int i=0; i<r; i++)
{
int j;
for (j=c-1; j>0; j--)
if (mat[i][j-1] <= mat[i][j])
break ;
if (c > 1 && j == 0)
result++;
}
return result;
}
int main()
{
int m = 4, n = 5;
int mat[][MAX] = {{1, 2, 3, 4, 5},
{4, 3, 1, 2, 6},
{8, 7, 6, 5, 4},
{5, 7, 8, 9, 10}};
cout << sortedCount(mat, m, n);
return 0;
}
|
Java
class GFG {
static int MAX = 100 ;
static int sortedCount( int mat[][], int r, int c)
{
int result = 0 ;
for ( int i = 0 ; i < r; i++) {
int j;
for (j = 0 ; j < c - 1 ; j++)
if (mat[i][j + 1 ] <= mat[i][j])
break ;
if (j == c - 1 )
result++;
}
for ( int i = 0 ; i < r; i++) {
int j;
for (j = c - 1 ; j > 0 ; j--)
if (mat[i][j - 1 ] <= mat[i][j])
break ;
if (c > 1 && j == 0 )
result++;
}
return result;
}
public static void main(String arg[])
{
int m = 4 , n = 5 ;
int mat[][] = { { 1 , 2 , 3 , 4 , 5 },
{ 4 , 3 , 1 , 2 , 6 },
{ 8 , 7 , 6 , 5 , 4 },
{ 5 , 7 , 8 , 9 , 10 } };
System.out.print(sortedCount(mat, m, n));
}
}
|
Python
def sortedCount(mat, r, c):
result = 0
for i in range (r):
j = 0
for j in range (c - 1 ):
if mat[i][j + 1 ] < = mat[i][j]:
break
if j = = c - 2 :
result + = 1
for i in range ( 0 , r):
j = 0
for j in range (c - 1 , 0 , - 1 ):
if mat[i][j - 1 ] < = mat[i][j]:
break
if c > 1 and j = = 1 :
result + = 1
return result
m, n = 4 , 5
mat = [[ 1 , 2 , 3 , 4 , 5 ],
[ 4 , 3 , 1 , 2 , 6 ],
[ 8 , 7 , 6 , 5 , 4 ],
[ 5 , 7 , 8 , 9 , 10 ]]
print (sortedCount(mat, m, n))
|
C#
using System;
class GFG {
static int sortedCount( int [,]mat, int r,
int c)
{
int result = 0;
for ( int i = 0; i < r; i++) {
int j;
for (j = 0; j < c - 1; j++)
if (mat[i,j + 1] <= mat[i,j])
break ;
if (j == c - 1)
result++;
}
for ( int i = 0; i < r; i++) {
int j;
for (j = c - 1; j > 0; j--)
if (mat[i,j - 1] <= mat[i,j])
break ;
if (c > 1 && j == 0)
result++;
}
return result;
}
public static void Main()
{
int m = 4, n = 5;
int [,]mat = { { 1, 2, 3, 4, 5 },
{ 4, 3, 1, 2, 6 },
{ 8, 7, 6, 5, 4 },
{ 5, 7, 8, 9, 10 } };
Console.WriteLine(
sortedCount(mat, m, n));
}
}
|
PHP
<?php
$MAX = 100;
function sortedCount( $mat ,
$r , $c )
{
$result = 0;
for ( $i = 0; $i < $r ; $i ++)
{
$j ;
for ( $j = 0; $j < $c - 1; $j ++)
if ( $mat [ $i ][ $j + 1] <= $mat [ $i ][ $j ])
break ;
if ( $j == $c - 1)
$result ++;
}
for ( $i = 0; $i < $r ; $i ++)
{
$j ;
for ( $j = $c - 1; $j > 0; $j --)
if ( $mat [ $i ][ $j - 1] <= $mat [ $i ][ $j ])
break ;
if ( $c > 1 && $j == 0)
$result ++;
}
return $result ;
}
$m = 4; $n = 5;
$mat = array ( array (1, 2, 3, 4, 5),
array (4, 3, 1, 2, 6),
array (8, 7, 6, 5, 4),
array (5, 7, 8, 9, 10));
echo sortedCount( $mat , $m , $n );
?>
|
Javascript
<script>
let MAX = 100;
function sortedCount(mat,r,c)
{
let result = 0;
for (let i = 0; i < r; i++) {
let j;
for (j = 0; j < c - 1; j++)
if (mat[i][j + 1] <= mat[i][j])
break ;
if (j == c - 1)
result++;
}
for (let i = 0; i < r; i++) {
let j;
for (j = c - 1; j > 0; j--)
if (mat[i][j - 1] <= mat[i][j])
break ;
if (c > 1 && j == 0)
result++;
}
return result;
}
let m = 4, n = 5;
let mat = [[1, 2, 3, 4, 5],
[4, 3, 1, 2, 6],
[8, 7, 6, 5, 4],
[5, 7, 8, 9, 10]]
document.write(sortedCount(mat, m, n))
</script>
|
Time Complexity : O(m*n)
Auxiliary space : O(1)
If you have another optimized approach to solve this problem then please share in comments.
Last Updated :
09 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...