# Find a number such that maximum in array is minimum possible after XOR

Given an array of non-negative integers. Choose an integer **P** and take XOR of **P** with all elements of array. The task is to choose **P** such that the maximum value of the array is minimum possible after performing XOR of all elements of the array with P.

**Examples:**

Input:arr = {3, 2, 1}

Output:2

Explanation:

We can choose P = 3 such that after taking XOR the maximum element is minimum possible.

After taking exclusive-OR arr = {0, 1, 2}

2 is minimum possible value.

Input:arr = {5, 1}

Output:4

**Approach:**

- We can solve this problem recursively starting from the most significant bit.
- Split the elements into two groups, one with the elements which have the current bit on (1) and one with the elements which have the current bit off (0).
- If either group is empty, we can assign the current bit of P accordingly so that we have the current bit off in our answer.
- Otherwise, If both groups aren’t empty, so whatever value we assign to the current bit of P, we will have this bit on(1) in our answer.
- Now, To decide which value to assign to the current bit of P, we will recursively call for each of the groups for the next bit and return the minimum of both.

Below is the implementation of the above approach:

`// C++ program that find the minimum ` `// possible maximum ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Recursive function that find the ` `// minimum value after exclusive-OR ` `int` `RecursiveFunction(vector<` `int` `> ref, ` ` ` `int` `bit) ` `{ ` ` ` `// Condition if ref size is zero or ` ` ` `// bit is negative then return 0 ` ` ` `if` `(ref.size() == 0 || bit < 0) ` ` ` `return` `0; ` ` ` ` ` `vector<` `int` `> curr_on, curr_off; ` ` ` ` ` `for` `(` `int` `i = 0; i < ref.size(); i++) ` ` ` `{ ` ` ` `// Condition if current bit is ` ` ` `// off then push current value ` ` ` `// in curr_off vector ` ` ` `if` `(((ref[i] >> bit) & 1) == 0) ` ` ` `curr_off.push_back(ref[i]); ` ` ` ` ` `// Condition if current bit is on ` ` ` `// then push current value in ` ` ` `// curr_on vector ` ` ` `else` ` ` `curr_on.push_back(ref[i]); ` ` ` `} ` ` ` ` ` `// Condition if curr_off is empty ` ` ` `// then call recursive function ` ` ` `// on curr_on vector ` ` ` `if` `(curr_off.size() == 0) ` ` ` `return` `RecursiveFunction(curr_on, ` ` ` `bit - 1); ` ` ` ` ` `// Condition if curr_on is empty ` ` ` `// then call recursive function ` ` ` `// on curr_off vector ` ` ` `if` `(curr_on.size() == 0) ` ` ` `return` `RecursiveFunction(curr_off, ` ` ` `bit - 1); ` ` ` ` ` `// Return the minimum of curr_off and ` ` ` `// curr_on and add power of 2 of ` ` ` `// current bit ` ` ` `return` `min(RecursiveFunction(curr_off, ` ` ` `bit - 1), ` ` ` `RecursiveFunction(curr_on, ` ` ` `bit - 1)) ` ` ` `+ (1 << bit); ` `} ` ` ` `// Function that print the minimum ` `// value after exclusive-OR ` `void` `PrintMinimum(` `int` `a[], ` `int` `n) ` `{ ` ` ` `vector<` `int` `> v; ` ` ` ` ` `// Pushing values in vector ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `v.push_back(a[i]); ` ` ` ` ` `// Printing answer ` ` ` `cout << RecursiveFunction(v, 30) ` ` ` `<< ` `"\n"` `; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 3, 2, 1 }; ` ` ` ` ` `int` `size = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` ` ` `PrintMinimum(arr, size); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

2

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- Maximum and minimum of an array using minimum number of comparisons
- Minimum number greater than the maximum of array which cannot be formed using the numbers in the array
- Find the minimum and maximum sum of N-1 elements of the array
- Program to find the minimum (or maximum) element of an array
- C program to find the maximum and minimum element of the array
- Recursive Programs to find Minimum and Maximum elements of array
- Sum of Maximum and Minimum prime factor of every number in the Array
- Minimum and Maximum element of an array which is divisible by a given number k
- Number of times Maximum and minimum value updated during traversal of array
- Find a number which give minimum sum when XOR with every number of array of integers
- Missing occurrences of a number in an array such that maximum absolute difference of adjacent elements is minimum
- Find prime number K in an array such that (A[i] % K) is maximum
- Find the minimum number of moves to reach end of the array
- Find minimum number K such that sum of array after multiplication by K exceed S
- Find a number that divides maximum array elements
- Find integers that divides maximum number of elements of the array
- Find the minimum number of elements that should be removed to make an array good
- Find minimum number of merge operations to make an array palindrome
- Array Range Queries to find the Maximum Armstrong number with updates
- Array range queries to find the maximum Fibonacci number with updates

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.