Valid K-Unique Subarrays II Problem
Description
LeetCode Problem 4042.
You are given an integer array nums of length n and an integer k.
You are also given integers l0 and r0, which define the first query, and an integer q, representing the total number of queries to process.
A subarray nums[l_i..r_i] is considered valid if:
- It contains exactly
kdistinct numbers, and - Every distinct number in it occurs an even number of times.
For query 0, set l_0 = l0 and r_0 = r0.
Let ans_i denote the result of the i^th query, where ans_i = 1 if nums[l_i..r_i] is valid, and ans_i = 0 otherwise.
For each i > 0, generate the next query as follows:
- If
ans_(i-1) = 1, setg_(i-1) = l_(i-1) + r_(i-1). Otherwise, setg_(i-1) = r_(i-1) - l_(i-1). - Compute
l_i = (l_(i-1) XOR g_(i-1)) % nandr_i = (r_(i-1) XOR g_(i-1)) % n. - If
l_i > r_i, swap them.
Return a boolean array ans, where ans[i] is true if ans_i = 1, and false otherwise.
Example 1:
Input: nums = [1,2,2,1], k = 2, l0 = 1, r0 = 2, q = 2
Output: [false,true]
Explanation:
i [l_i, r_i] Subarray Distinct numbers Counts Validity check ans[i] [l_(i+1), r_(i+1)]
0 [1, 2] [2, 2] {2} → 1 {2:2} false: The subarray contains fewer than k distinct numbers. ans_0 = 0 g_0 = 2 - 1 = 1 l_1 = (1 XOR 1) % 4 = 0 r_1 = (2 XOR 1) % 4 = 3
1 [0, 3] [1, 2, 2, 1] {1,2} → 2 {1:2,2:2} true: The subarray contains exactly k distinct numbers, each occurring an even number of times. ans_1 = 1 -
Thus, ans = [false, true].
Example 2:
Input: nums = [1,2,3,3,4], k = 1, l0 = 2, r0 = 3, q = 2
Output: [true,false]
Explanation:
i [l_i, r_i] Subarray Distinct numbers Counts Validity check ans[i] [l_(i+1), r_(i+1)]
0 [2, 3] [3, 3] {3} → 1 {3:2} true: The subarray contains exactly k distinct numbers, each occurring an even number of times. ans_0 = 1 g_0 = 2 + 3 = 5 l_1 = (2 XOR 5) % 5 = 7 % 5 = 2 r_1 = (3 XOR 5) % 5 = 6 % 5 = 1 Since l_1 > r_1, swap them to obtain [l_1, r_1] = [1, 2].
1 [1, 2] [2, 3] {2,3} → 2 {2:1,3:1} false: The subarray contains 2 distinct numbers instead of exactly k = 1. ans_1 = 0 -
Thus, ans = [true, false].
Constraints:
2 <= n == nums.length <= 5 × 10^51 <= nums[i] <= 5 × 10^51 <= k <= n0 <= l_0 < r_0 <= n - 11 <= q <= 5 × 10^5
Solution
Approach: Prefix Hash and Wavelet Matrix
A query is valid when two conditions hold, and each can be checked in about O(log n) time even though the queries depend on earlier answers.
All counts even. Give every value a random 64-bit hash (two 32-bit halves) and let P[i] be the XOR of the hashes of nums[0..i-1]. A value that appears an even number of times cancels itself, so every count in nums[l..r] is even exactly when P[l] == P[r + 1]. (A collision is possible in theory but has negligible probability.)
Exactly k distinct values. Let prev[i] be the previous index holding the same value (or -1). The values in nums[l..r] are counted by their first occurrence there, that is, by the indices i in [l, r] with prev[i] < l. Every i < l also satisfies prev[i] < l, so
distinct(l, r) = (number of i <= r with prev[i] < l) - l.
This is an online "count smaller values in a prefix" question, which a wavelet matrix over prev + 1 answers in O(log n): at each of the log n levels, keep the number of zero bits in every prefix, and walk down the bits of l + 1.
Each query first checks the cheap hash condition and only then counts distinct values. The next query is generated from the answer as described in the statement.
Complexity: O((n + q) log n) time and O(n log n) space.
class Solution:
def validSubarrays(self, nums: List[int], k: int, l0: int, r0: int, q: int) -> List[bool]:
n = len(nums)
M = 0xFFFFFFFF
def fmix(h):
h ^= h >> 16
h = (h * 0x85EBCA6B) & M
h ^= h >> 13
h = (h * 0xC2B2AE35) & M
h ^= h >> 16
return h
# prefix XOR of random hashes: equal at l and r + 1 means every value has an even count
p1 = [0] * (n + 1)
p2 = [0] * (n + 1)
# prev[i] + 1: the previous index holding the same value, plus one (0 if none)
prev = [0] * n
last = {}
for i, x in enumerate(nums):
h1 = fmix((x + 0x9E3779B9) & M)
h2 = fmix(fmix(x) ^ 0x7F4A7C15)
p1[i + 1] = p1[i] ^ h1
p2[i + 1] = p2[i] ^ h2
prev[i] = last.get(x, -1) + 1
last[x] = i
# wavelet matrix over prev, to count i < r + 1 with prev[i] + 1 < l + 1
L = n.bit_length()
zeros = []
nz = []
cur = prev
for lvl in range(L):
b = L - 1 - lvl
z = [0] * (n + 1)
zs, os = [], []
for i, v in enumerate(cur):
if (v >> b) & 1:
os.append(v)
z[i + 1] = z[i]
else:
zs.append(v)
z[i + 1] = z[i] + 1
zeros.append(z)
nz.append(len(zs))
cur = zs + os
def count_less(hi, x):
lo = 0
res = 0
for lvl in range(L):
z = zeros[lvl]
zl, zh = z[lo], z[hi]
if (x >> (L - 1 - lvl)) & 1:
res += zh - zl
lo = nz[lvl] + lo - zl
hi = nz[lvl] + hi - zh
else:
lo, hi = zl, zh
return res
ans = []
l, r = l0, r0
for _ in range(q):
ok = False
if p1[l] == p1[r + 1] and p2[l] == p2[r + 1]:
ok = count_less(r + 1, l + 1) - l == k
ans.append(ok)
g = l + r if ok else r - l
l, r = (l ^ g) % n, (r ^ g) % n
if l > r:
l, r = r, l
return ans