Maximum XOR of two numbers in an array

Tries Problems Hard

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

Examples:

Input : nums = [3, 9, 10, 5, 1]

Output : 15

Explanation : The maximum XOR value is 10 XOR 5 => 15.

Input : nums = [26, 49, 30, 15, 69]

Output : 116

Explanation : The maximum XOR value is 69 XOR 49 => 116.

Input : nums = [1, 2, 3, 4, 5, 6]

Constraints

  • 1 <= nums.length <= 105
  • 0 <= nums[i] <= 231 - 1

Hints

  • "Insert all numbers into a binary Trie: Each number is represented as a 32-bit binary string. Each Trie node represents a bit (0 or 1), storing paths for efficient lookups."
  • "While checking each number, traverse the Trie to find the best complement that maximizes the XOR value. The best complement for a bit is its opposite (i.e., 0 prefers 1 and vice versa)."

Company Tags

Activision Blizzard Johnson & Johnson Etsy Lyft Flipkart Stripe Wayfair Splunk Roche Shopify Cerner Snowflake Visa Oracle PwC KPMG Chewy Rockstar Games Seagate Technology HashiCorp Dropbox MongoDB Airbnb IBM PayPal Google Microsoft Amazon Meta Apple Netflix Adobe