Maximum XOR of Two Numbers in an Array

Tries & Bit Manipulation, problem 7 of 8

Maximum XOR of Two Numbers in an Array

Medium

LC #421

triebit manipulationgreedy

Not attempted yet

Return the largest value of nums[i] ^ nums[j] over all 0 ≤ i ≤ j < n (a number paired with itself gives 0).

Checking every pair is O(n²). Can you do better?

Example 1

Input: nums = [3, 10, 5, 25, 2, 8]
Output: 28

5 ^ 25 = 00101 ^ 11001 = 11100 = 28

Example 2

Input: nums = [8, 1, 2]
Output: 10

8 ^ 2 = 1000 ^ 0010 = 1010

Example 3

Input: nums = [7]
Output: 0

Constraints

  • 1 ≤ len(nums) ≤ 2 · 10⁴
  • 0 ≤ nums[i] ≤ 2³¹ - 1

Python

Loading draft…

Test results

9 tests available

No results yet

Run tests your code against the examples; Submit runs the hidden tests too.

3 examples, 6 hidden

Run examples, then submit all tests.