Smallest Subarray with Sum At Least K

Given an array of integers and a target K, find the length of the smallest contiguous subarray whose sum is >= K. If none exists, return -1.

Input Format

nums = array of integers, K = target sum

Output Format

Length of smallest subarray with sum >= K, or -1 if none

Constraints

  • 1 <= nums.length <= 10^5; -10^4 <= nums[i] <= 10^4

Examples

Example 1:

Input:

nums = [2,1,1,4,3,1,1]
K = 8

Output:

3

Explanation:

Subarray [4,3,1] has sum 8 with length 3.

Loading...
Smallest Subarray with Sum At Least K - Arrays