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:
k distinct numbers, andFor 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:
ansi-1 = 1, set gi-1 = li-1 + ri-1. Otherwise, set gi-1 = ri-1 - li-1.li = (li-1 XOR gi-1) % n and ri = (ri-1 XOR gi-1) % n.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 × 105No sample inputs for this problem yet. The examples in the statement show the format.