Given positive integers N, L, and R, we have to find the maximum value of N ⊕ X, where X ∈ [L, R].
Input : N = 7
L = 2
R = 23
Output : 23
Explanation : When X = 16, we get 7 ⊕ 16 = 23 which is the maximum value for all X ∈ [2, 23].
Input : N = 10
L = 5
R = 12
Output : 15
Explanation : When X = 5, we get 10 ⊕ 5 = 15 which is the maximum value for all X ∈ [5, 12].
Brute force approach: We can solve this problem using brute force approach by looping over all integers over the range [L, R] and taking their XOR with N while keeping a record of the maximum result encountered so far. The complexity of this algorithm will be O(R – L), and it is not feasible when the input variables approach high values such as 109.
Efficient approach: Since the XOR of two bits is 1 if and only if they are complementary to each other, we need X to have complementary bits to that of N to have the maximum value. We will iterate from the largest bit (log2(R)th Bit) to the lowest (0th Bit). The following two cases can arise for each bit:
- If the bit is not set, i.e. 0, we will try to set it in X. If setting this bit to 1 results in X exceeding R, then we will not set it.
- If the bit is set, i.e. 1, then we will try to unset it in X. If the current value of X is already greater than or equal to L, then we can safely unset the bit. In the other case, we will check if setting all of the next bits is enough to keep X >= L. If not, then we are required to set the current bit. Observe that setting all the next bits is equivalent to adding (1 << b) – 1, where b is the current bit.
The time complexity of this approach is O(log2(R)).
The output is 23
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.
- Maximum XOR value of maximum and second maximum element among all possible subarrays
- Find the XOR of the elements in the given range [L, R] with the value K for a given set of queries
- Maximum XOR value of a pair from a range
- Maximum XOR with given value in the path from root to given node in the tree
- Number whose sum of XOR with given array range is maximum
- Range Query on array whose each element is XOR of index value and previous element
- Find a value whose XOR with given number is maximum
- Queries to find the maximum Xor value between X and the nodes of a given level of a perfect binary tree
- Given a set, find XOR of the XOR's of all subsets.
- Maximum XOR value in matrix
- Find Maximum XOR value of a sub-array of size k
- Maximum XOR-value of at-most k-elements from 1 to n
- Find the node whose xor with x gives maximum value
- Maximum value of XOR among all triplets of an array
- Find XOR of two number without using XOR operator
- Choose X such that (A xor X) + (B xor X) is minimized
- Segment Tree | Set 3 (XOR of given range)
- Segment Tree | (XOR of a given range )
- Check whether XOR of all numbers in a given range is even or odd
- XOR of all the elements in the given range [L, R]
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.