Find the length of the shortest contiguous subarray whose sum is at least the target.
Input
The first line has n and target (1 ≤ n ≤ 10⁵, 1 ≤ target ≤ 10⁹). The second line has n positive integers (each at most 10⁴).
Output
The shortest length, or 0 if no subarray reaches the target.
Examples
Example 1 · Input
6 7 2 3 1 2 4 3
Expected output
2
Example 2 · Input
3 11 1 1 1
Expected output
0
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
All numbers are positive, so growing the window only increases its sum.
Hint 2
Move right to grow; while the sum is at least the target, record the length and shrink from the left.
Hint 3
Print 0 if no window ever reaches the target.
Solution
Show one correct solution (try it yourself first)
n, target = map(int, input().split())
nums = list(map(int, input().split()))
left = total = 0
best = n + 1
for right, x in enumerate(nums):
total += x
while total >= target:
best = min(best, right - left + 1)
total -= nums[left]
left += 1
print(best if best <= n else 0)