Skip to content

Maximum XOR of Two Numbers in an Array

MediumBit Manipulation & Math
Decode it first. Clues, constraints, pattern and plan before any code.

Given an array of non-negative integers nums, return the largest value of nums[i] ^ nums[j] over all pairs with 0 <= i <= j < n (so a number may be paired with itself, giving 0).

nums = [3, 10, 5, 25, 2, 8]  →  28   (5 ^ 25)
nums = [8, 10, 2]            →  10   (8 ^ 2)
nums = [7]                   →  0

Constraints

  • 1 <= nums.length <= 2 * 10^5
  • 0 <= nums[i] <= 2^31 - 1