# Check if a Matrix is Bitonic or not

• Last Updated : 15 Nov, 2022

Given a matrix m[][], the task is to check if the given matrix is Bitonic or not. If the given matrix is 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 increasing followed by strictly decreasing

Then the given matrix is said to be a Bitonic Matrix

Example:

Input: m[][] = {{1, 2, 3}, {4, 5, 6}, {2, 3, 4}}
Output: YES
Explanation:
All the columns of the given matrix {1, 4, 2}, {2, 5, 3}, {3, 6, 4} forms an increasing followed by decreasing sequence
All the rows of the given matrix have an increasing sequence.
Therefore, the matrix is Bitonic.
Input: m[][] = {{1, 2, 3}, {4, 5, 6}, {2, 5, 4}}
Output: NO
Explanation:
Since the column {2, 5, 5} does not satisfy any of the three conditions, the given matrix is not 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 Bitonic sequence or not. If any row is found to be not Bitonic, print NO.
• Similarly, check the elements of each column one by one,, if it forms a Bitonic sequence or not. If any row is found to be not Bitonic, print NO.
• If all the rows and columns are found to be Bitonic, then print YES

Below is the implementation of the above approach:

## C++

 `// C++ Program to check if a``// matrix is Bitonic or not``#include ``using` `namespace` `std;` `const` `int` `N = 3, M = 3;` `// Function to check if an``// array is Bitonic or not``bool` `checkBitonic(``int` `arr[], ``int` `n)``{``    ``int` `i, j, f = 0;` `    ``// Check for increasing 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 decreasing 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` `(!checkBitonic(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` `(!checkBitonic(temp, N)) {``            ``cout << ``"NO"` `<< endl;``            ``return``;``        ``}``    ``}` `    ``cout << ``"YES"``;``}` `// Driver Code``int` `main()``{``    ``int` `m[N][M] = { { 1, 2, 3 },``                    ``{ 3, 4, 5 },``                    ``{ 2, 6, 4 } };` `    ``check(m);` `    ``return` `0;``}`

## Java

 `// Java program to check if a``// matrix is Bitonic or not``class` `GFG{` `final` `static` `int` `N = ``3``, M = ``3``;` `// Function to check if an``// array is Bitonic or not``static` `boolean` `checkBitonic(``int` `arr[], ``int` `n)``{``    ``int` `i, j, f = ``0``;` `    ``// Check for increasing 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 decreasing 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` `(!checkBitonic(arr[i], M))``        ``{``            ``System.out.println(``"NO"``);``            ``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` `(!checkBitonic(temp, N))``        ``{``            ``System.out.println(``"NO"``);``            ``return``;``        ``}``    ``}``    ``System.out.println(``"YES"``);``}``    ` `// Driver Code``public` `static` `void` `main(String[] args)``{``    ``int` `m[][] = { { ``1``, ``2``, ``3` `},``                  ``{ ``3``, ``4``, ``5` `},``                  ``{ ``2``, ``6``, ``4` `} };``                  ` `    ``check(m);``}``}` `// This code is contributed by rutvik_56`

## Python3

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

## C#

 `// C# program to check if a``// matrix is Bitonic or not``using` `System;``class` `GFG{` `readonly` `static` `int` `N = 3, M = 3;` `// Function to check if an``// array is Bitonic or not``static` `bool` `checkBitonic(``int` `[]arr, ``int` `n)``{``    ``int` `i, j, f = 0;` `    ``// Check for increasing 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 decreasing 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` `(!checkBitonic(GetRow(arr, i), M))``        ``{``            ``Console.WriteLine(``"NO"``);``            ``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` `(!checkBitonic(temp, N))``        ``{``            ``Console.WriteLine(``"NO"``);``            ``return``;``        ``}``    ``}``    ``Console.WriteLine(``"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 = { { 1, 2, 3 },``                 ``{ 3, 4, 5 },``                 ``{ 2, 6, 4 } };``                  ` `    ``check(m);``}``}` `// This code is contributed by sapnasingh4991`

## Javascript

 ``

Output:

`YES`

Time Complexity: O(N * N)
Auxiliary Space: O(N)

My Personal Notes arrow_drop_up