Maximum XOR of Two Numbers in an Array

IF
AlgoAxiomStaff Engineers
JSTS
Medium20 mins

Given an integer array nums, return the maximum result of nums[i] XOR nums[j], where 0 <= i <= j < n.

You must solve this in O(n) time using a trie (bitwise trie).

Examples

Example 1:

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

Output: 28

Explanation: The maximum XOR is 5 XOR 25 = 28.

Example 2:

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

Output: 127

Explanation: The maximum XOR is 36 XOR 91 = 127.

Example 3:

Input: nums = [0]

Output: 0

Explanation: With only one number, the maximum XOR is 0 XOR 0 = 0.

Constraints

  • 1 <= nums.length <= 2 * 10^5
  • 0 <= nums[i] <= 2^31 - 1
Source: Trie pattern — AlgoAxiom
JavaScript
Test Case 1
root = [1, 2, 3]
Test Case 2
root = [1, 2, 3, 4, 5]
Idle