The pivot index is an index i such that the sum of the elements strictly to the left of i equals the sum of the elements strictly to the right of i. Elements to the side of an empty range sum to 0.
Return the leftmost pivot index, or -1 if none exists.
Line 1: an integer n.
Line 2: n space-separated integers (present whenever n >= 1).
One line: the leftmost pivot index (0-based), or -1.
Example 1
Input
6 1 7 3 6 5 6
Expected
3
Explanation
At index 3 the left sum 1+7+3=11 equals the right sum 5+6=11. No smaller index balances the two sides.
Example 2
Input
3 1 2 3
Expected
-1
Explanation
No index balances the two sides, so the answer is -1.
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 →