The UptoSkills Courier
The UptoSkills Courier service has to deliver packages across the city. You are given the delivery times for n consecutive tasks, represented by an array t. The courier starts at task 0 and must complete tasks 0 to n-1 in order. A single courier can handle a batch of tasks if they are consecutive and the total time for the batch does not exceed a given limit k. Your goal is to find the maximum number of tasks a courier can deliver in a single batch, starting from task 0. That is, you want to find the largest L such that there exists a subarray t[0...L-1] whose sum is at most k.
Input
The first line contains two integers n and k (1 <= n <= 200000, 0 <= k <= 10^14). The second line contains n integers representing the delivery times t_i (0 <= t_i <= 10^9).
Output
Print a single integer: the maximum number of tasks L that can be delivered in a single batch starting from task 0.
Constraints
1 <= n <= 200000 0 <= k <= 10^14 0 <= t_i <= 10^9
Example 1
5 10 3 2 1 5 4
3
The prefix sums are [0, 3, 5, 6, 11, 15]. The largest prefix sum <= 10 is 6 (for L=4). So, max L is 4.
Example 2
3 0 1 1 1
0
With k=0, only an empty batch (L=0) is possible if any delivery time is positive.
Hints
Similar problems
- Maximum Subarray Sum with Fixed LengthMedium
- Find Minimums in Sliding WindowsMedium
- Sliding Window MinimumMedium
- Longest Prefix With Maximum FrequencyMedium
- Largest Element in PrefixMedium
- Longest Subarray with Sum at Most KMedium
More topics: Sliding Window problems, Queue and Deque problems
# Read the input from stdin and print the answer.
import sys
def main():
data = sys.stdin.read().split()
# your code here
main()
5 10 3 2 1 5 4
3 0 1 1 1