Count Complete Subarrays in an Array
MediumUpdated: Aug 2, 2025
Practice on:
Problem
You are given an array nums consisting of positive integers.
We call a subarray of an array complete if the following condition is satisfied:
- The number of distinct elements in the subarray is equal to the number of distinct elements in the whole array.
Return the number of complete subarrays.
A subarray is a contiguous non-empty part of an array.
Examples
Example 1:
Input: nums = [1,3,1,2,2]
Output: 4
Explanation: The complete subarrays are the following: [1,3,1,2], [1,3,1,2,2], [3,1,2] and [3,1,2,2].
Example 2:
Input: nums = [5,5,5,5]
Output: 10
Explanation: The array consists only of the integer 5, so any subarray is complete. The number of subarrays that we can choose is 10.
Constraints:
1 <= nums.length <= 10001 <= nums[i] <= 2000
Solution
Method 1 - Using sliding window
We need to count the number of complete subarrays in the array nums.
- A subarray is complete if the count of distinct elements in it equals the count of distinct elements in the entire array
nums. - To solve this problem, the sliding window technique with hashing can be used efficiently. The sliding window allows us to dynamically validate subarrays while avoiding unnecessary computations.
Approach
- Compute the number of distinct elements in the array
numsusing a set. Let's call this valuetotal_distinct. - Use a sliding window strategy to traverse possible subarrays. Maintain a hash map (or dictionary) to keep track of the frequency of elements within the window.
- For each valid subarray (with
total_distinctelements), increment the count of complete subarrays. - Iterate over all possible subarray starting points using the above technique.
Code
Java
class Solution {
public int countCompleteSubarrays(int[] nums) {
Set<Integer> distinctSet = new HashSet<>();
for (int num : nums) {
distinctSet.add(num);
}
int totalDistinct = distinctSet.size();
Map<Integer, Integer> freqMap = new HashMap<>();
int ans = 0, left = 0;
for (int right = 0; right < nums.length; right++) {
// Add current element to the frequency map
freqMap.put(nums[right], freqMap.getOrDefault(nums[right], 0) + 1);
// Check for complete subarrays for the current window
while (freqMap.size() == totalDistinct) {
ans += nums.length - right; // Count all extensions to the right
freqMap.put(nums[left], freqMap.get(nums[left]) - 1);
if (freqMap.get(nums[left]) == 0) {
freqMap.remove(nums[left]);
}
left++;
}
}
return ans;
}
}
Python
class Solution:
def countCompleteSubarrays(self, nums: List[int]) -> int:
total_distinct = len(set(nums)) # Calculate total distinct numbers in the array
freq_map = {}
ans = 0
left = 0
for right in range(len(nums)):
# Add current element to the frequency map
freq_map[nums[right]] = freq_map.get(nums[right], 0) + 1
# Check for complete subarrays for current window
while len(freq_map) == total_distinct:
ans += len(nums) - right # Count all extensions to the right
freq_map[nums[left]] -= 1
if freq_map[nums[left]] == 0:
del freq_map[nums[left]]
left += 1
return ans
Complexity
- ⏰ Time complexity:
O(n). The computation of distinct elements in the array takesO(n)using a set.- The sliding window approach also traverses the array once, yielding
O(n)overall.
- The sliding window approach also traverses the array once, yielding
- 🧺 Space complexity:
O(n). The hash map/dictionary can hold up tonelements in the worst case (if all elements are unique).