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++
#include <bits/stdc++.h>
using namespace std;
const int N = 3, M = 3;
bool checkBitonic( int arr[], int n)
{
int i, j, f = 0;
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 ;
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 ;
}
void check( int arr[N][M])
{
int f = 0;
for ( int i = 0; i < N; i++) {
if (!checkBitonic(arr[i], M)) {
cout << "NO" << endl;
return ;
}
}
for ( int i = 0; i < N; i++) {
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" ;
}
int main()
{
int m[N][M] = { { 1, 2, 3 },
{ 3, 4, 5 },
{ 2, 6, 4 } };
check(m);
return 0;
}
|
Java
class GFG{
final static int N = 3 , M = 3 ;
static boolean checkBitonic( int arr[], int n)
{
int i, j, f = 0 ;
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 ;
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 ;
}
static void check( int arr[][])
{
int f = 0 ;
for ( int i = 0 ; i < N; i++)
{
if (!checkBitonic(arr[i], M))
{
System.out.println( "NO" );
return ;
}
}
for ( int i = 0 ; i < N; i++)
{
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" );
}
public static void main(String[] args)
{
int m[][] = { { 1 , 2 , 3 },
{ 3 , 4 , 5 },
{ 2 , 6 , 4 } };
check(m);
}
}
|
Python3
N = 3
M = 3
def checkBitonic(arr, n):
i, j, f = 0 , 0 , 0
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
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
def check(arr):
f = 0
for i in range (N):
if ( not checkBitonic(arr[i], M)):
print ( "NO" )
return
i = 0
for i in range (N):
temp = [ 0 ] * N
for j in range (N):
temp[j] = arr[j][i]
if ( not checkBitonic(temp, N)):
print ( "NO" )
return
print ( "YES" )
if __name__ = = '__main__' :
m = [ [ 1 , 2 , 3 ],
[ 3 , 4 , 5 ],
[ 2 , 6 , 4 ] ]
check(m)
|
C#
using System;
class GFG{
readonly static int N = 3, M = 3;
static bool checkBitonic( int []arr, int n)
{
int i, j, f = 0;
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 ;
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 ;
}
static void check( int [,]arr)
{
int f = 0;
for ( int i = 0; i < N; i++)
{
if (!checkBitonic(GetRow(arr, i), M))
{
Console.WriteLine( "NO" );
return ;
}
}
for ( int i = 0; i < N; i++)
{
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;
}
public static void Main(String[] args)
{
int [,]m = { { 1, 2, 3 },
{ 3, 4, 5 },
{ 2, 6, 4 } };
check(m);
}
}
|
Javascript
<script>
let N = 3, M = 3;
function checkBitonic(arr,n)
{
let i, j, f = 0;
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 ;
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 check(arr)
{
let f = 0;
for (let i = 0; i < N; i++)
{
if (!checkBitonic(arr[i], M))
{
document.write( "NO" );
return ;
}
}
for (let i = 0; i < N; i++)
{
let temp = [N];
for (let j = 0; j < N; j++)
{
temp[j] = arr[j][i];
}
if (!checkBitonic(temp, N))
{
document.write( "NO" );
return ;
}
}
document.write( "YES" );
}
let m = [[ 1, 2, 3 ],
[3, 4, 5 ],
[ 2, 6, 4 ]];
check(m);
</script>
|
Time Complexity: O(N * N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...