Given a range [L, R], we need to find two integers in this range such that their XOR is maximum among all possible choices of two integers. More Formally,
given [L, R], find max (A ^ B) where L <= A, B
Input : L = 8 R = 20 Output : 31 31 is XOR of 15 and 16. Input : L = 1 R = 3 Output : 3
A simple solution is to generate all pairs, find their XOR values and finally return the maximum XOR value.
An efficient solution is to consider pattern of binary values from L to R. We can see that first bit from L to R either changes from 0 to 1 or it stays 1 i.e. if we take the XOR of any two numbers for maximum value their first bit will be fixed which will be same as first bit of XOR of L and R itself.
After observing the technique to get first bit, we can see that if we XOR L and R, the most significant bit of this XOR will tell us the maximum value we can achieve i.e. let XOR of L and R is 1xxx where x can be 0 or 1 then maximum XOR value we can get is 1111 because from L to R we have all possible combination of xxx and it is always possible to choose these bits in such a way from two numbers such that their XOR becomes all 1. It is explained below with some examples,
Examples 1: L = 8 R = 20 L ^ R = (01000) ^ (10100) = (11100) Now as L ^ R is of form (1xxxx) we can get maximum XOR as (11111) by choosing A and B as 15 and 16 (01111 and 10000) Examples 2: L = 16 R = 20 L ^ R = (10000) ^ (10100) = (00100) Now as L ^ R is of form (1xx) we can get maximum xor as (111) by choosing A and B as 19 and 20 (10011 and 10100)
So the solution of this problem depends on the value of (L ^ R) only. We will calculate the L^R value first and then from most significant bit of this value, we will add all 1s to get the final result.
This article is contributed by Utkarsh Trivedi. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
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
- Value in a given range with maximum XOR
- Minimum XOR Value Pair
- Range Query on array whose each element is XOR of index value and previous element
- Find the XOR of the elements in the given range [L, R] with the value K for a given set of queries
- Number whose sum of XOR with given array range is maximum
- Maximum Bitwise AND pair from given range
- Maximum Bitwise OR pair from a range
- Maximum Bitwise AND pair (X, Y) from given range such that X and Y can be same
- 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 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
- Maximum XOR with given value in the path from root to given node in the tree
- Maximum AND value of a pair in an array
- Print pair with maximum AND value in an array
- Maximum OR value of a pair in an Array | Set 2