Equilibrium index
807597Oct 26 2005 — edited Oct 26 2005Any help please ?
Equilibrium index
Equilibrium index of a sequence is an index such that the sum of elements at lower indexes is equal to the sum of elements at higher indexes. For example, in a sequence A:
A[0]=-7 A[1]=1 A[2]=5 A[3]=2 A[4]=-4 A[5]=3 A[6]=0
3 is an equilibrium index, because : :
A[0] + A[1] + A[2] = A[4] + A[5] + A[6]
6 is also an equilibrium index, because:
A[0] + A[1] + A[2] + A[3] + A[4] + A[5] = 0 (sum of zero elements is zero)
7 is not an equilibrium index, because it is not a valid index of sequence A.
If you still have doubts, this is a precise definition: the integer k is an equilibrium index of a sequence A[0],A[1],...,A[n] if and only if 0≤k≤ n and . We assume that sum of zero elements is equal zero.
Write a function, that given a sequence, returns its equilibrium index (any) or -1 if no equilibrium indexes exist. Assume that the sequence may be very long. If possible, use one of the following languages and define your function as follows:
Java static public int equi(int a[]) // in class Equi