AlgoMaster Logo

Minimum Size Subarray Sum

Last Updated: March 21, 2026

medium
4 min read

Understanding the Problem

We're looking for the shortest contiguous subarray whose elements sum to at least target. Two things jump out right away.

First, the array contains only positive integers. That's a huge deal. It means that 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 exactly what makes the sliding window technique work here.

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

Key Constraints:

  • nums.length up to 10^5 means O(n^2) is borderline. It might pass with a tight constant factor, but we should 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 unlocks binary search. 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 even summing all 10^5 elements gives at most 10^9. There are valid cases where no subarray meets the target.

Approach 1: Brute Force

Intuition

The most straightforward approach is to 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. Since we want the minimum, we can break early for each i once we find a valid subarray starting there (the first valid one for a given i is the shortest for that start, because we're extending one element at a time).

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. But if the subarray [i..j] has sum >= target, then when we move to i + 1, the right boundary can only stay the same or move right. Can we 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 means we can use a sliding window with two pointers.

The idea is simple. Expand the window by moving the right pointer forward, adding elements to the running sum. Once the sum reaches or exceeds target, we've found a valid window, but it might not be the shortest. So we try shrinking it from the left: subtract nums[left], advance left, and check if the window is still valid. We keep shrinking as long as the sum stays >= target, updating the minimum length each time.

Every element enters the window once (when right advances) and leaves once (when left advances). That's at most 2n operations total, giving us O(n).

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 already optimal at O(n) time and O(1) space. But the problem's follow-up asks for an O(n log n) solution using a different technique: prefix sums combined with binary search.

Approach 3: Prefix Sum + Binary Search

Intuition

Here's a different angle. If we build a prefix sum array where prefix[i] is the sum of the first i elements, then the sum of the subarray from index i to j is prefix[j + 1] - prefix[i]. We want that difference to be >= target, which means for each index i, we need to find the smallest j such that prefix[j + 1] >= prefix[i] + target.

Since all elements are positive, the prefix sum array is strictly increasing. A strictly increasing array is sorted, which means we can binary search for the value prefix[i] + target in the prefix array. This gives us the left 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