4042. Valid K-Unique Subarrays II π
Description
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[li..ri] 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 l0 = l0 and r0 = r0.
Let ansi denote the result of the ith query, where ansi = 1 if nums[li..ri] is valid, and ansi = 0 otherwise.
For each i > 0, generate the next query as follows:
- If
ansi-1 = 1, setgi-1 = li-1 + ri-1. Otherwise, setgi-1 = ri-1 - li-1. - Compute
li = (li-1 XOR gi-1) % nandri = (ri-1 XOR gi-1) % n. - If
li > ri, swap them.
Return a boolean array ans, where ans[i] is true if ansi = 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 | [li, ri] | Subarray | Distinct numbers | Counts | Validity check | ans[i] | [li+1, ri+1] |
|---|---|---|---|---|---|---|---|
| 0 | [1, 2] | [2, 2] | {2} β 1 | {2:2} | false: The subarray contains fewer than k distinct numbers. | ans0 = 0 | g0 = 2 - 1 = 1 |
| 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. | ans1 = 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 | [li, ri] | Subarray | Distinct numbers | Counts | Validity check | ans[i] | [li+1, ri+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. | ans0 = 1 | g0 = 2 + 3 = 5Since l1 > r1, swap them to obtain [l1, r1] = [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. | ans1 = 0 | - |
Thus, ans = [true, false].
Constraints:
2 <= n == nums.length <= 5 Γ 1051 <= nums[i] <= 5 Γ 1051 <= k <= n0 <= l0 < r0 <= n - 11 <= q <= 5 Γ 105
Solutions
Solution 1
1 | |
1 | |
1 | |
1 | |