You are given an array of positive integers and a threshold t. Find the shortest contiguous subarray whose elements add up to at least t, and report its length. If no contiguous subarray reaches t, report 0.
Line 1: two integers n and t separated by a space.
Line 2: n space-separated positive integers.
A single integer: the minimum length of a contiguous subarray with sum ≥ t, or 0 if no such subarray exists.
Example 1
Input
6 7 2 3 1 2 4 3
Expected
2
Explanation
The subarray [4,3] has sum 7 ≥ 7 and length 2. No single element reaches 7, so 2 is the shortest.
Example 2
Input
5 20 1 2 3 4 5
Expected
0
Explanation
The entire array sums to 15, which is below 20, so no subarray qualifies and the answer is 0.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →