Dsa
Prefix Sum
Warm-up
Balanced Cargo Belt
A conveyor belt carries cargo items. A sensor records the weight difference between the left and right side of the belt for each of N items. If the sum of weight differences from the beginning of the belt up to item i is exactly zero, the belt is perfectly 'balanced' at that moment. Count how many times the belt was balanced during the processing of N items.
Input Format:
The first line contains an integer N.
The second line contains N integers representing the (Left - Right) weight difference for each item.
Output Format:
A single integer representing the count of balanced moments.
Example:
Input:
6
1 -1 2 -2 0 5
Output:
3
(Explanation: Cumulative sums: 1, 0, 2, 0, 0, 5. The zeros occur at indices 1, 3, and 4. Total = 3.)
Key concepts
prefix_sumcounting
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now