# Check if a Matrix is Reverse Bitonic or Not

Last Updated : 07 May, 2021

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

 `// C++ Program to check if a` `// matrix is Reverse Bitonic or not` `#include ` `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;` `}`

## Java

 `// Java Program to check if a` `// matrix is Reverse Bitonic or not` `import` `java.util.*;` `class` `GFG{`   `static` `int` `N = ``3``, M = ``3``;`   `// Function to check if an` `// array is Reverse Bitonic or not` `static` `boolean` `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` `static` `void` `check(``int` `arr[][])` `{` `    ``int` `f = ``0``;`   `    ``// Check row-wise` `    ``for` `(``int` `i = ``0``; i < N; i++) ` `    ``{` `        ``if` `(!checkReverseBitonic(arr[i], M))` `        ``{` `            ``System.out.print(``"No"` `+ ``"\n"``);` `            ``return``;` `        ``}` `    ``}`   `    ``// Check column wise` `    ``for` `(``int` `i = ``0``; i < N; i++) ` `    ``{` `        ``// Generate an array` `        ``// consisting of elements` `        ``// of the current column` `        ``int` `temp[] = ``new` `int``[N];`   `        ``for` `(``int` `j = ``0``; j < N; j++) ` `        ``{` `            ``temp[j] = arr[j][i];` `        ``}`   `        ``if` `(!checkReverseBitonic(temp, N)) ` `        ``{` `            ``System.out.print(``"No"` `+ ``"\n"``);` `            ``return``;` `        ``}` `    ``}` `    ``System.out.print(``"Yes"``);` `}`   `// Driver Code` `public` `static` `void` `main(String[] args)` `{` `    ``int` `m[][] = { { ``2``, ``3``, ``4` `},` `                  ``{ ``1``, ``2``, ``3` `},` `                  ``{ ``4``, ``5``, ``6` `} };`   `    ``check(m);` `}` `}`   `// This code is contributed by Rajput-Ji`

## Python3

 `# Python3 program to check if a` `# matrix is Reverse Bitonic or not` `N ``=` `3` `M ``=` `3`   `# Function to check if an` `# array is Reverse Bitonic or not` `def` `checkReverseBitonic(arr, n):`   `    ``f ``=` `0`   `    ``# Check for decreasing sequence` `    ``for` `i ``in` `range``(``1``, n):` `        ``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 ``in` `range``(i ``+` `1``, n):` `        ``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` `def` `check(arr):`   `    ``f ``=` `0`   `    ``# Check row-wise` `    ``for` `i ``in` `range` `(N):` `        ``if` `(``not` `checkReverseBitonic(arr[i], M)): ` `            ``print``(``"No"``)` `            ``return`   `    ``# Check column wise` `    ``for` `i ``in` `range``(N):` `        `  `        ``# Generate an array` `        ``# consisting of elements` `        ``# of the current column` `        ``temp ``=` `[``0``] ``*` `N`   `        ``for` `j ``in` `range``(N):` `            ``temp[j] ``=` `arr[j][i]`   `        ``if` `(``not` `checkReverseBitonic(temp, N)):` `            ``print``(``"No"``)` `            ``return`   `    ``print``(``"Yes"``)`   `# Driver Code` `if` `__name__ ``=``=` `"__main__"``:`   `    ``m ``=` `[ [ ``2``, ``3``, ``4` `],` `          ``[ ``1``, ``2``, ``3` `],` `          ``[ ``4``, ``5``, ``6` `] ]`   `    ``check(m)`   `# This code is contributed by chitranayal`

## C#

 `// C# Program to check if a` `// matrix is Reverse Bitonic or not` `using` `System;` `class` `GFG{`   `static` `int` `N = 3, M = 3;`   `// Function to check if an` `// array is Reverse Bitonic or not` `static` `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` `static` `void` `check(``int` `[,]arr)` `{` `    ``// Check row-wise` `    ``for` `(``int` `i = 0; i < N; i++) ` `    ``{` `        ``if` `(!checkReverseBitonic(GetRow(arr, i), M))` `        ``{` `            ``Console.Write(``"No"` `+ ``"\n"``);` `            ``return``;` `        ``}` `    ``}`   `    ``// Check column wise` `    ``for` `(``int` `i = 0; i < N; i++) ` `    ``{` `        ``// Generate an array` `        ``// consisting of elements` `        ``// of the current column` `        ``int` `[]temp = ``new` `int``[N];`   `        ``for` `(``int` `j = 0; j < N; j++) ` `        ``{` `            ``temp[j] = arr[j,i];` `        ``}`   `        ``if` `(!checkReverseBitonic(temp, N)) ` `        ``{` `            ``Console.Write(``"No"` `+ ``"\n"``);` `            ``return``;` `        ``}` `    ``}` `    ``Console.Write(``"Yes"``);` `}` `  `  `public` `static` `int``[] GetRow(``int``[,] matrix, ``int` `row)` `{` `    ``var` `rowLength = matrix.GetLength(1);` `    ``var` `rowVector = ``new` `int``[rowLength];`   `    ``for` `(``var` `i = 0; i < rowLength; i++)` `      ``rowVector[i] = matrix[row, i];`   `    ``return` `rowVector;` `}` `  `  `// Driver Code` `public` `static` `void` `Main(String[] args)` `{` `    ``int` `[,]m = {{ 2, 3, 4 },` `                ``{ 1, 2, 3 },` `                ``{ 4, 5, 6 }};`   `    ``check(m);` `}` `}`   `// This code is contributed by Rajput-Ji`

## Javascript

 ``

Output:

`Yes`

Time Complexity: O(N Ã— M)
Auxiliary Space: O(N)

Previous
Next