Maximum XOR

Hard
Concept
Code
Loading editor...
Tap Analyze to see visualization
Variables

Run code to see variables

Output

Console output will appear here

Press Space to start to step? all shortcuts

Solution Guide: Maximum XOR

Approach

Compare all pairs of numbers by computing their XOR and tracking the maximum. For each pair (i, j), XOR gives the combined differing bits, and the largest XOR value represents the pair with the most significant differing bits.

Complexity Analysis

Time
O(n^2)
Space
O(1)

Pattern

XOR Pattern

Why It Works

XOR of two numbers produces the largest value when they differ at the highest bit positions, so exhaustive comparison finds the optimal pair.

Updated Feb 2026