Check if matrix A can be converted to B by changing parity of corner elements of any submatrix

Given two binary matrices A[][] and B[][] of N X M. In a single operation, one can choose a sub-matrix (min of 2 rows and 2c columns) and change the parity of the corner elements i.e. 1 can be changed to a 0 and 0 can be changed to a 1. The task is to check if the matrix A can be converted to B using any number of operations.

Examples:

Input: A[][] = {{0, 1, 0}, {0, 1, 0}, {1, 0, 0}},
B[][] = {{1, 0, 0}, {1, 0, 0}, {1, 0, 0}}
Output: Yes
Choose the sub-matrix whose top-left corner is (0, 0)
and bottom-right corner is (1, 1).
Changing the parity of the corner elements
of this sub-matrix will convert A[][] to B[][]

Input: A[][] = {{0, 1, 0, 1}, {1, 0, 1, 0}, {0, 1, 0, 1}},
B[][] = {{1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}}
Output: No

Approach: The first thing to notice is that despite the conversions the total parity of both the matrix will remain the same. Hence check if a[i][j] is not same as b[i][j] then change the parity of the corner elements of the sub-matrix whose left top corner is (0, 0) and right bottom corner is (i, j) for 1 ≤ i, j. After performing parity change for every a[i][j] which is not equal to b[i][j], check if both the matrix are same or not. If they are the same, we can change A to B.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the
// above appoach
  
#include <bits/stdc++.h>
using namespace std;
#define N 3
#define M 3
  
// Boolean function that returns
// true or false
bool check(int a[N][M], int b[N][M])
{
    // Traverse for all elements
    for (int i = 1; i < N; i++) {
        for (int j = 1; j < M; j++) {
  
            // If both are not equal
            if (a[i][j] != b[i][j]) {
  
                // Change the parity of
                // all corner elements
                a[i][j] ^= 1;
                a[0][0] ^= 1;
                a[0][j] ^= 1;
                a[i][0] ^= 1;
            }
        }
    }
  
    // Check if A is equal to B
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < M; j++) {
  
            // Not equal
            if (a[i][j] != b[i][j])
                return false;
        }
    }
    return true;
}
  
// Driver Code
int main()
{
    // First binary matrix
    int a[N][N] = { { 0, 1, 0 },
                    { 0, 1, 0 },
                    { 1, 0, 0 } };
  
    // Second binary matrix
    int b[N][N] = { { 1, 0, 0 },
                    { 1, 0, 0 },
                    { 1, 0, 0 } };
  
    if (check(a, b))
        cout << "Yes";
    else
        cout << "No";
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 implementation of the
# above appoach
N = 3
M = 3
  
# Boolean function that returns
# true or false
def check(a, b):
      
    # Traverse for all elements
    for i in range(1, N, 1):
        for j in range(1, M, 1):
              
            # If both are not equal
            if (a[i][j] != b[i][j]):
                  
                # Change the parity of
                # all corner elements
                a[i][j] ^= 1
                a[0][0] ^= 1
                a[0][j] ^= 1
                a[i][0] ^= 1
  
    # Check if A is equal to B
    for i in range(N):
        for j in range(M):
              
            # Not equal
            if (a[i][j] != b[i][j]):
                return False
      
    return True
  
# Driver Code
if __name__ == '__main__':
      
    # First binary matrix
    a = [[0, 1, 0],
         [0, 1, 0],
         [1, 0, 0]]
  
    # Second binary matrix
    b = [[1, 0, 0],
         [1, 0, 0],
         [1, 0, 0]]
  
    if (check(a, b)):
        print("Yes")
    else:
        print("No")
  
# This code is contributed by
# Surendra_Gangwar

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the 
// above appoach 
using System; 
  
class GFG
{
      
static readonly int N = 3,M =3; 
  
// Boolean function that returns 
// true or false 
static bool check(int [,]a, int [,]b) 
    // Traverse for all elements 
    for (int i = 1; i < N; i++) 
    
        for (int j = 1; j < M; j++) 
        
  
            // If both are not equal 
            if (a[i,j] != b[i,j]) 
            
  
                // Change the parity of 
                // all corner elements 
                a[i,j] ^= 1; 
                a[0,0] ^= 1; 
                a[0,j] ^= 1; 
                a[i,0] ^= 1; 
            
        
    
  
    // Check if A is equal to B 
    for (int i = 0; i < N; i++)
    
        for (int j = 0; j < M; j++)
        
  
            // Not equal 
            if (a[i,j] != b[i,j]) 
                return false
        
    
    return true
  
// Driver Code 
public static void Main(String []args)
    // First binary matrix 
    int [,]a = { { 0, 1, 0 }, 
                    { 0, 1, 0 }, 
                    { 1, 0, 0 } }; 
  
    // Second binary matrix 
    int [,]b = { { 1, 0, 0 }, 
                    { 1, 0, 0 }, 
                    { 1, 0, 0 } }; 
  
    if (check(a, b)) 
        Console.Write( "Yes"); 
    else
        Console.Write( "No"); 
}
  
// This code has been contributed by 29AjayKumar

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP implementation of the above appoach 
  
$N = 3; 
$M = 3 ;
  
// Boolean function that returns 
// true or false 
function check($a, $b
    // Traverse for all elements 
    for ($i = 1; $i < $GLOBALS['N']; $i++) 
    
        for ($j = 1; $j < $GLOBALS['M']; $j++) 
        
  
            // If both are not equal 
            if ($a[$i][$j] != $b[$i][$j]) 
            
  
                // Change the parity of 
                // all corner elements 
                $a[$i][$j] ^= 1; 
                $a[0][0] ^= 1; 
                $a[0][$j] ^= 1; 
                $a[$i][0] ^= 1; 
            
        
    
  
    // Check if A is equal to B 
    for ($i = 0; $i < $GLOBALS['N']; $i++) 
    
        for ($j = 0; $j < $GLOBALS['M']; $j++) 
        
  
            // Not equal 
            if ($a[$i][$j] != $b[$i][$j]) 
                return false; 
        
    
    return true; 
  
    // Driver Code 
    // First binary matrix 
    $a = array(array( 0, 1, 0 ), 
                array( 0, 1, 0 ), 
                array( 1, 0, 0 ) ); 
  
    // Second binary matrix 
    $b = array( array( 1, 0, 0 ), 
                array(1, 0, 0 ), 
                array( 1, 0, 0 ) ); 
  
    if (check($a, $b)) 
        echo "Yes"
    else
        echo "No"
  
// This code is contributed by Ryuga
?>

chevron_right


Output:

Yes


My Personal Notes arrow_drop_up

Striver(underscore)79 at Codechef and codeforces D

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.