Maximum XOR of Two Numbers in an Array

Implement maximumXor

Given an array of non-negative integers, find the largest value that XOR-ing any two of them together can produce. Checking every pair directly works, but there's a faster path: build the answer one bit at a time, starting from the most significant bit. At each step, tentatively assume that bit can be set, then check whether any two values in the array — reduced to just the bits decided so far — actually XOR together to hit that target. Lock the bit in only when a real pair backs it up.

Example 1:

Input: nums = [9,14,3,21]

Output: 28

Example 2:

Input: nums = [8,1,2,12,7,6]

Output: 15

Example 3:

Input: nums = [0]

Output: 0

+ 9 hidden test cases run on Submit.

Constraints:

  • 1 ≤ nums.length ≤ 1000
  • 0 ≤ nums[i] ≤ 10⁹

nums =

[9, 14, 3, 21]