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++
// 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 increasingbool 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 codeint main(){Â
    int a[N][M] = { { 2, 10 },                    { 11, 20 } };Â
    if (isMatrixInc(a))        cout << "Yes";    else        cout << "No";Â
    return 0;} |
Java
// Java implementation of the approachimport java.io.*;Â
class GFG {Â
static int N = 2;static int M = 2;Â
// Function that returns true if the matrix// is strictly increasingstatic 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.. |
Python3
# 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 |
C#
// C# implementation of the approachusing System; Â Â Â Â Â class GFG {Â
static int N = 2;static int M = 2;Â
// Function that returns true if the matrix// is strictly increasingstatic 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 codepublic 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 |
Javascript
<script>// Java Script implementation of the approachlet N = 2;let M = 2;Â
// Function that returns true if the matrix// is strictly increasingfunction isMatrixInc(a){Â
    // Check if the matrix    // is strictly increasing    for (let i = 0; i < N; i++)    {        for (let 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Â
Â
         let a = [[2, 10 ],                    [ 11, 20 ]];         if (isMatrixInc(a))        document.write( "Yes");    else        document.write( "No");Â
// This code is contributed by sravan kumar</script> |
Yes
Â
Time Complexity: O(N*M)
Auxiliary Space: O(1)
Â
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



