Check if matrix A can be converted to B by changing parity of corner elements of any submatrix | Java implementation of the above approach ; Boolean function that returns true or false ; Traverse for all elements ; If both are not equal ; Change the parity of all corner elements ; Check if A is equal to B ; Not equal ; Driver Code ; First binary matrix ; Second binary matrix
class GFG { static final int N = 3 , M = 3 ; static boolean check ( int a [ ] [ ] , int b [ ] [ ] ) { for ( int i = 1 ; i < N ; i ++ ) { for ( int j = 1 ; j < M ; j ++ ) { if ( a [ i ] [ j ] != b [ i ] [ j ] ) { a [ i ] [ j ] ^= 1 ; a [ 0 ] [ 0 ] ^= 1 ; a [ 0 ] [ j ] ^= 1 ; a [ i ] [ 0 ] ^= 1 ; } } } for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { if ( a [ i ] [ j ] != b [ i ] [ j ] ) return false ; } } return true ; } public static void main ( String args [ ] ) { int a [ ] [ ] = { { 0 , 1 , 0 } , { 0 , 1 , 0 } , { 1 , 0 , 0 } } ; int b [ ] [ ] = { { 1 , 0 , 0 } , { 1 , 0 , 0 } , { 1 , 0 , 0 } } ; if ( check ( a , b ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }