Check if Bitwise AND of concatenation of diagonals exceeds that of middle row / column elements of a Binary Matrix | 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
function convert ( p ) { let ans = 0 ; for ( let i of p ) { ans = ( ans << 1 ) | i ; } return ans ; } function count ( num ) { let ans = 0 ; while ( num > 0 ) { ans += num & 1 ; num >>= 1 ; } return ans ; } function checkGoodMatrix ( mat ) { let P = [ ] , S = [ ] , MR = [ ] , MC = [ ] ; for ( let i = 0 ; i < mat . length ; i ++ ) { for ( let j = 0 ; j < mat [ 0 ] . length ; j ++ ) { if ( i == j ) P . push ( mat [ i ] [ j ] ) ; if ( i + j == mat . length - 1 ) S . push ( mat [ i ] [ j ] ) ; if ( i == Math . floor ( ( mat . length - 1 ) / 2 ) ) MR . push ( mat [ i ] [ j ] ) ; if ( j == Math . floor ( ( mat . length - 1 ) / 2 ) ) MC . push ( mat [ i ] [ j ] ) ; } } S . reverse ( ) ; let P0 = convert ( P ) ; let S0 = convert ( S ) ; let MR0 = convert ( MR ) ; let MC0 = convert ( MC ) ; let setBitsPS = count ( ( P0 & S0 ) ) ; let setBitsMM = count ( ( MR0 & MC0 ) ) ; if ( setBitsPS > setBitsMM ) document . write ( " " ) ; else document . write ( " " ) ; } let mat = [ [ 1 , 0 , 1 ] , [ 0 , 0 , 1 ] , [ 0 , 1 , 1 ] ] ; checkGoodMatrix ( mat ) ;