Given an integer array nums
, return the maximum result of nums[i] XOR nums[j]
, where 0 <= i <= j < n
.
For example:
nums = [3,10,5,25,2,8]
The maximum result is 5 XOR 25 = 28
.nums = [14,70,53,83,49,91,36,80,92,51,66,70]
The maximum result is 127
.Could you provide a solution that efficiently finds the maximum XOR value, considering the constraints that 1 <= nums.length <= 2 * 10^5
and 0 <= nums[i] <= 2^31 - 1
?
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach for this problem is simple: try every possible pair of numbers. For each pair, calculate their XOR value and keep track of the largest XOR value you find.
Here's how the algorithm would work step-by-step:
def maximum_xor_brute_force(numbers):
max_xor_value = 0
# Iterate through each number in the list
for first_number_index in range(len(numbers)):
# Compare the first number against all other numbers
for second_number_index in range(len(numbers)):
# Calculate the XOR value for the pair
current_xor_value = numbers[first_number_index] ^ numbers[second_number_index]
# Update max_xor_value if a larger value is found
if current_xor_value > max_xor_value:
max_xor_value = current_xor_value
return max_xor_value
The goal is to find two numbers in a list that, when combined using a special operation called XOR, give the largest possible result. Instead of checking every pair of numbers, we use a clever trick involving building the largest XOR value bit by bit from left to right.
Here's how the algorithm would work step-by-step:
def find_maximum_xor(numbers):
maximum_xor_result = 0
mask = 0
# Iterate from the most significant bit to the least significant bit
for i in range(31, -1, -1):
# Update the mask to include the current bit
mask |= (1 << i)
prefixes = set()
# Store all prefixes of the numbers with the current mask
for number in numbers:
prefixes.add(number & mask)
# Try to find a number that, when XORed with the current prefix,
# results in the current maximum XOR with the current bit set to 1
potential_maximum_xor = maximum_xor_result | (1 << i)
for prefix in prefixes:
if (potential_maximum_xor ^ prefix) in prefixes:
# If such a number exists, update the maximum XOR
maximum_xor_result = potential_maximum_xor
break
return maximum_xor_result
Case | How to Handle |
---|---|
Empty input array | Return 0 since no XOR operation is possible. |
Input array with a single element | Return 0 since XOR requires two elements. |
Array contains only zeros | Return 0 as 0 XOR 0 is 0. |
Array with maximum integer values (e.g., all 2147483647) | The XOR operation will produce a result within the integer range; handle potential integer overflow by ensuring use of unsigned integers if needed, or by using a larger data type like 'long' if language specific integer max values are small. |
Array with minimum integer values (e.g., all -2147483648) | The problem specifies non-negative integers, so this should not occur, but the solution should define what an error state is and gracefully handle it. |
Large input array (e.g., 100,000 elements) | The time complexity should be considered; a naive O(n^2) solution will likely time out, therefore Trie or hash-based approaches are favored for performance. |
Array with elements that result in XOR exceeding maximum integer value (if negative numbers were allowed). | Since the input is non-negative, the XOR will always be non-negative and less than twice the maximum value of any element; this scenario is irrelevant with the non-negative constraint, however, the chosen data type for storing the XOR result needs to be large enough to handle the maximum potential value. |
Array containing duplicate numbers. | Duplicate numbers do not affect the correctness of the trie or greedy algorithm as they are treated as distinct elements during the XOR operations. |