Minimum Size Subarray Sum — Solution in Python, Java & C++
Minimum Size Subarray Sum solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript. A medium problem.
Minimum Size Subarray Sum is a medium coding problem on arrays, prefix sum and sliding window, asked in HCL and Arcesium interviews.
- Difficulty: Medium
- Topics: Arrays, Prefix Sum, Sliding Window
- Asked at: HCL, Arcesium
- Time limit: 2 s
- Memory limit: 256 MB
- Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby
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 >= target. If none exists, return 0.
Example 1
Input: target = 7, nums = [2,3,1,2,4,3]
Output: 2
Explanation: [4,3] has the minimal length.
Example 2
Input: target = 11, nums = [1,1,1,1,1,1,1,1]
Output: 0
Constraints
1 <= target <= 2001 <= nums.length <= 301 <= nums[i] <= 50
Hints for Minimum Size Subarray Sum
Hint 1
All values are positive, so growing the window only increases the sum — a sliding window works.
Hint 2
Shrink from the left while the window sum still meets the target.
How to solve Minimum Size Subarray Sum
All values are positive, which is what makes a sliding window valid: extending the window strictly increases the sum and shrinking it strictly decreases it. So grow the window until it reaches the target, then shrink from the left as far as it still qualifies, recording the length each time.
Approach
- Keep a left edge
l, a runningsum, and the best length found (start it at infinity). - Advance the right edge
r, addingnums[r]tosum. - While
sum >= target, record the window lengthr - l + 1and then removenums[l], advancingl. - Continue to the end of the array.
- Return the best length, or
0if the window never qualified.
Why it works
Positivity gives monotonicity, so for each right edge there is a single leftmost l at which the window still meets the target — and the inner loop walks exactly to it. Every candidate subarray is therefore represented by the shortest window ending at some r, and the minimum over those is the global minimum. Each index enters and leaves the window once, so despite the nested loop the sweep is linear.
Complexity
- Time —
O(n) - Space —
O(1)
Pitfalls
- The condition is
>= target, not==— an exact hit is not required. - Shrink inside a
while, not anif; one long value can allow several left advances at once. - Return
0when no window qualifies, not the infinite sentinel. - This window relies on all values being positive. With negatives the monotonicity fails and prefix sums are needed instead.
Reference solution
Python
from typing import List
def minSubArrayLen(target: int, nums: List[int]) -> int:
l = 0
total = 0
best = float("inf")
for r, x in enumerate(nums):
total += x
while total >= target:
best = min(best, r - l + 1)
total -= nums[l]
l += 1
return 0 if best == float("inf") else bestJava
public static int minSubArrayLen(int target, int[] nums) {
int l = 0, sum = 0, best = nums.length + 1;
for (int r = 0; r < nums.length; r++) {
sum += nums[r];
while (sum >= target) {
if (r - l + 1 < best) best = r - l + 1;
sum -= nums[l];
l++;
}
}
return best == nums.length + 1 ? 0 : best;
}C++
int minSubArrayLen(int target, vector<int>& nums) {
int n = (int) nums.size();
int l = 0, sum = 0, best = n + 1;
for (int r = 0; r < n; r++) {
sum += nums[r];
while (sum >= target) {
if (r - l + 1 < best) best = r - l + 1;
sum -= nums[l];
l++;
}
}
return best == n + 1 ? 0 : best;
}JavaScript
var minSubArrayLen = function(target, nums) {
let l = 0, sum = 0, best = Infinity;
for (let r = 0; r < nums.length; r++) {
sum += nums[r];
while (sum >= target) {
best = Math.min(best, r - l + 1);
sum -= nums[l++];
}
}
return best === Infinity ? 0 : best;
};Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.
All 1199 arrays problems · the whole catalogue
Learn the technique: Arrays · Prefix Sum