Problems > Longest Affordable Streak

Longest Affordable Streak

Difficulty: 1400 3000 ms 128 MB

sliding-window two-pointers

Item prices are positive. Find the longest contiguous run of items whose total price does not exceed the budget K (possibly length 0).

Input: the first line has N and K; the second line has N positive integers.

Output: the maximum length of a contiguous run with sum ≤ K.

Constraints:

  • 1 ≤ N ≤ 105
  • 1 ≤ ai ≤ 104
  • 1 ≤ K ≤ 109

Sample Tests

Input 1
6 8
3 1 2 5 1 1
Output 1
3
Input 2
3 1
5 6 7
Output 2
0

Read the Editorial

Discussion (opens after you solve — no spoilers before)

No comments yet.

Submit Your Solution

Log in or create a free account to run and submit code.