SystemTutor
About
Sign in
Loading course documentation…
SystemTutor

COURSES

  • Data Structures & Algorithms
  • CSS MasterclassNew course
  • System DesignNew course

PRACTICE

  • Algorithm practice
  • CSS practice

LEGAL

  • Privacy Policy
  • Terms of Service
  • About

© 2026 SystemTutor. All rights reserved.

Built with 🫶 by Shatlyk

Course Overview
Algorithm Patterns OverviewFrequency CountersTwo SumValid AnagramGroup AnagramsFrequency Counters and Hash-Map LookupTwo PointersValid PalindromeTwo Sum IIContainer With Most WaterTwo-Pointer ReasoningSliding WindowMaximum Average Subarray ILongest Substring Without Repeating CharactersMinimum Size Subarray SumSliding-Window Reasoning
Algorithm Patterns

Minimum Size Subarray Sum

Use a shrinking sliding window to find the shortest qualifying subarray.

Try It Yourself

Use the editor below to find the shortest contiguous window whose sum reaches the target.

Shrink the window when it reaches the target

The positive input values allow the window to shrink safely whenever its sum is large enough. Return 0 if no window reaches the target.

Submit your implementation when it passes the examples.

Minimum Size Subarray Sum

mediumSliding Window · Arrays30 minLC 209
Problem

Given an array of positive integers nums and a positive integer target, return the smallest length of a contiguous subarray whose sum is at least target.

Return 0 when no qualifying subarray exists.

Example 1:

Input: target = 7, nums = [2, 3, 1, 2, 4, 3]
Output: 2
Explanation: The adjacent values 4 and 3 add up to 7. Every single value is below 7, so the shortest qualifying subarray has length 2.

Example 2:

Input: target = 4, nums = [1, 4, 4]
Output: 1
Explanation: A single 4 already reaches the target, so the minimum length is 1.

Constraints

  • 1 ≤ nums.length ≤ 100,000
  • All values and target are positive integers.
0 attempts

Test results

Results appear here after a run.

Solution

The right edge expands until the sum reaches the target. The left edge then moves inward to test shorter windows.

Start with shortest = 0 to show that no window has reached the target yet. Expand the right edge and add each value to the running sum. While the sum is at least the target, save the current length if it is the first qualifying length or shorter than the best so far. Then remove the leftmost value and check the smaller window.

For target 7, suppose the current window is [1, 2, 4, 3] with sum 10. Removing 1 leaves sum 9. Removing 2 leaves [4, 3] with sum 7. Record length two before removing 4 makes the sum too small.

function minSubArrayLen(target, nums) {
  let left = 0;
  let sum = 0;
  let shortest = 0;

  for (let right = 0; right < nums.length; right++) {
    sum += nums[right];

    while (sum >= target) {
      const length = right - left + 1;
      if (shortest === 0 || length < shortest) {
        shortest = length;
      }

      sum -= nums[left];
      left++;
    }
  }

  return shortest;
}

Big O notation

MeasureComplexityExplanation
TimeO(n)Each value is added once and removed from the window at most once.
Auxiliary spaceO(1)The window is represented by indices and a running sum.

How is this practice?

Previous lessonLongest Substring Without Repeating CharactersNext lessonSliding-Window Reasoning
Edit on GitHubReport on GitHub
On this page
%

On this page

Try It YourselfSolutionBig O notation