Check if rows of a Matrix can be rearranged to make Bitwise XOR of first column non | Function to check if there is any row where number of unique elements are greater than 1 ; Iterate over the matrix ; Function to check if it is possible to rearrange mat [ ] [ ] such that XOR of its first column is non - zero ; Find bitwise XOR of the first column of mat [ ] [ ] ; If bitwise XOR of the first column of mat [ ] [ ] is non - zero ; Otherwise check rearrangements ; Given Matrix mat [ ] [ ] ; Function Call
function checkRearrangements ( mat , N , M ) { for ( let i = 0 ; i < N ; i ++ ) { for ( let j = 1 ; j < M ; j ++ ) { if ( mat [ i ] [ 0 ] != mat [ i ] [ j ] ) { return " " ; } } } return " " ; } function nonZeroXor ( mat , N , M ) { let res = 0 ; for ( let i = 0 ; i < N ; i ++ ) { res = res ^ mat [ i ] [ 0 ] ; } if ( res != 0 ) return " " ; else return checkRearrangements ( mat , N , M ) ; } let mat = [ [ 1 , 1 , 2 ] , [ 2 , 2 , 2 ] , [ 3 , 3 , 3 ] ] ; let N = mat . length ; let M = mat [ 0 ] . length ; document . write ( nonZeroXor ( mat , N , M ) ) ;