Check if a Matrix is Bitonic or not | C # program to check if a matrix is Bitonic or not ; Function to check if an array is Bitonic or not ; Check for increasing sequence ; Check for decreasing sequence ; Function to check whether given matrix is bitonic or not ; Check row - wise ; Check column wise ; Generate an array consisting of elements of the current column ; Driver Code
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 ) ; } }