FastPrepShortest Subarray with Sum at Least K

Shortest Subarray with Sum at Least K

Goldman Sachs logoGoldman Sachs● HardFULLTIMEPHONE SCREEN
Learn

Problem statement

Given a signed integer array nums and an integer k, return the length of the shortest nonempty contiguous subarray whose sum is at least k.

Return -1 if no such subarray exists.

Function

shortestSubarrayAtLeastK(nums: int[], k: long) → int

Examples

Example 1

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

Only the entire array reaches sum 3.

Example 2

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

No subarray reaches 4.

Constraints

  • 1 <= nums.length <= 200000
  • -10^9 <= nums[i] <= 10^9
  • 1 <= k <= 10^18

More Goldman Sachs problems

See Goldman Sachs hiring insights
public int shortestSubarrayAtLeastK(int[] nums, long k) {
  // write your code here
}
nums[2,-1,2]
k3
expected3
Checking account…