Open In App

Minimum flips in two binary arrays so that their XOR is equal to another array

Given three binary arrays, each of size n, the task is to find minimum flip of bits in the first and second array such that the XOR of i’th index bit of first and second arrays is equal to i’th index bit of third array. Given a constraint that we can only flip at most p bits of array 1 and at most q bits of array 2. If not possible, output -1. 

Rearrangement of bits is not allowed.

Examples : 

Input :  p = 2, q = 2
  arr1[] = {0, 0, 1}
  arr2[] = {0, 1, 0}
  arr3[] = {0, 1, 0}
Output : 1
arr1[0] ^ arr2[0] = 0 ^ 0 = 0, which is equal 
to arr3[0], so no flip required.
arr1[1] ^ arr2[1] = 0 ^ 1 = 1, which is equal
to arr3[1], so no flip required.
arr1[2] ^ arr2[2] = 1 ^ 0 = 1, which is not 
equal to arr3[0], so one flip required.
Also p = 2 and q = 2, so flip arr1[2].

Input :  p = 2, q = 4
  arr1 = { 1, 0, 1, 1, 1, 1, 1 }
  arr2 = { 0, 1, 1, 1, 1, 0, 0 }
  arr3 = { 1, 1, 1, 1, 0, 0, 1 }
Output : 3
When the XOR of i'th bit of array1 and arry2 is
equal to i'th bit of array3, no flip is required.

Now let's observe when XOR is not equal. 
There can be following cases:
Case 1: When arr3[i] = 0, 
        then either arr1[i] = 1, arr2[i] = 0 or
                    arr1[i] = 0, arr2[i] = 1.
Case 2: When arr3[i] = 1, 
        then either arr1[i] = 1, arr2[i] = 1 or 
                    arr1[i] = 0, arr2[i] = 0.
At least one flip is required in each case. 

For case 1, XOR should be 0 which can be obtained by 0 ^ 0 or 1 ^ 1 and for case 2, 1 can be obtained by 1 ^ 0 or 0 ^ 1. 
So, observe that we can flip either arr1[i] or arr2[i] depending on the value of p and q. 
If p = 0, flip arr2 need to be flip and if q is also 0, output -1. And similarly, if p = 0, flip arr1 need to be flip and if p is also 0, output -1
So, we can say that number of flips required to make XOR of arr1 and arr2 equal to arr3 should be less than or equal to p + q.

Implementation:




// C++ program to find minimum flip required to make
// XOR of two arrays equal to another array with
// constraints on number of flip on each array.
  
#include <bits/stdc++.h>
using namespace std;
  
// Return minimum number of flip required
int minflip(int arr1[], int arr2[], int arr3[],
            int p, int q, int n)
{
    int flip = 0;
  
    // Counting number of mismatch, XOR of arr1[] and
    // arr2[] is not equal to arr3[].
    for (int i = 0; i < n; i++)
        if (arr1[i] ^ arr2[i] != arr3[i])
            flip++;
  
    // if flip is less than allowed constraint return
    // it. else return -1.
    return (flip <= p + q) ? flip : -1;
}
  
// Driven Program
int main()
{
    int arr1[] = { 1, 0, 1, 1, 1, 1, 1 };
    int arr2[] = { 0, 1, 1, 1, 1, 0, 0 };
    int arr3[] = { 1, 1, 1, 1, 0, 0, 1 };
  
    int n = sizeof(arr1) / sizeof(arr1[0]);
    int p = 2, q = 4;
  
    cout << minflip(arr1, arr2, arr3, p, q, n);
    return 0;
}




// Java program to find minimum flip required to make
// XOR of two arrays equal to another array with
// constraints on number of flip on each array.
import java.io.*;
  
class GFG {
  
    // Return minimum number of flip required
    static int minflip(int[] arr1, int[] arr2, int[] arr3,
                                      int p, int q, int n)
    {
        int flip = 0;
  
        // Counting number of mismatch, XOR of arr1[] and
        // arr2[] is not equal to arr3[].
        for (int i = 0; i < n; i++)
            if (arr1[i] > 0 ^ arr2[i] > 0 != arr3[i] > 0)
                flip++;
  
        // if flip is less than allowed constraint return
        // it. else return -1.
        return (flip <= p + q) ? flip : -1;
    }
  
    // Driver program
    static public void main(String[] args)
    {
        int[] arr1 = {1, 0, 1, 1, 1, 1, 1};
        int[] arr2 = {0, 1, 1, 1, 1, 0, 0};
        int[] arr3 = {1, 1, 1, 1, 0, 0, 1};
  
        int n = arr1.length;
        int p = 2, q = 4;
  
        System.out.println(minflip(arr1, arr2, arr3, p, q, n));
    }
}
  
// This code is contributed by vt_m.




# Python 3 program to find 
# minimum flip required to 
# make XOR of two arrays 
# equal to another array
# with constraints on number
# of flip on each array.
  
# Return minimum number
# of flip required
def minflip(arr1, arr2, 
            arr3, p, q, n):
  
    flip = 0
  
    # Counting number of 
    # mismatch, XOR of 
    # arr1[] and arr2[] 
    # is not equal to arr3[].
    for i in range(0 , n):
        if (arr1[i] ^ 
            arr2[i] != arr3[i]):
            flip += 1
  
    # if flip is less than
    # allowed constraint return
    # it. else return -1.
    return flip if (flip <= p + q) else -1
  
# Driver Code
arr1 = [1, 0, 1, 1, 1, 1, 1
arr2 = [0, 1, 1, 1, 1, 0, 0]
arr3 = [1, 1, 1, 1, 0, 0, 1
  
n = len(arr1)
p = 2
q = 4
  
print(minflip(arr1, arr2, 
              arr3, p, q, n))
  
# This code is contributed 
# by Smitha




// C# program to find minimum flip required to make
// XOR of two arrays equal to another array with
// constraints on number of flip on each array.
using System;
  
class GFG {
  
    // Return minimum number of flip required
    static int minflip(int[] arr1, int[] arr2, int[] arr3,
                                      int p, int q, int n)
    {
        int flip = 0;
  
        // Counting number of mismatch, XOR of arr1[] and
        // arr2[] is not equal to arr3[].
        for (int i = 0; i < n; i++)
            if (arr1[i] > 0 ^ arr2[i] > 0 != arr3[i] > 0)
                flip++;
  
        // if flip is less than allowed constraint return
        // it. else return -1.
        return (flip <= p + q) ? flip : -1;
    }
  
    // Driver program
    static public void Main()
    {
        int[] arr1 = { 1, 0, 1, 1, 1, 1, 1 };
        int[] arr2 = { 0, 1, 1, 1, 1, 0, 0 };
        int[] arr3 = { 1, 1, 1, 1, 0, 0, 1 };
  
        int n = arr1.Length;
        int p = 2, q = 4;
  
        Console.WriteLine(minflip(arr1, arr2, arr3, p, q, n));
    }
}
  
// This code is contributed by vt_m.




<?php
// PHP program to find minimum
// flip required to make XOR
// of two arrays equal to another
// array with constraints on number
// of flip on each array.
  
// Return minimum number
// of flip required
function minflip($arr1, $arr2, $arr3,
                          $p, $q, $n)
{
    $flip = 0;
  
    // Counting number of mismatch,
    // XOR of arr1[] and arr2[] 
    // is not equal to arr3[].
    for ($i = 0; $i < $n; $i++)
        if ($arr1[$i] ^ $arr2[$i] != $arr3[$i])
            $flip++;
  
    // if flip is less than
    // allowed constraint return
    // it. else return -1.
    return ($flip <= $p + $q) ? $flip : -1;
}
  
    // Driver code
    $arr1 = array(1, 0, 1, 1, 1, 1, 1);
    $arr2 = array(0, 1, 1, 1, 1, 0, 0);
    $arr3 = array(1, 1, 1, 1, 0, 0, 1);
  
    $n = count($arr1);
    $p = 2; $q = 4;
  
    echo minflip($arr1, $arr2, $arr3, $p, $q, $n);
      
// This code is contributed by anuj_67.
?>




<script>
  
// Javascript program to find 
// minimum flip required to make
// XOR of two arrays equal to 
// another array with
// constraints on number of 
// flip on each array.
  
// Return minimum number
// of flip required
function minflip(arr1, arr2, arr3, p, q, n)
{
    let flip = 0;
  
    // Counting number of mismatch,
    // XOR of arr1[] and
    // arr2[] is not equal to arr3[].
    for (let i = 0; i < n; i++)
        if (arr1[i] ^ arr2[i] != arr3[i])
            flip++;
  
    // if flip is less than 
    // allowed constraint return
    // it. else return -1.
    return (flip <= p + q) ? flip : -1;
}
  
// Driven Program
    let arr1 = [ 1, 0, 1, 1, 1, 1, 1 ];
    let arr2 = [ 0, 1, 1, 1, 1, 0, 0 ];
    let arr3 = [ 1, 1, 1, 1, 0, 0, 1 ];
  
    let n = arr1.length;
    let p = 2, q = 4;
  
    document.write(minflip(arr1, arr2, arr3, p, q, n));
  
</script>

Output
3

Time Complexity : O(n).

Auxiliary Space: O(1) because constant space is being used for variables

 


Article Tags :