Problem · Array
Minimum Size Subarray Sum With Negatives
Learn this problemProblem statement
Given an integer array nums, which may contain negative values, and a positive integer k, return the minimum length of a non-empty contiguous subarray whose sum is at least k.
If no such subarray exists, return 0.
Function
minSubArrayLenWithNegatives(nums: int[], k: int) → intExamples
Example 1
nums = [2,3,1,2,4,3]k = 7return = 2The subarray [4,3] has sum 7, and no one-element subarray reaches 7.
Example 2
nums = [2,-1,2]k = 3return = 3Only the complete subarray has sum at least 3.
Example 3
nums = [1,-1,1]k = 4return = 0No non-empty subarray reaches a sum of 4.
Constraints
1 <= nums.length <= 10^5.1 <= k <= 10^9.numsmay contain positive, zero, and negative integers.