A diver begins a dive exactly at the surface, which is treated as depth 0. Over the course of the dive, the dive computer records n consecutive depth-change readings in chronological order. The i-th reading is a nonzero signed integer: a positive value means the diver descended (moved deeper) by that many meters since the previous reading, and a negative value means the diver ascended (moved toward the surface) by that many meters.
After each reading, the diver's cumulative depth is the sum of all readings so far. Count how many of the n readings leave the diver's cumulative depth exactly equal to 0 (i.e. exactly back at the surface) at that moment. The very start of the dive, before any readings are taken, is also at depth 0 but must not be counted.
Line 1: a single integer n, the number of readings.
Line 2: n space-separated nonzero integers -- the depth-change readings, in chronological order.
Print a single integer: the number of readings after which the cumulative depth is exactly 0.
1 <= n <= 100000-10000 <= each reading <= 10000, and no reading is 0.Example 1
Input
5 2 3 -5 5 -3
Expected
1
Explanation
Running totals after each reading: 2, 5, 0, 5, 2. Exactly one of these totals (after the third reading) is 0, so the answer is 1.
Example 2
Input
3 3 2 -3
Expected
0
Explanation
Running totals after each reading: 3, 5, 2. None of these equal 0, so the answer 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 →