Count Complete Subarrays in an Array (LeetCode 2799): Solution and Explanation

Medium
Array
Hash Table
Sliding Window

Count the subarrays that contain every distinct value of the whole array at least once.

Intuition

First count k, the number of distinct values in the full array. A subarray is complete exactly when it holds all k of them.

Completeness only grows as a subarray gets longer. If [l..r] is complete, then pulling the start further left keeps every value inside, so [l-1..r], [l-2..r] and so on are complete too.

So for a fixed right end r, the complete subarrays are the ones whose start is before some boundary. If we find that boundary, the count for r is just its index. A sliding window finds it as r moves right.

Approach: Shrink until incomplete, then count starts

  1. Compute k = len(set(nums)) and keep a count per value plus have, the number of distinct values in the window.
  2. For each right, add nums[right]; if its count becomes 1, increase have.
  3. While have == k, remove nums[left], decreasing have if its count drops to 0, and move left forward.
  4. After the loop, the window [left..right] is missing one value, while [left-1..right] was complete. So starts 0..left-1 are all complete: add left to the answer.
  5. Return the total.

Step-by-step example

nums = [1,3,1,2,2]
  1. k = 3 (values 1, 2, 3).
  2. right = 0, 1, 2: the window holds at most 1 and 3, never 3 distinct values, so left stays 0 and nothing is added.
  3. right = 3 (value 2): the window [0..3] has all three. Remove 1 at index 0, still complete because index 2 has a 1. Remove 3 at index 1, now 3 is missing. left = 2, add 2.
  4. right = 4 (value 2): the window [2..4] holds 1 and 2 only. left stays 2, add 2.
  5. Total = 2 + 2 = 4.

Code: Python, Java, C++

1class Solution:
2    def countCompleteSubarrays(self, nums: List[int]) -> int:
3        k = len(set(nums))
4        count = [0] * 2001
5        have = 0
6        left = 0
7        res = 0
8        for x in nums:
9            count[x] += 1
10            if count[x] == 1:
11                have += 1
12            while have == k:
13                count[nums[left]] -= 1
14                if count[nums[left]] == 0:
15                    have -= 1
16                left += 1
17            res += left
18        return res
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(n)

Each index is added once and removed at most once, and every step is constant work.

Space

O(V)

A count array over the value range (V = 2000), or a hash map with at most k entries.

Other approaches

Try every start (time O(n^2), space O(k))

For each start, extend right with a set until it holds k values; then every longer subarray from that start is complete too, so add n - r. With n up to 1000 this is fast enough.

At most k minus at most k - 1 (time O(n), space O(k))

The number of subarrays with exactly k distinct values equals atMost(k) - atMost(k - 1), the trick from Subarrays with K Different Integers. Since no subarray can have more than k, exactly k is the answer.

Common mistakes

  • Adding right - left + 1 like in many window problems. Here the complete subarrays are the ones starting before left, so the amount to add is left.
  • Forgetting the case k = 1. Every subarray is complete, and the window logic handles it: left ends at right + 1 each time, giving 1 + 2 + ... + n.
  • Decreasing have on every removal instead of only when a value's count reaches 0.
  • Comparing against the window's own distinct count instead of the whole array's k.

Frequently asked questions

Why does adding left count the right subarrays?

The while loop stops the first time the window loses a value, so [left-1..right] was complete and [left..right] is not. Every start from 0 to left-1 contains [left-1..right] and is complete; every start from left onward is inside [left..right] and is not.

Can left move backward?

No. When right grows, a window that was incomplete can only become complete, never the reverse, so the boundary only moves forward.

Does the answer fit in an int?

Yes. With n at most 1000 there are at most 500,500 subarrays.