Given an array of integers ,You have to find two elements whose XOR is maximum.
There is naive approach –just by picking each element and xoring with other elements and then comparing the results to find the pair.
Other than this ,Is there any efficient algorithm?
I think I have a
O(n lg U)algorithm for this, whereUis the largest number. The idea is similar to user949300‘s, but with a bit more detail.The intuition is as follows. When you’re XORing two numbers together, to get the maximum value, you want to have a
1at the highest possible position, and then of the pairings that have a1at this position, you want a pairing with a1at the next possible highest position, etc.So the algorithm is as follows. Begin by finding the highest
1bit anywhere in the numbers (you can do this in timeO(n lg U)by doingO(lg U)work per each of thennumbers). Now, split the array into two pieces – one of the numbers that have a1in that bit and the group with0in that bit. Any optimal solution must combine a number with a1in the first spot with a number with a0in that spot, since that would put a1bit as high as possible. Any other pairing has a0there.Now, recursively, we want to find the pairing of numbers from the
1and0group that has the highest1in them. To do this, of these two groups, split them into four groups:11100100If there are any numbers in the
11and00group or in the10and01groups, their XOR would be ideal (starting with11). Consequently, if either of those pairs of groups isn’t empty, recursively compute the ideal solution from those groups, then return the maximum of those subproblem solutions. Otherwise, if both groups are empty, this means that all the numbers must have the same digit in their second position. Consequently, the optimal XOR of a number starting with1and a number starting with0will end up having the next second bit cancel out, so we should just look at the third bit.This gives the following recursive algorithm that, starting with the two groups of numbers partitioned by their MSB, gives the answer:
1and group0and a bit indexi:1group and the (unique) number in the0group.11,10,01, and00from those groups.11and group00are nonempty, recursively find the maximum XOR of those two groups starting at biti + 1.10and group01are nonempty, recursively find the maximum XOR of those two groups, starting at biti + 1.i, so return the maximum pair found by looking at biti + 1on groups1and0.To start off the algorithm, you can actually just partition the numbers from the initial group into two groups – numbers with MSB
1and numbers with MSB0. You then fire off a recursive call to the above algorithm with the two groups of numbers.As an example, consider the numbers
5 1 4 3 0 2. These have representationsWe begin by splitting them into the
1group and the0group:Now, we apply the above algorithm. We split this into groups
11,10,01, and00:Now, we can’t pair any
11elements with00elements, so we just recurse on the10and01groups. This means we construct the100,101,010, and011groups:Now that we’re down to buckets with just one element in them, we can just check the pairs
101and010(which gives111) and100and011(which gives111). Either option works here, so we get that the optimal answer is7.Let’s think about the running time of this algorithm. Notice that the maximum recursion depth is
O(lg U), since there are onlyO(log U)bits in the numbers. At each level in the tree, each number appears in exactly one recursive call, and each of the recursive calls does work proportional to the total number of numbers in the0and1groups, because we need to distribute them by their bits. Consequently, there areO(log U)levels in the recursion tree, and each level doesO(n)work, giving a total work ofO(n log U).Hope this helps! This was an awesome problem!