A network reliability engineer is reviewing a sequence of n latency-delta readings captured back to back from a monitoring probe. Reading i records how much the round-trip latency changed since the previous probe: a positive value means latency got worse by that many milliseconds, a negative value means it improved, and the readings are given in the fixed order they were captured.
The engineer wants to flag the largest possible number of non-overlapping (index-disjoint) contiguous runs of consecutive readings such that the deltas within each flagged run add up to exactly a given target drift value. Flagged runs never need to touch or cover every reading — any number of unflagged readings may sit between, before, or after them — but no two flagged runs may share a reading, and no reading may be reused across runs.
Determine the maximum number of such non-overlapping runs that can be flagged.
n — the number of readings.target — the required delta sum for a flagged run.n space-separated integers — the latency deltas in probe order.A single integer: the maximum number of non-overlapping contiguous runs whose deltas sum exactly to target.
1 <= n <= 100000-10000 <= reading[i] <= 100000 <= target <= 1000000Example 1
Input
5 2 1 1 1 1 1
Expected
2
Explanation
The readings are [1,1,1,1,1] and the target drift is 2. Taking indices 0-1 (sum 1+1=2) and indices 2-3 (sum 1+1=2) gives two non-overlapping runs that each sum to 2, leaving index 4 unused. No arrangement can produce a third disjoint run summing to 2 from the remaining single reading, so the maximum is 2.
Example 2
Input
6 2 -2 4 -2 4 -2 4
Expected
3
Explanation
The readings are [-2,4,-2,4,-2,4] and the target is 2. Each consecutive pair (-2,4) sums to 2: indices 0-1, indices 2-3, and indices 4-5 are three pairwise disjoint runs, each summing exactly to 2 and together covering the whole sequence with no overlap. This uses every reading in exactly one run each, giving the maximum possible count of 3.
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 →