Loading practice problems…
Loading your practice workspace…
Loading code editor…
9 / 29
Sign in
Algorithms/Sliding Window

Minimum Size Subarray Sum

medium30 min

Given an array of positive integers nums and a positive integer target, return the smallest length of a contiguous subarray whose sum is at least target.

Return 0 when no qualifying subarray exists.

Example 1

Inputtarget = 7, nums = [2, 3, 1, 2, 4, 3]

Output2

The adjacent values 4 and 3 add up to 7. Every single value is below 7, so the shortest qualifying subarray has length 2.

Example 2

Inputtarget = 4, nums = [1, 4, 4]

Output1

A single 4 already reaches the target, so the minimum length is 1.

Constraints

  • 1 ≤ nums.length ≤ 100,000
  • All values and target are positive integers.
Sliding WindowArrays