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++
#include <bits/stdc++.h>
using namespace std;
const int N = 3, M = 3;
bool checkReverseBitonic( 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 (!checkReverseBitonic(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 (!checkReverseBitonic(temp, N)) {
cout << "No" << endl;
return ;
}
}
cout << "Yes" ;
}
int main()
{
int m[N][M] = { { 2, 3, 4 },
{ 1, 2, 3 },
{ 4, 5, 6 } };
check(m);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int N = 3 , M = 3 ;
static boolean checkReverseBitonic( 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 (!checkReverseBitonic(arr[i], M))
{
System.out.print( "No" + "\n" );
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 (!checkReverseBitonic(temp, N))
{
System.out.print( "No" + "\n" );
return ;
}
}
System.out.print( "Yes" );
}
public static void main(String[] args)
{
int m[][] = { { 2 , 3 , 4 },
{ 1 , 2 , 3 },
{ 4 , 5 , 6 } };
check(m);
}
}
|
Python3
N = 3
M = 3
def checkReverseBitonic(arr, n):
f = 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 checkReverseBitonic(arr[i], M)):
print ( "No" )
return
for i in range (N):
temp = [ 0 ] * N
for j in range (N):
temp[j] = arr[j][i]
if ( not checkReverseBitonic(temp, N)):
print ( "No" )
return
print ( "Yes" )
if __name__ = = "__main__" :
m = [ [ 2 , 3 , 4 ],
[ 1 , 2 , 3 ],
[ 4 , 5 , 6 ] ]
check(m)
|
C#
using System;
class GFG{
static int N = 3, M = 3;
static bool checkReverseBitonic( 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)
{
for ( int i = 0; i < N; i++)
{
if (!checkReverseBitonic(GetRow(arr, i), M))
{
Console.Write( "No" + "\n" );
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 (!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;
}
public static void Main(String[] args)
{
int [,]m = {{ 2, 3, 4 },
{ 1, 2, 3 },
{ 4, 5, 6 }};
check(m);
}
}
|
Javascript
<script>
let N = 3, M = 3;
function checkReverseBitonic(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 (!checkReverseBitonic(arr[i], M))
{
document.write( "No" + "<br>" );
return ;
}
}
for (let i = 0; i < N; i++)
{
let temp= [N];
for (let j = 0; j < N; j++)
{
temp[j] = arr[j][i];
}
if (!checkReverseBitonic(temp, N))
{
document.write( "No" + "<br>" );
return ;
}
}
document.write( "Yes" );
}
let m = [[ 2, 3, 4 ],
[ 1, 2, 3 ],
[ 4, 5, 6 ]];
check(m);
</script>
|
Time Complexity: O(N × M)
Auxiliary Space: O(N)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!