A treasury ledger records n daily net movements (credits positive, debits negative). An auditor wants to know how many contiguous runs of days have a net total that lands inside an inclusive band [lo, hi].
Count the contiguous runs (subarrays) whose sum S satisfies lo <= S <= hi. The intended approach builds prefix sums and, during a merge sort of those prefix sums, counts qualifying pairs whose difference lies in the band.
Line 1: three integers n, lo, hi (with lo <= hi).
Line 2: n space-separated integers, the daily net movements.
A single integer: the number of contiguous runs whose sum lies in [lo, hi].
Example 1
Input
4 1 3 1 -1 2 3
Expected
5
Explanation
The runs with sum in [1,3] are [1], [1,-1,2], [-1,2], [2] and [3]: five runs.
Example 2
Input
3 0 0 2 -2 2
Expected
2
Explanation
Only [2,-2] and [-2,2] have sum exactly 0, so the count is 2.
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 →