Compile Ready
Module 3 · Variable Window

Minimum Size Subarray Sum

MediumProblem 6 of 17 8 min read ~16 min to solve LeetCode
Sliding WindowTwo PointersArrayPrefix SumVariable Window
Asked atAmazonGoogleMicrosoftMetaApple

Problem Statement

Given an array of positive integers nums and a positive integer target, return the minimal length of a contiguous subarray whose sum is at least target. If no such subarray exists, return 0.

Input

A positive integer target and an array of positive integers nums.

Output

An integer: the length of the shortest contiguous subarray with sum at least target, or 0 if none exists.

Constraints

  • 1 <= target <= 10^9
  • 1 <= nums.length <= 10^5
  • 1 <= nums[i] <= 10^4

Examples

Example 1

Input:
target = 7, nums = [2,3,1,2,4,3]
Output: 2
Explanation: The shortest valid subarray is **[4,3]**, whose sum is 7.

Example 2

Input:
target = 4, nums = [1,4,4]
Output: 1
Explanation: The single element **4** already reaches the target.

Example 3

Input:
target = 11, nums = [1,1,1,1,1,1,1,1]
Output: 0
Explanation: Even the full array sums to only 8, so no valid subarray exists.

Learning Objectives

  • Recognise a shortest-valid variable window over positive numbers.
  • Use positivity to justify shrinking while the current sum remains at least the target.
  • Record the answer before removing the leftmost element from a valid window.
  • Distinguish this linear window from prefix-sum binary search alternatives.

Intuition

Pattern Identification

This is a shortest-valid variable-window problem. The window condition is sum >= target, and all numbers are positive. That positivity is what makes the window monotone: expanding right never decreases the sum, and shrinking left never increases it.

The expand and shrink invariant is: expand until the window is valid, then shrink as much as possible while it stays valid. Every time the sum reaches the target, the current window is a candidate answer. Shrinking immediately tests whether a shorter candidate with the same right boundary exists.

Common mistakes

  • ×Using a sliding window when negative numbers are allowed; the monotone sum property would break.
  • ×Updating the best length only after shrinking, which can miss the current valid window.
  • ×Stopping after finding the first valid window instead of continuing to find a shorter one.
  • ×Returning the sentinel length instead of **0** when no valid subarray exists.

Algorithm Explanation

Window setup

Keep left, right, the current window sum, and bestLength. The window contains positive numbers from left through right. Because every value is positive, moving left rightward makes the sum smaller in a predictable way.

Window visualization

For target = 7 and nums = [2,3,1,2,4,3], expand until the sum first reaches 8 at window [2,3,1,2]. Record length 4, then shrink from the left: removing 2 leaves sum 6, so this right boundary cannot produce a shorter valid window. Continue expanding to include 4, which gives sum 10 over [3,1,2,4]. Now repeated shrinking finds [2,4] with length 2 before the sum drops below 7. The final 3 confirms another length 2 window [4,3].

Algorithm

  1. Set left = 0, sum = 0, and bestLength to a sentinel larger than the array length.
  2. For each right index, add nums[right] to sum.
  3. While sum >= target, update bestLength with the current length.
  4. Subtract nums[left] and increment left to search for a shorter valid window.
  5. After the scan, return 0 if the sentinel was never changed; otherwise return bestLength.

Solutions

Solution: Shortest valid positive-sum window

Since every element is positive, once a window reaches the target we can safely remove elements from the left until it becomes invalid. That finds the shortest valid window ending at each right index.

Step-by-step

  1. Expand the right boundary and add each number to the running sum.
  2. Whenever the sum is at least target, record the current window length.
  3. Remove nums[left] and move left forward to test a shorter window with the same right boundary.
  4. Repeat the shrink step until the sum falls below target.
  5. Return the best recorded length, or 0 if no valid window was recorded.
Time

O(n)

Space

O(1)

Both pointers move from left to right at most once.

Java implementation

Loading…

Dry Run

Sample input

target = 7, nums = [2,3,1,2,4,3]. Track the positive-sum window and update the best length before each shrink.

stepright valueleft before shrinkingsum after expandshrink actionsbest length
12 at 002sum < 7, keep expandingnone
23 at 105sum < 7, keep expandingnone
31 at 206sum < 7, keep expandingnone
42 at 308record 4, remove 2, sum becomes 64
54 at 4110record 4, remove 3; record 3, remove 1; record 2, remove 22
63 at 547record 2, remove 4, sum becomes 32

The shortest valid window has length 2, achieved by [2,4] and [4,3].

Interview Tips

Call out that positivity is the reason the two-pointer window works. If the interviewer allows negative numbers, this exact shrink rule is no longer safe and you need a prefix-sum plus monotonic deque style approach for related variants. In this problem, record the length before subtracting from the left because the current window is valid at that moment.

Likely follow-ups

  • What changes if **nums** can contain negative values?
  • How would you return the start and end indices of the shortest valid subarray?
  • How would you solve many target queries over the same positive array?
  • Can you solve it with prefix sums and binary search, and when would that be useful?

Similar Problems

Key Takeaways

  • Shortest-valid windows record an answer as soon as the window becomes valid.
  • Positive numbers make sum-based shrinking safe and monotone.
  • The inner while loop finds the shortest valid window for the current **right** boundary.
  • Return **0** when no window ever reaches the target.
Reusable template: For shortest positive-sum windows, expand until the sum reaches the target, then repeatedly record and shrink left until the window becomes invalid.