Maximum XOR Pairs

mediumtriebits-xor operation

You are given an array of $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ that satisfy

Your task is to compute $$$$$$ \max_{1 \le i < j \le n}\; (a_i \oplus a_j), $$$$$$ where "$$$\oplus$$$" denotes the bitwise XOR operation.

Input Format:

The first line contains one integer $$$n$$$ $$$(1 \le n \le 2 \times 10^{5}$$$).

The second line contains $$$n$$$ space-separated integers $$$a_1 \ldots a_n$$$ ($$$0 \le a_i < 2^{31}$$$).

Output Format:

Print a single integer — the maximum value of $$$a_i \oplus a_j$$$ over all valid pairs.

Examples:

Example 1:

Input:

5
2 3 6 8 9

Output:

15

Example 2:

Input:

6
3 10 5 25 2 8

Output:

28

Example 3:

Input:

12
14 70 53 83 49 91 36 80 92 51 66 70

Output:

127

Note:

In the first testcase, picking the elements $$$6$$$ and $$$9$$$ yields $$$6 \oplus 9 = 15$$$, which is the largest $$$XOR$$$ attainable from any pair in the array.

In the second testcase, choosing the elements $$$5$$$ and $$$25$$$ gives $$$5 \oplus 25 = 28$$$, which is the largest possible $$$XOR$$$ out of all possible pairs.

Code

Loading Editor...