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 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
- Choose X such that (A xor X) + (B xor X) is minimized
- Choose n elements such that their mean is maximum
- Choose two elements from the given array such that their sum is not present in any of the arrays
- Number of ways to choose elements from the array such that their average is K
- 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
- Find single in an array of 2n+1 integer elements
- Print All Distinct Elements of a given integer array
- Maximum height of triangular arrangement of array values
- Elements of Array which can be expressed as power of some integer to given exponent K
- Maximum number of unique values in the array after performing given operations
- Minimize the difference between the maximum and minimum values of the modified array
- Find the original Array using XOR values of all adjacent elements
- Queries for counts of array elements with values in given range
- Sum of elements in 1st array such that number of elements less than or equal to them in 2nd array is maximum
- Choose X elements from A and Y elements from B which satisfy the given condition
- Maximize occurrences of values between L and R on sequential addition of Array elements with modulo H
- Divide a sorted array in K parts with sum of difference of max and min minimized in each part
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.