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 = [500,520,2500,3000] Output: 1020 Explanation: There are 6 strong pairs in the array nums: (500, 500), (500, 520), (520, 520), (2500, 2500), (2500, 3000) and (3000, 3000). The maximum XOR possible from these pairs is 500 XOR 520 = 1020 since the only other non-zero XOR value is 2500 XOR 3000 = 636.
Constraints:
1 <= nums.length <= 5 * 1041 <= nums[i] <= 220 - 1Problem Overview: You are given an array of integers and must find the maximum XOR value among all strong pairs. A pair (x, y) is strong if |x - y| ≤ min(x, y). The goal is to efficiently evaluate valid pairs and compute the maximum x ^ y without checking every combination.
Approach 1: Sort and Two Pointer Approach with Trie (O(n log M) time, O(n) space)
Sort the array first. When the array is sorted and x ≤ y, the strong pair condition becomes y - x ≤ x, which simplifies to y ≤ 2x. This property lets you maintain a valid range using a sliding window. Move the right pointer through the array and keep the left pointer adjusted so that all values inside the window satisfy the strong pair constraint.
Within the window, the goal becomes finding the maximum XOR between the current value and any earlier value. A binary Trie stores the bit representation of numbers currently in the window. Each insertion and query takes O(log M), where M is the maximum value in the array (number of bits). For every new element, query the trie for the best XOR partner, update the answer, then insert the number into the trie.
The two-pointer window guarantees that only valid strong pairs remain in the trie. When the left pointer moves forward, remove outdated elements from the trie. Sorting costs O(n log n), and each element is inserted and removed once, leading to overall O(n log M) operations.
Approach 2: Bit Manipulation Optimization (O(n log M) time, O(n) space)
This method builds the XOR result greedily from the most significant bit to the least significant bit using bit manipulation. Instead of a full trie structure, maintain prefix sets of numbers while iteratively testing whether a candidate bit can be set in the answer.
For each bit position, assume the current bit of the answer can be 1 and check if two prefixes exist that produce this XOR. While performing this check, enforce the strong pair condition using the same sorted order and window constraint y ≤ 2x. Hash sets store prefixes efficiently and allow constant-time lookups.
This approach reduces structural overhead compared to an explicit trie while still leveraging bitwise properties of XOR. The complexity remains O(n log M) because each bit position is evaluated across the array.
Recommended for interviews: The sorted two-pointer + trie solution is the most common explanation in interviews. It clearly separates the constraints: sorting enforces the strong pair condition via a sliding window, and the trie efficiently computes maximum XOR. Interviewers expect you to derive the y ≤ 2x transformation and combine it with a bitwise structure. The bit manipulation variant demonstrates deeper understanding of XOR optimization techniques.
By sorting the array, the problem of finding pairs becomes simpler. The key observation is that if two numbers are close enough in value, i.e., their difference is less than or equal to their minimum value, they form a strong pair. Sorting the array ensures that once a valid strong pair is found, subsequent numbers are only larger, making them incoming candidates for a strong pair condition. By employing a two-pointer technique, we efficiently navigate through the pairs to find the maximum XOR.
The function first sorts the array, making it easier to identify valid 'strong' pairs using the two-pointer method. After sorting, for each potential starting number, it checks whether subsequent numbers satisfy the strong pair condition. If so, XORs are computed, and the maximum is updated. The process efficiently navigates through possible pairs by breaking inner loops early when conditions aren't met.
Time Complexity: O(n^2) due to the nested loop structure, even though the sorting itself is O(n log n).
Space Complexity: O(1) as no additional data structures proportional to input size are used.
This approach focuses on leveraging properties of bits to directly veer toward results that maximize XOR values. By storing numbers as nodes in a trie structure (radix tree) based on their bit patterns, it's possible to find potential maximum XOR pairs by aligning complementary bits.
The solution constructs a trie with each bit of the numbers. During querying for maximum XOR, it explores complimentary branches of trie nodes to achieve maximum XOR. The trie's essence is to facilitate efficient searching of numbers by their binary representation, which is highly conducive for XOR maximization.
Time Complexity: O(n) for insertion and querying in trie.
Space Complexity: O(n * bit_length), essentially storing bits of each number.
Observing the inequality |x - y| leq min(x, y), which involves absolute value and minimum value, we can assume x leq y, then we have y - x leq x, that is, y leq 2x. We can enumerate y from small to large, then x must satisfy the inequality y leq 2x.
Therefore, we sort the array nums, and then enumerate y from small to large. We use two pointers to maintain a window so that the elements x in the window satisfy the inequality y leq 2x. We can use a binary trie to maintain the elements in the window, so we can find the maximum XOR value in the window in O(1) time. Each time we add y to the trie, and remove the elements at the left end of the window that do not satisfy the inequality, this can ensure that the elements in the window satisfy the inequality y leq 2x. Then query the maximum XOR value from the trie and update the answer.
The time complexity is O(n times log M), and the space complexity is O(n times log M). Here, n is the length of the array nums, and M is the maximum value in the array nums. In this problem, M = 2^{20}.
Python
Java
C++
Go
TypeScript
| Approach | Complexity |
|---|---|
| Sort and Two Pointer Approach | Time Complexity: O(n^2) due to the nested loop structure, even though the sorting itself is O(n log n). |
| Bit Manipulation Optimization Approach | Time Complexity: O(n) for insertion and querying in trie. |
| Sorting + Binary Trie | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Sort + Two Pointers + Trie | O(n log M) | O(n) | Best general solution. Efficiently maintains valid strong pairs and computes maximum XOR using a trie. |
| Bit Manipulation Prefix Optimization | O(n log M) | O(n) | Useful when optimizing XOR computation without an explicit trie structure. |
Leetcode Weekly contest 371 - Hard - Maximum Strong Pair XOR II • Prakhar Agrawal • 1,894 views views
Watch 6 more video solutions →Practice Maximum Strong Pair XOR II with our built-in code editor and test cases.
Practice on FleetCode