Skip to main content
Back to problems
Codeforces
Medium
Arrays
Bit Manipulation
Stacks
Google
Maximum Xor Secondary

Find the maximum value of aiaja_i \oplus a_j over pairs of elements where the chosen pair must satisfy the constraints of the original problem’s ordering rule.

Acceptance 0%
Problem Statement

You are given an array of integers. Consider all valid pairs of positions that can be formed according to the problem’s ordering constraint, and compute the bitwise XOR of the two values in each valid pair.

Your task is to determine the maximum XOR value among all valid pairs.

The key challenge is that checking every pair directly is too slow for large arrays, so the solution must exploit structure in the way valid pairs are formed.

Input Format

Input

  • The first line contains an integer nn — the number of array elements.
  • The second line contains nn integers a1,a2,,ana_1, a_2, \dots, a_n.

Interpretation

  • All values are treated as integers suitable for bitwise XOR.

Output Format

Output

  • Print a single integer: the maximum XOR value among all valid pairs.

Constraints

  • 2n2 \le n
  • The array size is large enough that an O(n2)O(n^2) approach is not intended.
  • Values fit in standard 32-bit signed integer ranges unless otherwise stated by the platform.
Examples
Sample cases returned by the problem API.

Example 1

Input

3
1 2 3

Output

1

Explanation

Among the valid candidate pairs considered by the ordering rule, the best XOR value is 23=12 \oplus 3 = 1.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.