Subarrays with K Different Integers - Problem

You're given an integer array nums and an integer k. Your task is to count the number of "good" subarrays in the array.

A subarray is considered good if it contains exactly k different integers. For example, the subarray [1,2,3,1,2] has exactly 3 different integers: 1, 2, and 3.

Remember: A subarray is a contiguous part of an array, meaning all elements must be adjacent to each other in the original array.

Goal: Return the total count of all good subarrays.

Example: If nums = [1,2,1,2,3] and k = 2, then subarrays like [1,2], [2,1], [1,2,1], [2,1,2] are all good because they contain exactly 2 different integers.

Input & Output

example_1.py โ€” Basic Case
$ Input: nums = [1,2,1,2,3], k = 2
โ€บ Output: 7
๐Ÿ’ก Note: Subarrays with exactly 2 distinct integers: [1,2], [2,1], [1,2,1], [2,1,2], [1,2,1,2], [2,3], [1,2,3] (only the last 6 positions). Total = 7
example_2.py โ€” Single Element
$ Input: nums = [1,2,1,3,4], k = 3
โ€บ Output: 3
๐Ÿ’ก Note: Subarrays with exactly 3 distinct integers: [1,2,1,3], [2,1,3], [1,3,4]. Each contains exactly 3 different numbers.
example_3.py โ€” Edge Case
$ Input: nums = [1,1,1], k = 1
โ€บ Output: 6
๐Ÿ’ก Note: All subarrays contain exactly 1 distinct integer: [1], [1], [1], [1,1], [1,1], [1,1,1]. Total = 3 + 2 + 1 = 6

Visualization

Tap to expand
Sliding Window Approach: exactly k = atMost(k) - atMost(k-1)12123atMost(2) windowatMost(1) windowWindow CalculationsatMost(2): 1+2+3+4+5 = 15atMost(1): 1+1+1+1+1 = 5exactly(2): 15 - 5 = 10Each position contributes (right-left+1) subarraysThe magic happens when we subtract the overlapping counts!๐ŸŽฏ Key Insight: Transform hard counting into easy sliding window problems!
Understanding the Visualization
1
Setup Two Counters
Create helper function to count subarrays with 'at most k' distinct integers
2
Expand Window
Move right pointer, add new elements to frequency map
3
Contract When Needed
If distinct count > k, move left pointer and update frequencies
4
Count Valid Subarrays
For each position, add (right - left + 1) to total count
5
Apply Formula
Return atMost(k) - atMost(k-1) to get exactly k distinct integers
Key Takeaway
๐ŸŽฏ Key Insight: The brilliant transformation 'exactly k = at most k - at most k-1' converts a complex counting problem into two simple sliding window problems, achieving O(n) time complexity!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Each element is visited at most twice (once by right pointer, once by left pointer) in each of the two helper function calls

n
2n
โœ“ Linear Growth
Space Complexity
O(k)

Hash map stores at most k distinct elements and their frequencies

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค nums.length โ‰ค 2 ร— 104
  • 1 โ‰ค nums[i], k โ‰ค nums.length
  • All integers in nums are positive
Asked in
Google 45 Amazon 32 Meta 28 Microsoft 22 Apple 18
89.4K Views
Medium-High Frequency
~25 min Avg. Time
2.2K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen