Problem 481
Maximum XOR of Two Numbers
Medium · 20 pts · Array, Bit Manipulation, Trie
Problem
Find the largest value of A[i] XOR A[j] over all pairs of positions i <= j (a number XOR itself is 0, so a single element gives 0). Checking every pair is O(N²). A binary trie of the numbers' bits does it in O(N · 31): for each number, walk the trie from the highest bit, preferring at each step the child with the opposite bit.
Input
First line: N. Second line: N non-negative integers.
Output
Print the maximum XOR of two elements.
Constraints
1 <= N <= 100000; 0 <= A[i] <= 2147483647
Example input
6 3 10 5 25 2 8
Example output
28
Explanation
In 3 10 5 25 2 8, the pair 5 (00101) and 25 (11001) gives 5 XOR 25 = 28 (11100), the largest possible.