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
function checkGroup ( arr ) { if ( arr . length <= 2 ) return true ; let corner = arr [ 0 ] + arr [ arr . length - 1 ] ; let xorSum = 0 ; for ( let i = 0 ; i < arr . length - 1 ; i ++ ) xorSum += ( arr [ i ] ^ arr [ i + 1 ] ) ; if ( corner == 0 ) if ( xorSum > 2 ) return false ; else if ( corner == 1 ) if ( xorSum > 1 ) return false ; else if ( xorSum > 0 ) return false ; return true ; } function isInGroupUtil ( mat ) { for ( let i = 0 ; i < mat . length ; i ++ ) { let arr = [ ] for ( let j = 0 ; j < mat [ i ] . length ; j ++ ) { arr . push ( mat [ i ] [ j ] ) ; } if ( ! checkGroup ( arr ) ) return false ; } return true ; } function isInGroup ( mat ) { let ans = isInGroupUtil ( mat ) ; if ( ans ) document . write ( " " ) ; else document . write ( " " ) ; } let mat = [ [ 0 , 1 , 1 , 0 ] , [ 1 , 1 , 0 , 0 ] , [ 0 , 0 , 0 , 1 ] , [ 1 , 1 , 1 , 0 ] ] ; isInGroup ( mat ) ;