Maximum XOR Pair

medium bit-manipulation trees greedy 1000 ms · 256 MB

Maximum XOR Pair

You are given n non-negative integers a_1, a_2, ..., a_n. Choose two different indices i and j and compute a_i XOR a_j, where XOR is the bitwise exclusive or. Find the largest value this can take.

The two indices must be different, but the values at them may be equal.

Input

The first line contains an integer n (2 ≤ n ≤ 2·10^5).

The second line contains n integers a_1, a_2, ..., a_n (0 ≤ a_i < 2^30).

Output

Print one integer: the maximum of a_i XOR a_j over all pairs of indices i ≠ j.

Notes

In the first example, 5 XOR 25 = 00101 XOR 11001 = 11100 = 28 in binary, and no other pair does better.

In the second example, the largest number is 12, but pairing it with anything gives at most 8; the best pair is 11 XOR 4 = 15.

Example 1

Input

6
3 10 5 25 2 8

Output

28

Example 2

Input

3
12 11 4

Output

15

Hints · reveal one at a time if you're stuck

Hint 1

XOR results compare like binary numbers: one higher bit outweighs all lower bits together. Note that the best pair need not contain the largest number (see the second example), so think bit by bit, from the top.

Hint 2

Fix one number x. Its best partner is the one that differs from x in the highest possible bit, then, among those, in the next highest bit, and so on. How could you store the other numbers so this greedy search takes one step per bit?

Hint 3

Store the numbers as 30-bit strings, most significant bit first, in a binary trie. Walking down it lets you prefer the opposite bit at each level.

report an issue Prefer your own editor? sooks prep 25 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.