Sale
Summer Offer: $190/yr $99/yr
00 : 00 : 00 : 00
Get this deal
Backend Engineer Senior programming

Find the length of the shortest subarray with a sum at least K. You may assume that the array has at least one element. You must solve this in O(n) time complexity.


Constraints:


  • 1 <= arr.length <= 10^5

  • 1 <= arr[i] <= 10^4

  • 1 <= K <= 10^9


Examples:


1. Input: arr = [2, -1, 2, 3, 4], K = 3
Output: 2
Explanation: The subarray [2, 3] has the minimum length of 2.
2. Input: arr = [1, 2, 3, 4, 5], K = 11
Output: 3
Explanation: The subarray [3, 4, 5] has the minimum length of 3.
Suggested Answer

Trusted by 100+ professionals preparing for interviews

Trusted by 100+ professionals 50+ Company Question Banks 5+ Supported Languages

Practice More Questions Like This

Generate unlimited interview questions with structured answers, code runner, and AI-powered walkthroughs.

Get Started Free
Link copied to clipboard