Maximum XOR of Two Numbers in an Array

Medium

Description

Given an integer array nums, return the maximum XOR of two numbers in the array. Use a trie for O(n) solution.

Examples

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

Maximum XOR is 5 ^ 25 = 28.

Input:nums = [1]
Output:1
Explanation:

Edge case with a single-element array.

Input:nums = [14, 70, 53, 83, 49, 91, 36, 80, 92, 51, 66, 70]
Output:127
Explanation:

The maximum XOR is achieved by 36 ^ 91 = 127. In binary: 36 = 100100, 91 = 1011011. XOR gives 1111111 = 127, which is the maximum possible XOR for 7-bit numbers.

Constraints

  • 1 ≤ nums.length ≤ 2 * 10⁵
  • 0 ≤ nums[i] ≤ 2³¹ - 1

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!