A device logs n consecutive non-negative power draws. To stay within a power cap b, you want the longest contiguous stretch of readings whose total draw does not exceed b. Report the length of that longest stretch (0 if even a single reading exceeds b).
Line 1: two integers n and b.
Line 2: n space-separated non-negative integers, the power draws.
A single integer: the length of the longest contiguous window whose sum is at most b.
Example 1
Input
6 7 2 1 1 3 1 4
Expected
4
Explanation
The window [2, 1, 1, 3] sums to 7 (within the cap) and has length 4; no longer window stays at or below 7.
Example 2
Input
1 0 5
Expected
0
Explanation
The only reading is 5, which exceeds the cap 0, so no window 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 →