Valid K-Unique Subarrays I - Solution & Explanation
Practice this problemProblem Statement
You are given an integer array nums and an integer k.
You are also given a 2D integer array queries, where queries[i] = [li, ri] represents the subarray nums[li..ri].
For each query, the subarray nums[li..ri] 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[li..ri] 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 |
[li, ri] |
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 |
[li, ri] |
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 <= 1051 <= nums[i] <= 1051 <= k <= n1 <= queries.length <= 105queries[i] == [li, ri]0 <= li < ri <= n - 1
Approach Overview
Problem Overview: Given an integer array nums and an integer k, return the number of subarrays that contain exactly k distinct values. This is a classic sliding window problem where you need to count contiguous subarrays with a fixed number of unique elements.
Approach 1: Brute Force (O(n²) time, O(n) space)
Iterate over every possible subarray using two nested loops. For each starting index, expand the ending index while maintaining a frequency map. After each expansion, check if the number of distinct keys equals k; if so, increment the counter. This approach is straightforward but inefficient for large inputs because it explores all subarrays, leading to quadratic time complexity.
Approach 2: Sliding Window with At Most K (O(n) time, O(k) space)
The key insight is that the number of subarrays with exactly k distinct elements equals subarraysWithAtMostK(k) - subarraysWithAtMostK(k-1). Implement a helper function that counts subarrays with at most k distinct elements using a sliding window. Expand the right pointer, add the new element to a frequency map, and if the map size exceeds k, shrink the left pointer until the size is ≤ k. For each valid window, add right - left + 1 to the count. This runs in O(n) because each element is processed at most twice (once when added, once when removed).
Approach 3: Direct Sliding Window with Two Pointers (O(n) time, O(k) space)
For a direct solution, maintain two pointers left and right and a frequency map. Expand right and add the element. If the number of distinct elements exceeds k, move left forward and decrement frequencies until the count drops to k. Then, count all subarrays ending at right that have exactly k distinct elements by using a helper to find the minimum left index that still gives exactly k distinct elements. This approach is more complex but avoids the subtraction trick. However, the subtraction method is cleaner and equally efficient.
Recommended for interviews: The optimal solution is the sliding window with the at-most-k subtraction technique. It demonstrates a deep understanding of the sliding window pattern and reduces the problem to a simpler subproblem. Brute force shows you understand the problem but fails on constraints. The optimal approach is expected in interviews at top companies like Google, Amazon, and Meta.
Solutions for this problem are being prepared.
Try solving it yourselfDetailed Complexity Analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force | O(n²) | O(n) | Only for very small inputs or educational purposes |
| Sliding Window (At Most K) | O(n) | O(k) | Optimal for most cases, especially when k is small |
| Direct Sliding Window | O(n) | O(k) | When you need to avoid the subtraction trick or want a single-pass solution |
Video Solution
Beat My Solution: LeetCode 4033's Valid K-Unique Subarrays I (Hard)
Frequently Asked Questions
Is Valid K-Unique Subarrays I easy or hard?
Valid K-Unique Subarrays I Python/Java solution
How to solve Valid K-Unique Subarrays I in O(n)?
What is the best approach for Valid K-Unique Subarrays I?
Is Valid K-Unique Subarrays I asked at Google/Amazon/Meta?
What data structure is used in Valid K-Unique Subarrays I?
What is the time complexity of Valid K-Unique Subarrays I?
Ready to solve this problem?
Practice Valid K-Unique Subarrays I with our built-in code editor and test cases.
Practice on FleetCodeProblem Info
Table of Contents
Practice this problem
Open in Editor