AlgoMaster Logo

Minimum Size Subarray Sum

mediumFrequency8 min readUpdated June 23, 2026

Understanding the Problem

We're looking for the shortest contiguous subarray whose elements sum to at least target. Two properties of the input shape the solution.

First, the array contains only positive integers. As we extend a subarray to the right, the sum can only increase, and as we shrink it from the left, the sum can only decrease. This monotonic behavior is what makes the sliding window technique work here.

Second, we want the minimum length, not the subarray itself, so we only track the best length seen so far as we scan through the array.

Key Constraints:

  • nums.length up to 10^5 rules out a comfortable O(n^2) scan, so we aim for O(n) or O(n log n).
  • All values are positive (both nums[i] and target). This guarantees that prefix sums are strictly increasing, which is what makes binary search applicable. It also means the sliding window's sum behaves monotonically.
  • target can be up to 10^9 and nums[i] up to 10^4, so the sum of all 10^5 elements reaches 10^9. A subarray sum plus target can approach 2 * 10^9, which is close to the 32-bit integer limit. There are also valid cases where no subarray meets the target.

Approach 1: Brute Force

Intuition

Check every possible subarray. For each starting index i, extend the subarray to the right, accumulating the sum. The moment the sum reaches target, record the length and move on to the next starting index. We break early for each i once we find a valid subarray starting there: because we extend one element at a time, the first valid subarray for a given start is also the shortest for that start.

Algorithm

  1. Initialize minLen to infinity (or n + 1 as a sentinel).
  2. For each starting index i from 0 to n - 1:
    • Initialize a running sum to 0.
    • For each ending index j from i to n - 1:
      • Add nums[j] to the running sum.
      • If the sum >= target, update minLen = min(minLen, j - i + 1) and break.
  3. If minLen is still the sentinel, return 0. Otherwise return minLen.

Example Walkthrough

1i=0: sum=2, then 5, then 6, then 8 >= 7. len=4, minLen=4
0
2
i
1
3
2
1
3
2
4
4
5
3
1/7

Code

The brute force re-scans elements from scratch for every starting index. When the subarray [i..j] already meets the target, moving the start to i + 1 can only require a right boundary that stays the same or moves further right, never one that moves left. The next approach exploits this to avoid resetting the right pointer.

Approach 2: Sliding Window

Intuition

Since all elements are positive, adding an element to the window increases the sum and removing one decreases it. This monotonic behavior lets us maintain a single window with two pointers instead of restarting at every index.

Expand the window by moving the right pointer forward, adding each new element to a running sum. Once the sum reaches target, the window is valid but possibly longer than necessary, so shrink it from the left: subtract nums[left], advance left, and continue while the sum stays at or above target, updating the minimum length on each valid window.

Algorithm

  1. Initialize left = 0currentSum = 0, and minLen = n + 1.
  2. Iterate right from 0 to n - 1:
    • Add nums[right] to currentSum.
    • While currentSum >= target:
      • Update minLen = min(minLen, right - left + 1).
      • Subtract nums[left] from currentSum.
      • Increment left.
  3. Return 0 if minLen is still the sentinel, otherwise return minLen.

Example Walkthrough

1right=0: sum=2 < 7, expand
0
R
2
L
1
3
2
1
3
2
4
4
5
3
1/9

Code

The sliding window is optimal at O(n) time and O(1) space, so for this problem there is no need to look further. The follow-up asks for an O(n log n) solution, and the prefix-sum-plus-binary-search approach below answers it. It is worth seeing for a reason beyond the follow-up: the sliding window depends on every element being positive, while the prefix-sum approach can be adapted to arrays that contain negative numbers, where shrinking from the left no longer reduces the sum predictably.

Approach 3: Prefix Sum + Binary Search

Intuition

Build a prefix sum array where prefix[i] is the sum of the first i elements. The sum of the subarray from index i to j is then prefix[j + 1] - prefix[i]. For that difference to be at least target, we need prefix[j + 1] >= prefix[i] + target, so for each start index i we search for the smallest end index whose prefix value reaches that threshold.

Since all elements are positive, the prefix sum array is strictly increasing, and a strictly increasing array is sorted. We can binary search it for the value prefix[i] + target, which gives the right boundary of the smallest valid window starting at index i.

Algorithm

  1. Build a prefix sum array of size n + 1 where prefix[0] = 0 and prefix[i] = prefix[i-1] + nums[i-1].
  2. Initialize minLen = n + 1.
  3. For each index i from 0 to n:
    1. Binary search in prefix for the smallest index j where prefix[j] >= prefix[i] + target.
    2. If such a j exists and j <= n, update minLen = min(minLen, j - i).
  4. Return 0 if minLen is still the sentinel, otherwise return minLen.

Example Walkthrough

1Prefix sums built: [0, 2, 5, 6, 8, 12, 15]
0
0
1
2
2
5
3
6
4
8
5
12
6
15
1/9

Code