O20

Minimum Size Subarray Sum

Mid / Senior Engineer
Target Time: 25 - 35 Minutes
Type: NUMERIC

Problem Statement & Objective

Given an array of positive integers nums and a positive integer target, return the minimal length of a contiguous subarray [nums[l], nums[l+1], ..., nums[r-1], nums[r]] of which the sum is greater than or equal to target. If there is no such subarray, return 0 instead.

Function Signature (Python)

min_subarray_len(target: int, nums: list[int]) -> int

Input & Output Contract

Output Rule:Minimal subarray length, or 0 if unreachable.

System Constraints

1 <= target <= 10^9
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^4

Example Execution Walkthrough

target = 7, nums = [2, 3, 1, 2, 4, 3]
subarray [4, 3] has sum 7 and length 2 -> result 2

Example Test Scenarios (3)

Example 1
Input:[7,[2,3,1,2,4,3]]
Expected Output:2
Example 2: Single Element 4
Input:[4,[1,4,4]]
Expected Output:1
Example 3: Yetersiz Toplam
Input:[11,[1,1,1,1,1,1,1,1]]
Expected Output:0
Loading Python Editor...
Input Parameters
[
  7,
  [
    2,
    3,
    1,
    2,
    4,
    3
  ]
]
Expected Output
2