A ship's hull has n ballast tanks in a row, each holding an integer load (which may be negative to model a buoyancy correction). An index i (0-indexed) is called a balance point if the total load of all tanks strictly to the left of i equals the total load of all tanks strictly to the right of i. The tank at i itself is not counted on either side. For the first index the left side is empty (sum 0); for the last index the right side is empty (sum 0).
Count how many balance points the row has.
Line 1: an integer n.
Line 2: n space-separated integers, the tank loads from left to right.
A single integer: the number of balance points.
Example 1
Input
7 -7 1 5 2 -4 3 0
Expected
2
Explanation
At index 3 the left sum is -7+1+5 = -1 and the right sum is -4+3+0 = -1. At index 6 the left sum is -7+1+5+2-4+3 = 0 and the right side is empty (0). Both match, so there are 2 balance points.
Example 2
Input
3 1 2 3
Expected
0
Explanation
No index splits the row into two equal-sum sides, so the count is 0.
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 →