Grokking Google Coding Interview
Ask Author
Back to course home

0% completed

Vote For New Content
Shortest Subarray with Sum at Least K (hard)
On this page

Problem Statement

Examples

Try it yourself

Problem Statement

Given an array of integers nums and a positive integer k, return the minimum length of the non-empty subarray of nums such that sum of its elements should be at least k. If there is no such subarray, return -1.

A subarray is defined as a contiguous sequence of an array.

Examples

  1. Input: nums = [1, 2, 3, 4, 5], k = 11
    Expected Output: 3
    Justification: The shortest subarray with a sum of at least 11 is [3, 4, 5], which has a length of 3.

  2. Input: nums = [1, -1, 5, 2, 3, 4, 3, 2], k = 8
    Expected Output: 3
    Justification: The shortest subarray with a sum of at least 8 is [5, 2, 3], which has a length of 2.

  3. Input: nums = [-1, -1, -2, -3], k = 2
    Expected Output: -1
    Justification: The shortest subarray with a sum of at least 2 is not exist in the nums. So, the answer is -1.

Try it yourself

Try solving this question here:

Python3
Python3

. . . .

.....

.....

.....

Like the course? Get enrolled and start learning!

On this page

Problem Statement

Examples

Try it yourself