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
- Choose k array elements such that difference of maximum and minimum is minimized
- Number of ways to choose an integer such that there are exactly K elements greater than it in the given array
- Select K elements from an array whose maximum value is minimized
- Pair formation such that maximum pair sum is minimized
- Choose n elements such that their mean is maximum
- 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
- Number of ways to choose elements from the array such that their average is K
- Choose two elements from the given array such that their sum is not present in any of the arrays
- Find the original Array using XOR values of all adjacent elements
- Partition the array into two odd length groups with minimized absolute difference between their median
- Divide a sorted array in K parts with sum of difference of max and min minimized in each part
- Choose X elements from A and Y elements from B which satisfy the given condition
- 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.
- Count elements such that there are exactly X elements with values greater than or equal to X
- Largest subsequence such that all indices and all values are multiples individually
- Find a pair (n,r) in an integer array such that value of nPr is maximum
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.