#C13146. Equilibrium Index Problem
Equilibrium Index Problem
Equilibrium Index Problem
Given an array of integers, find the equilibrium index where the sum of elements to the left of the index is equal to the sum of the elements to the right of the index. Formally, for an index i, it should satisfy the equation: $$\sum_{j=0}^{i-1} a_j = \sum_{j=i+1}^{n-1} a_j$$.
If there are multiple equilibrium indices, return the first one (i.e., the smallest index). If no such index exists, return -1
.
Note: The input is read from standard input (stdin) and the output should be printed to standard output (stdout).
inputFormat
The first line of input contains a single integer n
representing the number of elements in the array. The second line contains n
space-separated integers which are the elements of the array.
outputFormat
Output a single integer representing the equilibrium index if one exists. If no equilibrium index exists, print -1
.
5
1 3 5 2 2
2