Prefix sums + frequency map
Count Balanced Shipment Runs
01. The question
A depot records signed stock changes: arrivals are positive and departures are negative. Count every nonempty contiguous run whose total equals the requested change. Overlapping runs count separately.
Before revealing the idea, predict the result and describe what information you would need to track.
02. Worked example
Inputchanges = [2, -2, 2, 0], target = 2
Output5
- Prefix totals, including the empty prefix, are 0, 2, 0, 2, 2.
- Each current prefix needs an earlier prefix equal to current - target.
- The three prefixes equal to 2 find 1, 2, and 2 earlier zero prefixes. That gives 5 runs.
Try your code
Define solve(data, parameter="") and return your result. Your code runs locally, not on a server.
Edit your test cases
Use 1-10 objects with data and an optional string parameter. Expected outputs are computed from the lesson. An optional expected field must be correct.
Input contract & reference code
Example data: [2,-2,2,0]. Parameter: "2". Numeric parameters arrive as strings; convert them with int(parameter) when needed.
The example and generated cases use the question’s exact variant. This checks behavior on a small test batch—not performance on every possible input.
Python downloads only when you run code, using compressed runtime assets. Code is not saved or uploaded. Enable JavaScript to run it.
Printed output
Five-second execution limit per batch. Standard-library Python only; additional packages and network access are unavailable. Stop ends execution without blocking the lesson.
03. The idea
Try sketching your own approach first. What would you track, and why would it work?
Reveal the idea
- Begin with one occurrence of prefix total 0, representing the empty prefix.
- Add the current change. Count all earlier prefixes equal to prefix - target.
- Only then remember this prefix, so empty runs are never counted.
04. Watch it happen
Use Step to move slowly, or Play to follow the whole trace. Change a preset or load your own input.
Input & settings
Red = current item / blue = active window or cells / green = completed work. A value docks at its actual destination; the original input stays unchanged. Positions start at 0.
Working state / what changes
Ready to begin
Enable JavaScript to step through the animation. The worked example and solution below remain readable without it.
Python / why it changes
The line behind the step
def balanced_runs(changes, target):
frequencies = {0: 1}
prefix = count = 0
for change in changes:
prefix += change
count += frequencies.get(prefix - target, 0)
frequencies[prefix] = frequencies.get(prefix, 0) + 1
return countHow to use this walkthrough
Hover or focus a control for its label. Play forms the input and follows values into the working state. Step shows one change; Back and the slider restore exact snapshots. Reduced motion uses instant updates. Saved snapshots and sorted heap displays are teaching overhead, not algorithm complexity.
05. Python solution
def balanced_runs(changes, target):
frequencies = {0: 1}
prefix = count = 0
for change in changes:
prefix += change
count += frequencies.get(prefix - target, 0)
frequencies[prefix] = frequencies.get(prefix, 0) + 1
return count06. Cost & edge cases
- Time
- O(n) expected
- Space
- O(n)
- Empty input returns 0.
- Zeros and negative changes are allowed; a positive-only shrinking window is not suitable.
- For [0, 0] and target 0, count 3 nonempty runs, not 2.