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.
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).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.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.
minLen to infinity (or n + 1 as a sentinel).i from 0 to n - 1:0.j from i to n - 1:nums[j] to the running sum.target, update minLen = min(minLen, j - i + 1) and break.minLen is still the sentinel, return 0. Otherwise return minLen.n starting indices, we may scan up to n elements in the worst case. With the early break, the best case is better, but worst case remains quadratic.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.
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.
The window never misses the shortest valid subarray. For a fixed right boundary, shrinking from the left stops at the first position where the sum drops below target, so the window recorded is the shortest one ending at that right. Because positivity makes the sum monotonic in both pointers, once left passes a position it can never need to return: a smaller left only produces a longer window with a larger sum. Taking the minimum over every right boundary therefore covers every candidate.
Each element is added to currentSum once (when right reaches it) and subtracted at most once (when left passes it). The inner while loop advances left a total of at most n times across the whole run, so the algorithm performs at most 2n operations.
left = 0, currentSum = 0, and minLen = n + 1.right from 0 to n - 1:nums[right] to currentSum.currentSum >= target:minLen = min(minLen, right - left + 1).nums[left] from currentSum.left.0 if minLen is still the sentinel, otherwise return minLen.right and once by left. The inner while loop doesn't make this quadratic because left moves at most n times total across all iterations.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.
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.
Binary search needs a sorted array, and the prefix array is sorted only because every element is positive. With negative numbers present, prefix sums can decrease, the array is no longer sorted, and this exact technique breaks. The fix for that case keeps prefix sums but replaces the binary search with a monotonic deque (the approach used in "Shortest Subarray with Sum at Least K"). For an all-positive input, the sorted prefix array is what reduces each start's search from a linear scan to O(log n).
n + 1 where prefix[0] = 0 and prefix[i] = prefix[i-1] + nums[i-1].minLen = n + 1.i from 0 to n:prefix for the smallest index j where prefix[j] >= prefix[i] + target.j exists and j <= n, update minLen = min(minLen, j - i).0 if minLen is still the sentinel, otherwise return minLen.n + 1 starting positions, we perform a binary search over at most n elements, taking O(log n) each. Total: O(n) + O(n log n) = O(n log n).