Minimum Size Subarray Sum

Medium Arrays & Hashing Sliding Window · Variable Original on LeetCode

Minimum Size Subarray Sum is a medium arrays & hashing problem solved with the sliding window · variable pattern. The best approach, optimal (variable sliding window), runs in O(n) time and O(1) space. Below are 2 approaches in Java, Python, C++ and JavaScript, from brute force up.

Problem

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

Examples

Example 1

Input
target = 8, nums = [3, 1, 2, 5, 2, 4]
Output
3
Why
[1, 2, 5] sums to 8 with length 3. No pair of neighbours reaches 8 (the largest is 5 + 2 = 7).

Example 2

Input
target = 20, nums = [2, 3, 4]
Output
0
Why
Even the whole array sums to only 9.

Constraints

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

Solve it here

Write your solution in Java, Python, C++ or JavaScript and run it against the examples, right in the page. Submit runs the hidden tests too and, with a free account, records the solve in your revision log.

00:00
Testcase

Run checks the testcases above (2 examples, or your own). Submit also runs 11 hidden tests and saves the solve to your log. Signed in, every Run and Submit is saved under My submissions; your draft stays in this browser.

Animated walkthrough

A narrated, step-by-step animation that builds the solution from the idea up. Press play, or step through it at your own pace.

Concepts first, then the problem and every approach, step by step.

Space to play or pause · ← → to jump a step · click or drag the bar to seek

Solutions

Try it yourself first. Then compare: each approach lists its idea, the steps, its time and space, and the code in Java, Python, C++ and JavaScript.

ApproachTimeSpace
Brute forceO(n²)O(1)
Optimal (variable sliding window)O(n)O(1)

1Brute force

TimeO(n²)
SpaceO(1)

For each start, extend until the sum reaches target and record the length.

  1. For each i, add nums[j] for j >= i until sum >= target.
  2. Track the smallest j - i + 1.
class Solution {
    public int minSubArrayLen(int target, int[] nums) {
        int best = Integer.MAX_VALUE;
        for (int i = 0; i < nums.length; i++) {
            int sum = 0;
            for (int j = i; j < nums.length; j++) {
                sum += nums[j];
                if (sum >= target) { best = Math.min(best, j - i + 1); break; }
            }
        }
        return best == Integer.MAX_VALUE ? 0 : best;
    }
}

2Optimal (variable sliding window)

TimeO(n)l and r each move at most n times.
SpaceO(1)

Grow the window on the right. While its sum is at least target, record the length and shrink from the left. Because values are positive, each element enters and leaves the window once.

  1. For each r: sum += nums[r].
  2. While sum >= target: best = min(best, r - l + 1); sum -= nums[l++].
  3. Return best, or 0 if it never changed.
class Solution {
    public int minSubArrayLen(int target, int[] nums) {
        int l = 0, sum = 0, best = Integer.MAX_VALUE;
        for (int r = 0; r < nums.length; r++) {
            sum += nums[r];
            while (sum >= target) {
                best = Math.min(best, r - l + 1);
                sum -= nums[l++];
            }
        }
        return best == Integer.MAX_VALUE ? 0 : best;
    }
}

Edge cases to test

  • No subarray reaches the target (return 0)
  • A single element already reaches the target

Hints

Hint 1

All numbers are positive, so growing the window only increases the sum and shrinking only decreases it.

FAQ

What is the best time complexity for Minimum Size Subarray Sum?

Optimal (variable sliding window) runs in O(n) time and O(1) extra space. l and r each move at most n times.

Which pattern does Minimum Size Subarray Sum use?

It is a arrays & hashing problem that uses the sliding window · variable pattern. Other problems with the same pattern: Count of Subarrays with Product Less Than K, Longest Repeating Character Replacement, Longest Substring Without Repeating Characters.

Is there a brute force solution for Minimum Size Subarray Sum?

Yes. Brute force takes O(n²) time and O(1) space. For each start, extend until the sum reaches target and record the length.

Which edge cases should I test for Minimum Size Subarray Sum?

No subarray reaches the target (return 0); A single element already reaches the target.