Given an array A consisting of N non-negative integers, the task is to choose an integer K such that the maximum of the xor values of K with all array elements is minimized. In other words find the minimum possible value of Z, where Z = max(A[i] xor K), 0 <= i <= n-1, for some value of K.
Input: A = [1, 2, 3]
On choosing K = 3, max(A[i] xor 3) = 2, and this is the minimum possible value.
Input: A = [3, 2, 5, 6]
Approach: To solve the problem mentioned above we will use recursion. We will start from the most significant bit in the recursive function.
- In the recursive step, split the element into two sections – one having the current bit on and the other with current bit off. If any of the sections doesn’t have a single element, then this particular bit for K can be chosen such that the final xor value has 0 at this bit position (since our aim is to minimise this value) and then proceed to the next bit in the next recursive step.
- If both the sections have some elements, then explore both the possibilities by placing 0 and 1 at this bit position and calculating the answer using the corresponding section in next recursive call.
Let answer_on be the value if 1 is placed and answer_off be the value if 0 is placed at this position (pos). Since both sections are non empty whichever bit we choose for K, 2pos will be added to the final value.
For each recursive step:
answer = min(answer_on, answer_off) + 2pos
Below is the implementation of the above approach:
Time Complexity: O(N * log(max(Ai))
- Choose X such that (A xor X) + (B xor X) is minimized
- Number of ways to choose elements from the array such that their average is K
- Find the original Array using XOR values of all adjacent elements
- Tree with N nodes and K leaves such that distance between farthest leaves is minimized
- Partition a set into two subsets such that difference between max of one and min of other is minimized
- Maximum XOR value of maximum and second maximum element among all possible subarrays
- Find maximum XOR of given integer in a stream of integers
- Given a set, find XOR of the XOR's of all subsets.
- Construct an array from XOR of all elements of array except element at same index
- Minimizing array sum by applying XOR operation on all elements of the array
- Largest subsequence such that all indices and all values are multiples individually
- Find a number such that maximum in array is minimum possible after XOR
- Find a pair (n,r) in an integer array such that value of nPr is maximum
- Find a pair (n,r) in an integer array such that value of nCr is maximum
- Maximum sum of Bitwise XOR of all elements of two equal length subsets
- Number of subsets with same AND, OR and XOR values in an Array
- Find maximum xor of k elements in an array
- Maximum value of XOR among all triplets of an array
- Find elements of array using XOR of consecutive elements
- Check if Sum and XOR of all elements of array is equal
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.