Maximum XOR of two numbers in an array

Tries Problems Hard
  • Fun Fact: XOR operations are commonly used in cryptography, a crucial component of data security in software development
  • In cryptography, XOR is used for its properties of being able to toggle between states
  • By using the same key again, you can decrypt the data
  • Similarly, finding the maximum XOR value can be used to enhance cryptographic algorithms making them more secure and harder to crack
  • This problem might not directly apply to a specific real-world application, but the concepts and algorithms used to solve it are fundamental to many areas in the software industry

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