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 <bits/stdc++.h> 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 = 3M = 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 |
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.
Recommended Posts:
- Find bitonic point in given bitonic sequence
- Check if a Matrix is Reverse Bitonic or Not
- Program to check if an array is bitonic or not
- Check if a given string is a Reverse Bitonic String or not
- Check if it is possible to make the given matrix increasing matrix or not
- Program to check if a matrix is Binary matrix or not
- Maximum Length Bitonic Subarray | Set 1 (O(n) time and O(n) space)
- Printing Longest Bitonic Subsequence
- Maximum Length Bitonic Subarray | Set 2 (O(n) time and O(1) Space)
- Longest Bitonic Subsequence in O(n log n)
- Find an element in Bitonic array
- Number of bitonic arrays of length n and consisting of elements from 1 to n
- Maximum sum bitonic subarray
- Length of longest strict bitonic subsequence
- Queries on insertion of an element in a Bitonic Sequence
- Bitonic point in the given linked list
- Maximum product of bitonic subsequence of size 3
- Longest Reverse Bitonic Sequence
- Generate a Bitonic array starting with N and adjacent difference of K
- Count of all possible bitonic subarrays
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.

