Check if the given matrix is increasing row and column wise
Last Updated :
21 Jun, 2022
Given a matrix mat[][], the task is to check if the given matrix is strictly increasing or not. A matrix is said to be strictly increasing if all of its rows as well as all of its columns are strictly increasing.
Examples:
Input: mat[][] = {{2, 10}, {11, 20}}
Output: Yes
All the rows and columns are strictly increasing.
Input: mat[][] = {{2, 1}, {11, 20}}
Output: No
First row doesn’t satisfy the required condition.
Approach: Linearly traverse for every element and check if there are increasing row-wise and column-wise or not. The two conditions are (a[i][j] > a[i – 1][j]) and (a[i][j] > a[i][j – 1]) . If any of the two conditions fail, then the matrix is not strictly increasing.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define N 2
#define M 2
bool isMatrixInc( int a[N][M])
{
for ( int i = 0; i < N; i++) {
for ( int j = 0; j < M; j++) {
if (i - 1 >= 0) {
if (a[i][j] <= a[i - 1][j])
return false ;
}
if (j - 1 >= 0) {
if (a[i][j] <= a[i][j - 1])
return false ;
}
}
}
return true ;
}
int main()
{
int a[N][M] = { { 2, 10 },
{ 11, 20 } };
if (isMatrixInc(a))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int N = 2 ;
static int M = 2 ;
static boolean isMatrixInc( int a[][])
{
for ( int i = 0 ; i < N; i++)
{
for ( int j = 0 ; j < M; j++)
{
if (i - 1 >= 0 )
{
if (a[i][j] <= a[i - 1 ][j])
return false ;
}
if (j - 1 >= 0 )
{
if (a[i][j] <= a[i][j - 1 ])
return false ;
}
}
}
return true ;
}
public static void main (String[] args)
{
int a[][] = { { 2 , 10 },
{ 11 , 20 } };
if (isMatrixInc(a))
System.out.print( "Yes" );
else
System.out.print( "No" );
}
}
|
Python3
N, M = 2 , 2
def isMatrixInc(a) :
for i in range (N) :
for j in range (M) :
if (i - 1 > = 0 ) :
if (a[i][j] < = a[i - 1 ][j]) :
return False ;
if (j - 1 > = 0 ) :
if (a[i][j] < = a[i][j - 1 ]) :
return False ;
return True ;
if __name__ = = "__main__" :
a = [ [ 2 , 10 ],
[ 11 , 20 ] ];
if (isMatrixInc(a)) :
print ( "Yes" );
else :
print ( "No" );
|
C#
using System;
class GFG
{
static int N = 2;
static int M = 2;
static Boolean isMatrixInc( int [,]a)
{
for ( int i = 0; i < N; i++)
{
for ( int j = 0; j < M; j++)
{
if (i - 1 >= 0)
{
if (a[i,j] <= a[i - 1,j])
return false ;
}
if (j - 1 >= 0)
{
if (a[i,j] <= a[i,j - 1])
return false ;
}
}
}
return true ;
}
public static void Main (String[] args)
{
int [,]a = { { 2, 10 },
{ 11, 20 } };
if (isMatrixInc(a))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
Javascript
<script>
let N = 2;
let M = 2;
function isMatrixInc(a)
{
for (let i = 0; i < N; i++)
{
for (let j = 0; j < M; j++)
{
if (i - 1 >= 0)
{
if (a[i][j] <= a[i - 1][j])
return false ;
}
if (j - 1 >= 0)
{
if (a[i][j] <= a[i][j - 1])
return false ;
}
}
}
return true ;
}
let a = [[2, 10 ],
[ 11, 20 ]];
if (isMatrixInc(a))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(N*M)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...