Check if Bitwise AND of concatenation of diagonals exceeds that of middle row / column elements of a Binary Matrix | Functio 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
def convert ( arr ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in arr : NEW_LINE INDENT ans = ( ans << 1 ) | i NEW_LINE DEDENT return ans NEW_LINE DEDENT def count ( num ) : NEW_LINE INDENT ans = 0 NEW_LINE while num : NEW_LINE INDENT ans += num & 1 NEW_LINE num >>= 1 NEW_LINE DEDENT return ans NEW_LINE DEDENT def checkGoodMatrix ( mat ) : NEW_LINE INDENT P = [ ] NEW_LINE S = [ ] NEW_LINE MR = [ ] NEW_LINE MC = [ ] NEW_LINE for i in range ( len ( mat ) ) : NEW_LINE INDENT for j in range ( len ( mat [ 0 ] ) ) : NEW_LINE INDENT if i == j : NEW_LINE INDENT P . append ( mat [ i ] [ j ] ) NEW_LINE DEDENT if i + j == len ( mat ) - 1 : NEW_LINE INDENT S . append ( mat [ i ] [ j ] ) NEW_LINE DEDENT if i == ( len ( mat ) - 1 ) // 2 : NEW_LINE INDENT MR . append ( mat [ i ] [ j ] ) NEW_LINE DEDENT if j == ( len ( mat ) - 1 ) // 2 : NEW_LINE INDENT MC . append ( mat [ i ] [ j ] ) NEW_LINE DEDENT DEDENT DEDENT S . reverse ( ) NEW_LINE P = convert ( P ) NEW_LINE S = convert ( S ) NEW_LINE MR = convert ( MR ) NEW_LINE MC = convert ( MC ) NEW_LINE setBitsPS = count ( P & S ) NEW_LINE setBitsMM = count ( MR & MC ) NEW_LINE if setBitsPS > setBitsMM : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT mat = [ [ 1 , 0 , 1 ] , [ 0 , 0 , 1 ] , [ 0 , 1 , 1 ] ] NEW_LINE checkGoodMatrix ( mat ) NEW_LINE