Check if all rows of a Binary Matrix have all ones placed adjacently or not | Function to check if all 1 s are placed adjacently in an array or not ; Base Case ; Stores the sum of XOR of all pair of adjacent elements ; Calculate sum of XOR of all pair of adjacent elements ; Check for corner cases ; Return true ; Function to check if all the rows have all 1 s grouped together or not ; Traverse each row ; Check if all 1 s are placed together in the ith row or not ; Function to check if all 1 s in a row are grouped together in a matrix or not ; Print the result ; Given matrix ; Function Call
def checkGroup ( arr ) : NEW_LINE INDENT if len ( arr ) <= 2 : NEW_LINE INDENT return True NEW_LINE DEDENT corner = arr [ 0 ] + arr [ - 1 ] NEW_LINE xorSum = 0 NEW_LINE for i in range ( len ( arr ) - 1 ) : NEW_LINE INDENT xorSum += ( arr [ i ] ^ arr [ i + 1 ] ) NEW_LINE DEDENT if not corner : NEW_LINE INDENT if xorSum > 2 : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT elif corner == 1 : NEW_LINE INDENT if xorSum > 1 : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if xorSum > 0 : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def isInGroupUtil ( mat ) : NEW_LINE INDENT for i in mat : NEW_LINE INDENT if not checkGroup ( i ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def isInGroup ( mat ) : NEW_LINE INDENT ans = isInGroupUtil ( mat ) NEW_LINE if ans : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT mat = [ [ 0 , 1 , 1 , 0 ] , [ 1 , 1 , 0 , 0 ] , [ 0 , 0 , 0 , 1 ] , [ 1 , 1 , 1 , 0 ] ] NEW_LINE isInGroup ( mat ) NEW_LINE