Check if the given matrix is increasing row and column wise

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
#define N 2
#define M 2
  
// Function that returns true if the matrix
// is strictly increasing
bool isMatrixInc(int a[N][M])
{
  
    // Check if the matrix
    // is strictly increasing
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < M; j++) {
  
            // Out of bound condition
            if (i - 1 >= 0) {
                if (a[i][j] <= a[i - 1][j])
                    return false;
            }
  
            // Out of bound condition
            if (j - 1 >= 0) {
                if (a[i][j] <= a[i][j - 1])
                    return false;
            }
        }
    }
  
    return true;
}
  
// Driver code
int main()
{
  
    int a[N][M] = { { 2, 10 },
                    { 11, 20 } };
  
    if (isMatrixInc(a))
        cout << "Yes";
    else
        cout << "No";
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
import java.io.*;
  
class GFG 
{
  
static int N = 2;
static int M = 2;
  
// Function that returns true if the matrix
// is strictly increasing
static boolean isMatrixInc(int a[][])
{
  
    // Check if the matrix
    // is strictly increasing
    for (int i = 0; i < N; i++) 
    {
        for (int j = 0; j < M; j++) 
        {
  
            // Out of bound condition
            if (i - 1 >= 0)
            {
                if (a[i][j] <= a[i - 1][j])
                    return false;
            }
  
            // Out of bound condition
            if (j - 1 >= 0
            {
                if (a[i][j] <= a[i][j - 1])
                    return false;
            }
        }
    }
  
    return true;
}
  
// Driver code
  
public static void main (String[] args) 
{
      
    int a[][] = { { 2, 10 },
                    { 11, 20 } };
      
    if (isMatrixInc(a))
        System.out.print( "Yes");
    else
        System.out.print( "No");
}
}
  
// This code is contributed by anuj_67..

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach 
  
N, M = 2, 2
  
# Function that returns true if the matrix 
# is strictly increasing 
def isMatrixInc(a) : 
  
    # Check if the matrix 
    # is strictly increasing 
    for i in range(N) :
        for j in range(M) :
  
            # Out of bound condition 
            if (i - 1 >= 0) :
                if (a[i][j] <= a[i - 1][j]) : 
                    return False
  
            # Out of bound condition 
            if (j - 1 >= 0) : 
                if (a[i][j] <= a[i][j - 1]) :
                    return False
                      
    return True;
  
  
# Driver code 
if __name__ == "__main__"
  
    a = [ [ 2, 10 ], 
        [11, 20 ] ]; 
  
    if (isMatrixInc(a)) :
        print("Yes"); 
    else :
        print("No"); 
          
# This code is contributed by AnkitRai01

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach
using System; 
      
class GFG 
{
  
static int N = 2;
static int M = 2;
  
// Function that returns true if the matrix
// is strictly increasing
static Boolean isMatrixInc(int [,]a)
{
  
    // Check if the matrix
    // is strictly increasing
    for (int i = 0; i < N; i++) 
    {
        for (int j = 0; j < M; j++) 
        {
  
            // Out of bound condition
            if (i - 1 >= 0)
            {
                if (a[i,j] <= a[i - 1,j])
                    return false;
            }
  
            // Out of bound condition
            if (j - 1 >= 0) 
            {
                if (a[i,j] <= a[i,j - 1])
                    return false;
            }
        }
    }
  
    return true;
}
  
// Driver code
public static void Main (String[] args) 
{
      
    int [,]a = { { 2, 10 },
                    { 11, 20 } };
      
    if (isMatrixInc(a))
        Console.WriteLine( "Yes");
    else
        Console.WriteLine( "No");
}
}
  
// This code is contributed by Princi Singh

chevron_right


Output:

Yes


My Personal Notes arrow_drop_up

Striver(underscore)79 at Codechef and codeforces D

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : AnkitRai01, vt_m, princi singh