Check if all rows of a Binary Matrix have all ones placed adjacently or not | C # program for the above approach ; 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 ; Driver code ; Given matrix ; Function Call
using System ; using System . Collections . Generic ; class GFG { static bool checkGroup ( List < int > arr ) { if ( arr . Count <= 2 ) return true ; int corner = arr [ 0 ] + arr [ arr . Count - 1 ] ; int xorSum = 0 ; for ( int i = 0 ; i < arr . Count - 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 ; } static bool isInGroupUtil ( int [ , ] mat ) { for ( int i = 0 ; i < mat . GetLength ( 1 ) ; i ++ ) { List < int > arr = new List < int > ( ) ; for ( int j = 0 ; j < mat . GetLength ( 0 ) ; j ++ ) { arr . Add ( mat [ i , j ] ) ; } if ( ! checkGroup ( arr ) ) return false ; } return true ; } static void isInGroup ( int [ , ] mat ) { bool ans = isInGroupUtil ( mat ) ; if ( ans ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } static void Main ( ) { int [ , ] mat = { { 0 , 1 , 1 , 0 } , { 1 , 1 , 0 , 0 } , { 0 , 0 , 0 , 1 } , { 1 , 1 , 1 , 0 } } ; isInGroup ( mat ) ; } }