Maximum XOR of Two Numbers

Practice this coding question on Coding Gurukul.

Problem statement

A diagnostics tool compares nonnegative device signatures using bitwise XOR. It needs the pair whose differing bits produce the greatest numerical result, without checking every possible pair in a large batch. Your task Find the maximum value of nums[i] XOR nums[j] over all valid indices i and j. Important details Indices may be equal, so a one-element array returns zero. XOR is a bitwise operation, not exponentiation. Use a binary trie or a greedy prefix-set approach that examines a fixed number of bit positions per value. Worked example For [3, 10, 5, 25, 2, 8], values 5 and 25 give 28 under XOR. No pair gives a larger value, so print 28.

Input format

The first line contains n. The next line contains n space-separated integers; for n = 0 this line may be empty.

Output format

Print the maximum XOR value.

Constraints

1 <= n <= 200000; 0 <= values <= 2147483647.

Sample input

6
3 10 5 25 2 8

Sample output

28

Sign in to run or submit a solution in C++, Java or Python.