Minimum Size Subarray Sum

Medium

Description

Given an array of positive integers nums and a positive integer target, return the minimal length of a subarray whose sum is greater than or equal to target. If there is no such subarray, return 0.

Examples

Input:target = 7, nums = [2,3,1,2,4,3]
Output:2
Explanation:

Subarray [4,3] has minimal length.

Input:target = 11, nums = [1,1,1,1,1,1,1,1]
Output:0
Explanation:

Edge case returning zero.

Input:target = 15, nums = [5,1,3,5,10,7,4,9,2,8]
Output:2
Explanation:

The subarray [5,10] has sum 15 which equals the target, and length 2. Other valid subarrays like [10,7] (sum=17) and [9,8] (sum=17) also have length 2, but no subarray with sum ≥ 15 has length 1.

Constraints

  • 1 ≤ target ≤ 10⁹
  • 1 ≤ nums.length ≤ 10⁵
  • 1 ≤ nums[i] ≤ 10⁴

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!