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.

Below is C++ implementation of this approach:

// 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 then 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; }

Output:

3

**Time Complexity : **O(n).

This article is contributed by **Anuj Chauhan**. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.