Check if a Matrix is Reverse Bitonic or Not

Given a matrix m[][], the task is to check if the given matrix is Reverse Bitonic or not. If the given matrix is Reverse Bitonic, then print Yes. Otherwise, print No.

If all the rows and the columns of the given matrix have elements in one of the following orders:

  • Strictly increasing
  • Strictly decreasing
  • Strictly decreasing followed by strictly increasing

Then the given matrix is said to be a Reverse Bitonic Matrix

Examples:

Input: m[][] = {{2, 3, 4}, {1, 2, 3}, {4, 5, 6} }
Output: Yes
Explanation:
All the rows of the given matrix forms an increasing sequence.
All the columns of the given matrix {2, 1, 4}, {3, 2, 5}, {4, 3, 6} forms a decreasing followed by increasing sequence.
Therefore, the matrix is Reverse Bitonic.



Input: m[][] = {{1, 2, 3}, {4, 5, 6}, {2, 5, 4}}
Output: No
Explanation:
Since the column {1, 4, 2} does not satisfy any of the three conditions, the given matrix is not Reverse Bitonic.

Approach:
Follow the steps below to solve the problem:

  • Check the elements of each row of the matrix one by one, if it forms a Reverse Bitonic sequence or not. If any row is found to be not Reverse Bitonic, print No.
  • Similarly, check the elements of each column one by one, if it forms a Reverse Bitonic sequence or not. If any row is found to be not Reverse Bitonic, print No.
  • If all the rows and columns are found to be Reverse Bitonic, then print Yes.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ Program to check if a
// matrix is Reverse Bitonic or not
#include <bits/stdc++.h>
using namespace std;
  
const int N = 3, M = 3;
  
// Function to check if an
// array is Reverse Bitonic or not
bool checkReverseBitonic(int arr[], int n)
{
    int i, j, f = 0;
  
    // Check for decreasing sequence
    for (i = 1; i < n; i++) {
        if (arr[i] < arr[i - 1])
            continue;
  
        if (arr[i] == arr[i - 1])
            return false;
  
        else {
            f = 1;
            break;
        }
    }
  
    if (i == n)
        return true;
  
    // Check for increasing sequence
    for (j = i + 1; j < n; j++) {
        if (arr[j] > arr[j - 1])
            continue;
  
        if (arr[i] == arr[i - 1])
            return false;
  
        else {
            if (f == 1)
                return false;
        }
    }
  
    return true;
}
  
// Function to check whether given
// matrix is bitonic or not
void check(int arr[N][M])
{
    int f = 0;
  
    // Check row-wise
    for (int i = 0; i < N; i++) {
        if (!checkReverseBitonic(arr[i], M)) {
            cout << "No" << endl;
            return;
        }
    }
  
    // Check column wise
    for (int i = 0; i < N; i++) {
        // Generate an array
        // consisting of elements
        // of the current column
        int temp[N];
  
        for (int j = 0; j < N; j++) {
            temp[j] = arr[j][i];
        }
  
        if (!checkReverseBitonic(temp, N)) {
            cout << "No" << endl;
            return;
        }
    }
  
    cout << "Yes";
}
  
// Driver Code
int main()
{
    int m[N][M] = { { 2, 3, 4 },
                    { 1, 2, 3 },
                    { 4, 5, 6 } };
  
    check(m);
  
    return 0;
}

chevron_right


Output:

Yes

Time Complexity: O(N × M)
Auxiliary Space: O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.