Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
You are given an array nums consisting of positive integers.
We call a subarray of an array complete if the following condition is satisfied:
Return the number of complete subarrays.
A subarray is a contiguous non-empty part of an array.
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] <= 2000Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map · Sliding Window
[1,3,1,2,2]
[5,5,5,5]
longest-substring-without-repeating-characters)subarrays-with-k-different-integers)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2799: Count Complete Subarrays in an Array
class Solution {
public int countCompleteSubarrays(int[] nums) {
Set<Integer> s = new HashSet<>();
for (int x : nums) {
s.add(x);
}
int cnt = s.size();
int ans = 0, n = nums.length;
for (int i = 0; i < n; ++i) {
s.clear();
for (int j = i; j < n; ++j) {
s.add(nums[j]);
if (s.size() == cnt) {
++ans;
}
}
}
return ans;
}
}
// Accepted solution for LeetCode #2799: Count Complete Subarrays in an Array
func countCompleteSubarrays(nums []int) (ans int) {
s := map[int]bool{}
for _, x := range nums {
s[x] = true
}
cnt := len(s)
for i := range nums {
s = map[int]bool{}
for _, x := range nums[i:] {
s[x] = true
if len(s) == cnt {
ans++
}
}
}
return
}
# Accepted solution for LeetCode #2799: Count Complete Subarrays in an Array
class Solution:
def countCompleteSubarrays(self, nums: List[int]) -> int:
cnt = len(set(nums))
ans, n = 0, len(nums)
for i in range(n):
s = set()
for x in nums[i:]:
s.add(x)
if len(s) == cnt:
ans += 1
return ans
// Accepted solution for LeetCode #2799: Count Complete Subarrays in an Array
use std::collections::HashSet;
impl Solution {
pub fn count_complete_subarrays(nums: Vec<i32>) -> i32 {
let mut s = HashSet::new();
for &x in &nums {
s.insert(x);
}
let cnt = s.len();
let n = nums.len();
let mut ans = 0;
for i in 0..n {
s.clear();
for j in i..n {
s.insert(nums[j]);
if s.len() == cnt {
ans += 1;
}
}
}
ans
}
}
// Accepted solution for LeetCode #2799: Count Complete Subarrays in an Array
function countCompleteSubarrays(nums: number[]): number {
const s: Set<number> = new Set(nums);
const cnt = s.size;
const n = nums.length;
let ans = 0;
for (let i = 0; i < n; ++i) {
s.clear();
for (let j = i; j < n; ++j) {
s.add(nums[j]);
if (s.size === cnt) {
++ans;
}
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
For each starting index, scan the next k elements to compute the window aggregate. There are n−k+1 starting positions, each requiring O(k) work, giving O(n × k) total. No extra space since we recompute from scratch each time.
The window expands and contracts as we scan left to right. Each element enters the window at most once and leaves at most once, giving 2n total operations = O(n). Space depends on what we track inside the window (a hash map of at most k distinct elements, or O(1) for a fixed-size window).
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.
Wrong move: Using `if` instead of `while` leaves the window invalid for multiple iterations.
Usually fails on: Over-limit windows stay invalid and produce wrong lengths/counts.
Fix: Shrink in a `while` loop until the invariant is valid again.