Shortest Length Subarray Sum
EasyQuestion
You're given an array of positive integers and a target. Find the length of the shortest subarray that adds up to at least the target. A subarray has to be numbers that sit next to each other.
If no subarray adds up to the target, return 0.
Input: nums = [2, 3, 1, 1, 3, 4], target = 6
Output: 2
The shortest subarray that adds up to at least 6 is [3, 4]. It has a length of 2.
Input: nums = [1, 5, 1], target = 4
Output: 1
The shortest subarray that adds up to at least 4 is [5]. It has a length of 1.
Input: nums = [1, 1, 1], target = 4
Output: 0
No subarray adds up to 4 or more.
Clarify the problem
What are some questions you'd ask an interviewer?
Understand the problem
What is the length of the shortest subarray that adds up to at least 8? [5, 2, 3, 3, 2, 1]
0
1
2
3
Remember to understand the problem and plan your approach before you start coding.