Problem 40
Pivot Index
Easy · 10 pts · Array, Prefix Sum
Problem
The pivot index of an array is a position where the sum of all elements strictly to its left equals the sum of all elements strictly to its right. For the first position the left sum is 0, and for the last position the right sum is 0. Print the leftmost pivot index (zero-based), or -1 if no position works. Compute the total sum once; then, walking from left to right with a running left sum, the right sum at index i is total - leftSum - A[i].
Input
First line: integer N. Second line: N space-separated integers.
Output
Print the leftmost pivot index, or -1.
Constraints
1 <= N <= 100000; -1000 <= A[i] <= 1000
Example input
6 1 7 3 6 5 6
Example output
3
Explanation
In 1 7 3 6 5 6, at index 3 the left side is 1 + 7 + 3 = 11 and the right side is 5 + 6 = 11. No earlier index balances, so the answer is 3.