Maximum Strong Pair XOR II Problem
Description
LeetCode Problem 2935.
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 * 10^41 <= nums[i] <= 2^20 - 1
Solution
Approach: Sorting + Binary Trie
Take a pair with x <= y. The condition |x - y| <= min(x, y) becomes y - x <= x, that is y <= 2x.
Sort the numbers and go through them as y in increasing order, keeping a window of earlier numbers x that still satisfy y <= 2x. Because y only grows, the window's left end only moves right: after adding y, drop numbers from the left until the condition holds.
Store the window in a binary trie (bits from high to low) with a count at each node, so removing a number is just decrementing counts. For each y, walk the trie preferring the opposite bit at each level to get the largest XOR with any number in the window, and update the answer. The pair (y, y) is always in the window, so a single number is allowed.
Complexity: O(n log M) time and O(n log M) space, where M is the largest value (here M < 2^20).
class Trie:
__slots__ = ("children", "cnt")
def __init__(self):
self.children: Optional[List[Trie]] = [None, None]
self.cnt = 0
def insert(self, x: int):
node = self
for i in range(20, -1, -1):
v = x >> i & 1
if node.children[v] is None:
node.children[v] = Trie()
node = node.children[v]
node.cnt += 1
def search(self, x: int) -> int:
node = self
ans = 0
for i in range(20, -1, -1):
v = x >> i & 1
if node.children[v ^ 1] and node.children[v ^ 1].cnt:
ans |= 1 << i
node = node.children[v ^ 1]
else:
node = node.children[v]
return ans
def remove(self, x: int):
node = self
for i in range(20, -1, -1):
v = x >> i & 1
node = node.children[v]
node.cnt -= 1
class Solution:
def maximumStrongPairXor(self, nums: List[int]) -> int:
nums.sort()
tree = Trie()
ans = i = 0
for y in nums:
tree.insert(y)
while y > nums[i] * 2:
tree.remove(nums[i])
i += 1
ans = max(ans, tree.search(y))
return ans