Skip to main content

Exercise 8: Minimal Length Subarray

Given an array of positive integers and a positive integer target value, return the minimal length of a subarray which has a sum greater than or equal to target. If there is no such subarray in the array, return 0.

Input: [2,3,1,2,4,3], 7
Output: 2
Explanation: The subarray [4,3] sums to 7. The length of [4,3] is 2.

Input: [1,4,4], 4
Output: 1
Explanation: The subarray [4] sums to 4. The length of [4] is 1.

Input: [1,1,1,1,1,1,1,1], 11
Output: 0
Explanation: There is no subarray which sums to a value greater than or equal to 11.

Sketch:

Script: