Maximum Subarray XOR with Bounded Range Problem
Description
LeetCode Problem 3845.
You are given a non-negative integer array nums and an integer k.
You must select a subarray of nums such that the difference between its maximum and minimum elements is at most k. The value of this subarray is the bitwise XOR of all elements in the subarray.
Return an integer denoting the maximum possible value of the selected subarray.
Example 1:
Input: nums = [5,4,5,6], k = 2
Output: 7
Explanation:
- Select the subarray [5, 4, 5, 6].
- The difference between its maximum and minimum elements is 6 - 4 = 2 <= k.
- The value is 4 XOR 5 XOR 6 = 7.
Example 2:
Input: nums = [5,4,5,6], k = 1
Output: 6
Explanation:
- Select the subarray [5, 4, 5, 6].
- The difference between its maximum and minimum elements is 6 - 6 = 0 <= k.
- The value is 6.
Constraints:
1 <= nums.length <= 4 * 10^40 <= nums[i] < 2^150 <= k < 2^15
Solution
Approach: Prefix XOR, Sliding Window and Binary Trie
The XOR of a subarray equals the XOR of two prefix XORs: prefix[right + 1] ^ prefix[left]. So for each right end we want the legal left end whose prefix XOR gives the largest result.
The legal left ends form a window: as right grows, the smallest valid left only moves forward. We keep the window's maximum and minimum with two monotonic deques, and shrink from the left while max - min > k.
The prefix XORs of the current window live in a binary trie over 15 bits, with a counter on every node so that a prefix can be removed when left advances. To query, walk the trie from the top bit down and always prefer the child with the opposite bit when it still has a positive counter; that greedily maximizes the XOR.
Each prefix is inserted once and removed at most once, so the work is proportional to n times the bit width.
Complexity: O(n log M) time and O(n log M) space, where M = 215 is the value range.
from collections import deque
class TrieNode:
__slots__ = ("children", "count")
def __init__(self):
self.children = [None, None]
self.count = 0
class Solution:
def maxXor(self, nums: List[int], k: int) -> int:
root = TrieNode()
def update(value: int, delta: int) -> None:
cur = root
for bit in range(14, -1, -1):
b = (value >> bit) & 1
if cur.children[b] is None:
cur.children[b] = TrieNode()
cur = cur.children[b]
cur.count += delta
def get_max_xor(value: int) -> int:
cur = root
ans = 0
for bit in range(14, -1, -1):
b = (value >> bit) & 1
opp = 1 - b
if cur.children[opp] is not None and cur.children[opp].count > 0:
ans |= 1 << bit
cur = cur.children[opp]
else:
cur = cur.children[b]
return ans
n = len(nums)
prefix = [0] * (n + 1)
for i, x in enumerate(nums):
prefix[i + 1] = prefix[i] ^ x
maxq, minq = deque(), deque()
left = 0
ans = 0
update(prefix[0], 1)
for right, x in enumerate(nums):
while maxq and nums[maxq[-1]] <= x:
maxq.pop()
while minq and nums[minq[-1]] >= x:
minq.pop()
maxq.append(right)
minq.append(right)
while nums[maxq[0]] - nums[minq[0]] > k:
if maxq[0] == left:
maxq.popleft()
if minq[0] == left:
minq.popleft()
update(prefix[left], -1)
left += 1
ans = max(ans, get_max_xor(prefix[right + 1]))
update(prefix[right + 1], 1)
return ans