Check if Bitwise AND of concatenation of diagonals exceeds that of middle row / column elements of a Binary Matrix | Java program for the above approach ; Function to convert obtained binary representation to decimal value ; Stores the resultant number ; Traverse string arr ; Return the number formed ; Function to count the number of set bits in the number num ; Stores the count of set bits ; Iterate until num > 0 ; Function to check if the given matrix satisfies the given condition or not ; To get P , S , MR , and MC ; Stores decimal equivalents of binary representations ; Gett the number of set bits ; Print the answer ; Driver code
import java . util . ArrayList ; import java . util . Collections ; class GFG { static int convert ( ArrayList < Integer > p ) { int ans = 0 ; for ( int i : p ) { ans = ( ans << 1 ) | i ; } return ans ; } static int count ( int num ) { int ans = 0 ; while ( num > 0 ) { ans += num & 1 ; num >>= 1 ; } return ans ; } static void checkGoodMatrix ( int mat [ ] [ ] ) { ArrayList < Integer > P = new ArrayList < Integer > ( ) ; ArrayList < Integer > S = new ArrayList < Integer > ( ) ; ArrayList < Integer > MR = new ArrayList < Integer > ( ) ; ArrayList < Integer > MC = new ArrayList < Integer > ( ) ; for ( int i = 0 ; i < mat . length ; i ++ ) { for ( int j = 0 ; j < mat [ 0 ] . length ; j ++ ) { if ( i == j ) P . add ( mat [ i ] [ j ] ) ; if ( i + j == mat . length - 1 ) S . add ( mat [ i ] [ j ] ) ; if ( i == Math . floor ( ( mat . length - 1 ) / 2 ) ) MR . add ( mat [ i ] [ j ] ) ; if ( j == Math . floor ( ( mat . length - 1 ) / 2 ) ) MC . add ( mat [ i ] [ j ] ) ; } } Collections . reverse ( S ) ; int P0 = convert ( P ) ; int S0 = convert ( S ) ; int MR0 = convert ( MR ) ; int MC0 = convert ( MC ) ; int setBitsPS = count ( ( P0 & S0 ) ) ; int setBitsMM = count ( ( MR0 & MC0 ) ) ; if ( setBitsPS > setBitsMM ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } public static void main ( String [ ] args ) { int mat [ ] [ ] = { { 1 , 0 , 1 } , { 0 , 0 , 1 } , { 0 , 1 , 1 } } ; checkGoodMatrix ( mat ) ; } }