Back to DSA

Minimum Size Subarray Sum

medium
Acceptance: 48%
ArraysSliding Window

An array of positive integers and a positive threshold value are provided. Find the length of the shortest contiguous subarray whose elements sum to at least the threshold. If no such subarray exists, return zero.

Examples

Example 1:
Input:threshold = 10, nums = [1,4,2,5,3,1]
Output:3
Explanation: The subarray [4,2,5] has sum 11, meeting the threshold. No shorter subarray reaches 10.
Example 2:
Input:threshold = 20, nums = [2,2,2,2]
Output:0
Explanation: The total of all elements is only 8, which is below 20.

Hints

00:00
1234567