Latency Budget Window
An SRE wants to find the longest contiguous run of requests whose total p99 latency stayed under a hard budget. Each request is logged with its measured latency in milliseconds. The team will use the length as a leading reliability indicator.
Problem
Given an array of non-negative integers latencies and an integer budget, return the length of the longest contiguous subarray whose sum is less than or equal to budget.
Input
An integer n (1 ≤ n ≤ 2·10^5), the array latencies (0 ≤ latencies[i] ≤ 10^4), and an integer budget (0 ≤ budget ≤ 10^9).
Output
A single integer — the length of the longest subarray whose sum ≤ budget. Zero if no element fits (i.e., budget is 0 and all latencies are positive).
Constraints
- 1 ≤ n ≤ 200,000
- 0 ≤ latencies[i] ≤ 10,000
- 0 ≤ budget ≤ 10^9
- All values are integers
Examples
Example 1
Input
n = 7, latencies = [3, 1, 2, 1, 5, 1, 1], budget = 6
Output
3
The window [3, 1, 2] sums to 6 (length 3). Any length-4 window would have to include 1 more element (sum 7+) or skip into the 5, both of which exceed the budget.
Example 2
Input
n = 7, latencies = [3, 1, 2, 1, 5, 1, 1], budget = 8
Output
4
Windows summing ≤ 8 include [3,1,2,1] = 7 (length 4) and [1,5,1,1] = 8 (length 4). Longest is 4.
Example 3
Input
n = 3, latencies = [10, 20, 30], budget = 5
Output
0
No single element ≤ 5, so no window fits.