Return Count, Indices
Naive Sum/Average
while expand + while shrink
Indexes of Subarray Sum
Find a contiguous subarray whose sum equals a given target s. The window expands to accumulate a greater total and shrinks to shed excess value.
Edge case: if s is zero, check for the presence of a zero in the array directly.
- Time
- O(N)
- Each element is considered at most twice.
- Space
- O(1)
- A fixed number of variables.
At Most K
while shrink
Distinct Values Subarrays II
Count subarrays with at most k distinct values. The atMost(k) helper expands the right edge, shrinks from the left whenever the distinct count exceeds k, and adds right - left (the number of valid subarrays ending at right) at every step.
- Time
- O(n)
nis the length ofs-leftandrighteach advance at mostntimes insideatMost, so every element enters and leaves the window once.- Space
- O(k)
counteris shrunk whenever its distinct count exceedsk, so it holds at mostkdistinct elements.
3258. Count Substrings That Satisfy K-Constraint I
A substring satisfies the k-constraint when its number of 0s is at most k or its number of 1s is at most k. A substring violates it only when both counts exceed k, and that violation never disappears as the window grows, so the valid substrings form a contiguous at most window.
atMost(k) expands right, then shrinks from the left while isValid() is false (both zeros > k and ones > k). Each step adds right - left, the number of valid substrings ending at right.
- Time
- O(N)
- Each character enters and leaves the window once across the single pass.
- Space
- O(1)
- Only two running counts (
zeros,ones) are tracked.
Less Than K (using atMost)
Idea: LessThan(k) = AtMost(k - 1)
while shrink
1513. Number of Substrings With Only 1s
A substring of only '1's is one that contains exactly zero '0' characters. That makes it a degenerate exactly-k case with k = 0: since atMost(-1) = 0, exactly(0) = atMost(0), so the helper returns the answer directly.
atMost(0) is a standard sliding window: expand right, and while the window holds more than 0 zeroes, shrink from the left. Each step contributes right - left substrings. Take the result mod 10**9 + 7.
- Time
- O(N)
Nis the length ofs.leftandrightinatMosteach advance at mostNtimes, so the window scan is a singleO(N)pass.- Space
- O(1)
- Only scalars (
left,right,count,zeroes) are tracked - no structure scales withN.
1759. Count Number of Homogenous Substrings
A homogenous substring contains only one unique character - that is at most 1 distinct character. This is the degenerate exactly-k case with k = 1: since atMost(0) = 0, exactly(1) = atMost(1), so the helper returns the answer directly.
atMost(k) is a standard sliding window over distinct count: expand right, and while the window holds more than k distinct characters, shrink from the left. Each step contributes right - left substrings. Take the result mod 10**9 + 7.
- Time
- O(N)
atMost(1)runs once, and within itrightandlefteach advance acrosssexactly once, so every character is added and removed fromcounteronce.- Space
- O(1)
counterholds one entry per distinct character seen, bounded by the fixed alphabet regardless ofN.
2743. Count Substrings Without Repeating Character
"Each character appears at most once" is already an at most constraint, so the answer is atMost(1) directly - no complement subtraction is needed (unlike the "at least k" problems in this section).
atMost(k) expands right, then shrinks from the left while the just-added character's frequency exceeds k. Each step contributes right - left valid substrings.
- Time
- O(N)
- Single pass through the string.
- Space
- O(26)
sconsists of lowercase English letters.
713. Subarray Product Less Than K
Count the number of contiguous subarrays where the product of all elements is less than k. Using the fact that less than k equals at most k-1, we use an atMost helper.
For each valid window, the number of new subarrays introduced by adding the latest element is right - left.
- Time
- O(N)
- Single pass with the sliding window.
- Space
- O(1)
- A minimal number of variables.
2302. Count Subarrays With Score Less Than K
Count contiguous subarrays where the score (sum * length) is less than k. Uses the same atMost pattern, with score = total * (right - left).
- Time
- O(n)
- Single pass through
nums(lengthn);leftandrighteach advance at mostnsteps. - Space
- O(1)
- A minimal number of variables.
Exactly K (using atMost)
Idea: Exactly(k) = AtMost(k) - AtMost(k - 1)
while shrink
930. Binary Subarrays With Sum
Count contiguous subarrays within a binary array that sum to a given goal. Using: exactly(goal) = atMost(goal) - atMost(goal - 1).
- Time
- O(2n)
atMostis called twice, and each call is a singleO(n)sliding-window pass overnums-2n.- Space
- O(1)
- Only
left,right,total, andcountare tracked per call.
1248. Count Number of Nice Subarrays
Count all subarrays that contain exactly k odd numbers. Using the equation: exactly(k) = atMost(k) - atMost(k - 1).
The atMost(k) helper counts subarrays with at most k odd numbers using a standard sliding window.
- Time
- O(2n)
atMost(k)runs a sliding window whererightandlefteach only advance forward across the array:O(n), wheren = len(nums).atMost(k) - atMost(k - 1)calls that sameO(n)window twice, so total isO(2n).- Space
- O(1)
- Only scalars (
left,right,odds,count) are tracked, no structure grows withn.
1358. Number of Substrings Containing All Three Characters
Count substrings that contain at least one of each character 'a', 'b', and 'c'. With exactly 3 distinct characters required, this becomes atMost(3) - atMost(2), where atMost(k) counts substrings with at most k distinct characters.
- Time
- O(2N)
atMost(k)is anO(n)sliding-window pass, and it's called twice -atMost(3)andatMost(2)- for2n.- Space
- O(1)
counterholds at most 3 distinct characters ('a', 'b', 'c'), independent ofn.
2799. Count Complete Subarrays in an Array
A "complete" subarray contains all k = len(set(nums)) distinct values - that is exactly k distinct values, since a window can never hold more. So this is an exactly-k problem: exactly(k) = atMost(k) - atMost(k - 1).
atMost(k) is a standard sliding window over distinct count: expand right, and while the window holds more than k distinct values, shrink from the left. Each step contributes right - left subarrays.
- Time
- O(2N)
- Two passes through the array, one for each call to
atMost. - Space
- O(K)
- The counter's size is bounded by
k, the number of unique elements.
992. Subarrays with K Different Integers
Count subarrays with exactly k different integers. Using: exactly(k) = atMost(k) - atMost(k - 1).
The atMost(k) helper counts subarrays with at most k distinct integers.
- Time
- O(2N)
- Two passes through the array.
- Space
- O(K)
- The counter holds up to
kdistinct elements.
At Least K (using atMost)
Idea: AtLeast(k) = Total - AtMost(k - 1)
while shrink
2537. Count the Number of Good Subarrays
A subarray is "good" if it has at least k pairs of equal elements. Counting "at least" directly is awkward, so flip it: count subarrays with at most k - 1 pairs and subtract from the total number of subarrays.
atMost(k) is a standard sliding window. When nums[right] enters, it forms one new pair with every equal element already inside the window - that is exactly the current count of nums[right] (read before incrementing). Shrink from the left while count > k. Every step, each window ending at right contributes right - left new subarrays.
The total subarray count is n * (n + 1) / 2, so the answer is total - atMost(k - 1).
- Time
- O(N)
- Single pass through the array.
- Space
- O(N)
- The counter could contain an entry for every unique element.
2962. Count Subarrays Where Max Element Appears at Least K Times
We want subarrays where the maximum element appears at least k times. "At least" is awkward to count directly, so flip it: count subarrays where the max appears at most k - 1 times and subtract from the total number of subarrays.
atMost(k) is a standard sliding window. Expand right, and whenever the window holds more than k copies of max_element, shrink from the left until it is valid again. Every step, each window ending at right contributes right - left subarrays.
The total subarray count is n * (n + 1) / 2, so the answer is total - atMost(k - 1).
- Time
- O(N)
- Single pass through the array.
- Space
- O(N)
- The counter tracks element frequencies.