Problem · Array

Minimum Size Subarray Sum With Negatives

Learn this problem
HardTekion logoTekionINTERNONSITE INTERVIEW

Problem 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) → int

Examples

Example 1

nums = [2,3,1,2,4,3]k = 7return = 2

The subarray [4,3] has sum 7, and no one-element subarray reaches 7.

Example 2

nums = [2,-1,2]k = 3return = 3

Only the complete subarray has sum at least 3.

Example 3

nums = [1,-1,1]k = 4return = 0

No non-empty subarray reaches a sum of 4.

Constraints

  • 1 <= nums.length <= 10^5.
  • 1 <= k <= 10^9.
  • nums may contain positive, zero, and negative integers.

More Tekion problems

drafts saved locally
public int minSubArrayLenWithNegatives(int[] nums, int k) {
    // Write your code here.
}
nums[2,3,1,2,4,3]
k7
expected2
checking account