Equilibrium Index
easyPlacement Coding+10 XPTime limit 2s
An index i is an equilibrium index if the sum of the elements before it equals the sum of the elements after it (the element at i itself is not counted on either side). An empty side has sum 0.
In [-7, 1, 5, 2, -4, 3, 0], index 3 is an equilibrium index: −7 + 1 + 5 = −4 + 3 + 0 = −1.
Input:
- line 1:
n - line 2:
nintegers
Output: the smallest equilibrium index (0-based), or -1 if there is none.
Constraints: 1 ≤ n ≤ 10⁵, −10⁴ ≤ values ≤ 10⁴.
Think about it: compute the total once. Walking left to right, keep the running leftSum; then rightSum = total − leftSum − a[i]. That makes it O(n) instead of O(n²).
Sample input 1
7 -7 1 5 2 -4 3 0
Sample output 1
3
Sample input 2
3 1 2 3
Sample output 2
-1
Read input from standard input and print the answer to standard output. Your code runs in a secure sandbox against the samples (Run) and hidden tests (Submit).