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

Last Updated : 19 Sep, 2023

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++

 `// 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 ` `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

 `// 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. `

## Python3

 `# 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#

 `// 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

 ` `

## Javascript

 ``

Output

`3`

Time Complexity : O(n).

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

Previous
Next