Valid K-Unique Subarrays I Problem
Description
LeetCode Problem 4033.
You are given an integer array nums and an integer k.
You are also given a 2D integer array queries, where queries[i] = [l_i, r_i] represents the subarray nums[l_i..r_i].
For each query, the subarray nums[l_i..r_i] is considered valid if:
- It contains exactly
kdistinct numbers, and - The frequency of every number in the subarray is even.
Return a boolean array ans, where ans[i] is true if nums[l_i..r_i] is valid, and false otherwise.
Example 1:
Input: nums = [1,2,2,1], k = 2, queries = [[0,1],[0,3],[1,2]]
Output: [false,true,false]
Explanation:
i [l_i, r_i] Subarray Unique numbers Frequency Validity check
0 [0, 1] [1, 2] {1, 2} → 2 {1: 1, 2: 1} false: Element counts are not even.
1 [0, 3] [1, 2, 2, 1] {1, 2} → 2 {1: 2, 2: 2} true: Exactly k = 2 distinct elements, all appear an even number of times.
2 [1, 2] [2, 2] {2} → 1 {2: 2} false: Number of distinct elements is less than k = 2.
Thus, ans = [false, true, false].
Example 2:
Input: nums = [3,3,3], k = 1, queries = [[1,2],[0,2]]
Output: [true,false]
Explanation:
i [l_i, r_i] Subarray Unique numbers Frequency Validity check
0 [1, 2] [3, 3] {3} → 1 {3: 2} true: Exactly k = 1 distinct element, appears an even number of times.
1 [0, 2] [3, 3, 3] {3} → 1 {3: 3} false: 3 does not appear an even number of times.
Thus, ans = [true, false].
Constraints:
2 <= n == nums.length <= 10^51 <= nums[i] <= 10^51 <= k <= n1 <= queries.length <= 10^5queries[i] == [l_i, r_i]0 <= l_i < r_i <= n - 1
Solution
Approach: Random XOR hash + offline distinct count with a Fenwick tree
Two conditions must hold for a query [l, r].
All frequencies even. Give every value a random 64-bit code and let pre[i] be the XOR of the codes of nums[0..i-1]. A value cancels out of pre[r + 1] ^ pre[l] exactly when it appears an even number of times in the subarray, so all frequencies are even if and only if pre[r + 1] == pre[l] (up to a negligible chance of a hash collision).
Exactly k distinct values. For each index i let prev[i] be the previous index holding the same value. The number of distinct values in [l, r] is the number of indices i in [l, r] with prev[i] < l. Answer the queries offline in order of r: when the right end reaches r, add 1 at position r and subtract 1 at prev[r] in a Fenwick tree, so each value is counted only at its latest position. Then the distinct count of [l, r] is the sum over positions l..r.
A query is valid when both checks pass.
Complexity: time O((n + q) log n), space O(n + q), where q = queries.length.
import random
class Solution:
def validSubarrays(self, nums: List[int], k: int, queries: List[List[int]]) -> List[bool]:
n = len(nums)
rng = random.Random(20260101)
code = {}
# pre[i] = XOR of random codes of nums[0..i-1]; an equal XOR at both ends means every value is even
pre = [0] * (n + 1)
prev = [-1] * n
last = {}
for i, x in enumerate(nums):
if x not in code:
code[x] = rng.getrandbits(64)
pre[i + 1] = pre[i] ^ code[x]
prev[i] = last.get(x, -1)
last[x] = i
by_right = [[] for _ in range(n)]
for qi, (l, r) in enumerate(queries):
by_right[r].append(qi)
tree = [0] * (n + 1)
def add(i, v):
i += 1
while i <= n:
tree[i] += v
i += i & -i
def total(i): # sum of positions 0..i
i += 1
s = 0
while i > 0:
s += tree[i]
i -= i & -i
return s
ans = [False] * len(queries)
for r in range(n):
add(r, 1)
if prev[r] >= 0:
add(prev[r], -1)
for qi in by_right[r]:
l = queries[qi][0]
if pre[r + 1] == pre[l] and total(r) - total(l - 1) == k:
ans[qi] = True
return ans