#K93597. Finding the Equilibrium Index
Finding the Equilibrium Index
Finding the Equilibrium Index
Given an array of integers, your task is to determine the equilibrium index of the array. An equilibrium index is defined as an index (i) such that the sum of elements at indices less than (i) is equal to the sum of elements at indices greater than (i). Formally, find an index (i) satisfying
[ \sum_{j=0}^{i-1} a_j = \sum_{j=i+1}^{n-1} a_j ]
If such an index exists, output the first (smallest) equilibrium index (using 0-based indexing). If no such index exists, output -1.
Note: The input is provided through standard input and the output should be written to standard output.
inputFormat
The input will be given via standard input as follows:
- The first line contains an integer \(n\), the number of elements in the array.
- The second line contains \(n\) space-separated integers representing the array elements.
outputFormat
Output a single integer: the equilibrium index if it exists (0-based); otherwise, output -1.## sample
7
-7 1 5 2 -4 3 0
3
</p>