Advertisement

Maximum XOR of Two Numbers in an Array - LeetCode 421 Solution

Maximum XOR of Two Numbers in an Array - Complete Solution Guide

Maximum XOR of Two Numbers in an Array is LeetCode problem 421, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

Given an integer array nums , return the maximum result of nums[i] XOR nums[j] , where 0 <= i <= j < n . Example 1: Input: nums = [3,10,5,25,2,8] Output: 28 Explanation: The maximum result is 5 XOR 25 = 28. Example 2: Input: nums = [14,70,53,83,49,91,36,80,92,51,66,70] Output: 127 Constraints: 1 <= nums.length <= 2 * 10 5 0 <= nums[i] <= 2 31 - 1

Detailed Explanation

The problem asks us to find the maximum XOR value that can be obtained by XORing any two numbers in a given array of integers. The XOR operation compares the bits of two numbers. If the bits are different, the result is 1; otherwise, the result is 0. The goal is to iterate through all possible pairs of numbers in the array and find the pair that yields the largest XOR value.

Solution Approach

The solution utilizes a greedy approach combined with bit manipulation and a hash set. It starts from the most significant bit (MSB) and iterates down to the least significant bit. In each iteration, it tries to maximize the XOR sum by setting the current bit to 1. It checks if there exist two numbers in the array whose prefixes (up to the current bit) XOR to the desired target, which is `max_xor | (1 << i)`. If such a pair exists, it means setting the current bit to 1 is possible, and the `max_xor` is updated accordingly.

Step-by-Step Algorithm

  1. Step 1: Initialize `max_xor` to 0 and `mask` to 0. `max_xor` will store the maximum XOR sum found so far, and `mask` will be used to extract the prefixes of the numbers.
  2. Step 2: Iterate from the 30th bit (MSB) down to the 0th bit. This loop considers each bit position from the most significant to the least significant.
  3. Step 3: Update the `mask` to include the current bit. This is done by performing a bitwise OR operation: `mask |= (1 << i)`. The mask will isolate the bits from the MSB down to the current bit `i`.
  4. Step 4: Create a set called `prefixes` and store the prefixes of all numbers in the array. The prefix of a number is obtained by performing a bitwise AND operation with the `mask`: `num & mask`.
  5. Step 5: Create a temporary variable `temp` and set the current bit of `max_xor` to 1: `temp = max_xor | (1 << i)`. This represents the potential maximum XOR sum if the current bit is set to 1.
  6. Step 6: Iterate through the `prefixes` set. For each prefix `p`, check if `(temp ^ p)` also exists in the `prefixes` set. If it does, it means there exists another number in the array whose prefix XORed with `p` equals `temp`. This indicates that setting the current bit to 1 is possible, and `max_xor` is updated to `temp`.
  7. Step 7: If a valid XOR is found (step 6), break the inner loop. Otherwise, continue iterating through the `prefixes` set to find a suitable pair.
  8. Step 8: After iterating through all bits, return the `max_xor` value.

Key Insights

  • Insight 1: Building the result bit by bit from the most significant bit (MSB) downwards allows us to greedily maximize the XOR sum.
  • Insight 2: Using a set (or hash table) to store prefixes of numbers enables efficient checking if a complementary prefix exists that would increase the XOR sum.
  • Insight 3: The constraint that each number is at most 2<sup>31</sup> - 1 implies that we need to consider 31 bits (bits 0 to 30) to represent the maximum possible XOR value.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(n)

Topics

This problem involves: Array, Hash Table, Bit Manipulation, Trie.

Companies

Asked at: Goldman Sachs.