Given n ledger entries, count the number of ways to split the sequence into a non-empty prefix (the first i entries, for some 1 <= i <= n-1) and the remaining non-empty suffix (the last n-i entries), such that the sum of the prefix equals the sum of the suffix. Print the total count of such split positions i.
If n < 2 there is no valid split, so the answer is 0.
Line 1: an integer n.
Line 2: n space-separated integers (present only if n >= 1).
A single integer: the number of split positions with equal prefix and suffix sums.
Example 1
Input
5 2 0 0 0 2
Expected
4
Explanation
Every split from i=1 to i=4 gives left sum 2 and right sum 2 (the zeros in the middle don't change either side), so all 4 splits qualify.
Example 2
Input
3 1 2 3
Expected
1
Explanation
Only the split after i=2 works: left=1+2=3, right=3. The split after i=1 gives left=1, right=5, which don't match.
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 →