Shortest Subarray with Sum at Least K

Given integer array nums and positive k, return the length of the shortest nonempty contiguous subarray whose sum is at least k. Negative values are allowed.…

Given integer array nums and positive k, return the length of the shortest nonempty contiguous subarray whose sum is at least k. Negative values are allowed. Return -1 if no such subarray exists. Return a length, never indices or an empty length of zero.

- 1 <= nums.length <= 100000. - -100000 <= nums[i] <= 100000; 1 <= k <= 1000000000. - Inputs are strict integers; Booleans, fractions and numeric strings are invalid. - Prefix sums require signed 64-bit arithmetic in Java/C++. - Teaching traces contain at most 12 values within the ARRAY_TARGET transport; coding retains the full bounds.

Example 1: input nums = [3,-2,4], k = 4; output 1. The singleton [4] qualifies; its length 1 is minimal.

Example 2: input nums = [2,-1,2], k = 3; output 3. Only the complete interval has sum at least 3, so return 3.

Example 3: input nums = [1,-1,1], k = 2; output -1. Every interval sum is below 2; return -1, not zero.

Category: Queues. Topic: queue. Difficulty: Hard. Patterns: linear-scan.