Equilibrium index of a sequence 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.

The idea is to create a program that given a sequence (array), returns its equilibrium index (any) or -1 if no equilibrium indexes exist.
Golfscript 17 16

Since the form of the input isn't specified, this takes a string in Golfscript array format from stdin.

~0\{1$+.@+\}/])?


So run as e.g.

golfscript.ry eqindex.gs &lt;&lt;&lt;"[-7 1 5 2 -4 3 0]"


The idea is very simple: it takes an array of A_i and maps to an array of A_i + 2 SUM_{j&lt;i} A_j and then looks for the first index which is equal to the sum of the whole array.



For @mellamokb's challenge I offer:

~0\{1$+.@+\}/:S;]:A,,{A=S=},`


for 29 chars.