You are given a 0-indexed integer array nums
. A pair of integers x
and y
is called a strong pair if it satisfies the condition:
|x - y| <= min(x, y)
You need to select two integers from nums
such that they form a strong pair and their bitwise XOR
is the maximum among all strong pairs in the array.
Return the maximum XOR
value out of all possible strong pairs in the array nums
.
Note that you can pick the same integer twice to form a pair.
Example 1:
Input: nums = [1,2,3,4,5]
Output: 7
Explanation: There are 11 strong pairs in the array nums
: (1, 1), (1, 2), (2, 2), (2, 3), (2, 4), (3, 3), (3, 4), (3, 5), (4, 4), (4, 5) and (5, 5).
The maximum XOR possible from these pairs is 3 XOR 4 = 7.
Example 2:
Input: nums = [10,100]
Output: 0
Explanation: There are 2 strong pairs in the array nums
: (10, 10) and (100, 100).
The maximum XOR possible from these pairs is 10 XOR 10 = 0 since the pair (100, 100) also gives 100 XOR 100 = 0.
Example 3:
Input: nums = [5,6,25,30]
Output: 7
Explanation: There are 6 strong pairs in the array nums
: (5, 5), (5, 6), (6, 6), (25, 25), (25, 30) and (30, 30).
The maximum XOR possible from these pairs is 25 XOR 30 = 7 since the only other non-zero XOR value is 5 XOR 6 = 3.
Constraints:
1 <= nums.length <= 50
1 <= nums[i] <= 100
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 method for this problem involves checking every single possible pair of numbers from the given set. For each pair, we determine if it qualifies as a 'strong pair' based on a distance requirement and if so, calculate a score for the pair.
Here's how the algorithm would work step-by-step:
def maximum_strong_pair_xor_i(numbers):
maximum_xor = 0
# Iterate through each number
for first_number in numbers:
for second_number in numbers:
# Check if the pair is a strong pair
if abs(first_number - second_number) <= min(first_number, second_number):
# Calculate XOR value
current_xor = first_number ^ second_number
#Update maximum_xor if necessary
if current_xor > maximum_xor:
maximum_xor = current_xor
return maximum_xor
The key to efficiently finding the maximum XOR value of a strong pair is to consider pairs that are relatively close to each other in value. Instead of comparing every single pair, we focus on examining numbers that are likely to form a strong pair and potentially maximize the XOR result.
Here's how the algorithm would work step-by-step:
def maximum_strong_pair_xor(numbers):
numbers.sort()
max_xor_value = 0
for i in range(len(numbers)):
for j in range(i, len(numbers)):
# Only consider pairs where one number is at most twice the other.
if numbers[i] <= 2 * numbers[j] and numbers[j] <= 2 * numbers[i]:
current_xor_value = numbers[i] ^ numbers[j]
# Track the largest XOR value encountered.
max_xor_value = max(max_xor_value, current_xor_value)
return max_xor_value
Case | How to Handle |
---|---|
Empty array nums | Return 0 since no strong pair exists. |
nums array with only one element | Return 0 since a pair requires two elements. |
All numbers in nums are 0 | Return 0, as 0 XOR 0 is 0, which is the maximum possible. |
Large nums array with all identical values | Check if the first two elements form a valid pair; if so, their XOR value is the answer. |
Large nums array where one number dominates and is much larger than others | Iterating through all possible pairs will find the best strong pair XOR. |
nums array contains very large integers that could potentially cause integer overflow in XOR operation | XOR operation doesn't cause overflow, but the int data type must be sufficient for all possible XOR results; if the input numbers can result in numbers outside of Int, we should switch to using long. |
nums array with duplicates | The definition of strong pair disallows the case of selecting the same index twice; our nested loops take care of it. |
All possible pairs result in 0 XOR | The algorithm will return 0 in such cases because 0 is the initial value, so no special handling is required. |