Minimum Size Subarray Sum - Problem
Find the shortest contiguous subarray that meets a sum threshold!

You're given an array of positive integers nums and a positive integer target. Your mission is to find the minimal length of a contiguous subarray whose sum is greater than or equal to the target value.

๐ŸŽฏ Goal: Return the length of the shortest qualifying subarray
๐Ÿ“ Special case: If no such subarray exists, return 0

This classic problem tests your ability to optimize from a brute force O(nยฒ) solution to an elegant O(n) sliding window approach!

Input & Output

example_1.py โ€” Basic Case
$ Input: target = 7, nums = [2,3,1,2,4,3]
โ€บ Output: 2
๐Ÿ’ก Note: The subarray [4,3] has the minimal length 2 under the problem constraint. Other valid subarrays include [2,3,1,2] with length 4, but we want the shortest one.
example_2.py โ€” Single Element
$ Input: target = 4, nums = [1,4,4]
โ€บ Output: 1
๐Ÿ’ก Note: The subarray [4] has the minimal length 1. Since 4 >= 4, a single element is sufficient.
example_3.py โ€” No Solution
$ Input: target = 11, nums = [1,1,1,1,1,1,1,1]
โ€บ Output: 0
๐Ÿ’ก Note: The target sum 11 cannot be achieved with any subarray since the total sum is only 8. Therefore, return 0.

Visualization

Tap to expand
Sliding Window Technique VisualizationArray: [2, 3, 1, 2, 4, 3] Target: 7231243Window Evolution:Step 1-3: Expand[2,3,1,2] sum=8 >= 7 โœ“ length=4Step 4: Contract[3,1,2] sum=6 < 7 โœ—Step 5: Expand[3,1,2,4] sum=10 >= 7 โœ“ length=4๐ŸŽฏ Optimal Found!Subarray: [4, 3]Sum: 7 >= 7 โœ“Length: 2 (minimum!)LR๐Ÿ’ก Key Insight: Since all numbers are positive, shrinking from left never causes us to miss better solutions!
Understanding the Visualization
1
Initialize Window
Start with both pointers at the beginning, window size = 0
2
Expand Window
Move right pointer to include more elements until sum >= target
3
Contract Window
Move left pointer to shrink window while maintaining sum >= target
4
Track Minimum
Record the smallest valid window size encountered
5
Continue Process
Keep expanding and contracting until right pointer reaches end
Key Takeaway
๐ŸŽฏ Key Insight: The sliding window technique transforms an O(nยฒ) brute force approach into an elegant O(n) solution by intelligently expanding and contracting a window, leveraging the property that all array elements are positive.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(nยฒ)

We have two nested loops: outer loop runs n times, inner loop runs up to n times in worst case

n
2n
โš  Quadratic Growth
Space Complexity
O(1)

Only using a few variables to track current sum, minimum length, and loop indices

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค target โ‰ค 109
  • 1 โ‰ค nums.length โ‰ค 105
  • 1 โ‰ค nums[i] โ‰ค 104
  • All integers are positive (crucial for sliding window approach)
Asked in
Facebook 85 Amazon 72 Google 68 Microsoft 54 Apple 41
94.2K Views
High Frequency
~18 min Avg. Time
2.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen