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 the implementation of this approach:

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

*chevron_right*

*filter_none*

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

*chevron_right*

*filter_none*

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

*chevron_right*

*filter_none*

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

*chevron_right*

*filter_none*

## PHP

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

*chevron_right*

*filter_none*

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.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Minimum flips in a Binary array such that XOR of consecutive subarrays of size K have different parity
- XOR of two numbers after making length of their binary representations equal
- Minimum Group Flips to Make Binary Array Elements Same
- Minimum Subarray flips required to convert all elements of a Binary Array to K
- Count pairs from 1 to N such that their Sum is divisible by their XOR
- Divide array into two sub-arrays such that their averages are equal
- Count all Quadruples from four arrays such that their XOR equals to 'x'
- Count minimum right flips to set all values in an array
- Minimum Bitwise XOR operations to make any two array elements equal
- Minimum cost required to rearrange a given array to make it equal to another given array
- Rearrange given binary strings to maximize their Bitwise XOR value
- Minimum flips to make all 1s in left and 0s in right | Set 2
- Minimum flips to make all 1s in left and 0s in right | Set 1 (Using Bitmask)
- Find two numbers from their sum and XOR
- Maximum possible XOR of every element in an array with another array
- Check if array can be divided into two sub-arrays such that their absolute difference is K
- Choose two elements from the given array such that their sum is not present in any of the arrays
- Find sub-arrays from given two arrays such that they have equal sum
- Find XOR of two number without using XOR operator
- Find number of pairs in an array such that their XOR is 0