Grokking Google Coding Interview
Ask Author
Back to course home

0% completed

Vote For New Content
Solution: Shortest Subarray with Sum at Least K
On this page

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

.....

.....

.....

Like the course? Get enrolled and start learning!

On this page