# 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 <<<"[-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<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.

• Since you easily have the shortest solution, I proclaim you must return all of the indexes, not just the first one :) – mellamokb May 4 '11 at 17:30
• @mellamokb, with my compliments. – Peter Taylor May 4 '11 at 22:29
• Cool! Now I've got some more GolfScript learning to do... – mellamokb May 5 '11 at 2:27

## Python - 72 chars

A=input()
print[i for i in range(len(A))if sum(A[:i])==sum(A[i+1:])]or-1


Takes comma separated input

• Awesome... this one returns all the equilibrium indexes... really cool. – Cristian May 4 '11 at 13:58
• @Christian: Mine does so, too. – FUZxxl May 4 '11 at 18:29
• I see :) I actually don't know how to run haskell code... will have to study. – Cristian May 5 '11 at 4:08
• Christian: There is ghc, a compiler and hugs, an interpreter. I'd suggest downloading hugs. It's better then downloading ghc, because hugs is about 7 MiB, while the whole ghc distribution is about 300 MiB. Using hugs, you can just type runhugs FILE.hs to run program FILE.hs. – FUZxxl May 6 '11 at 13:03

e l=[n|n<-[0..length l-1],sum(take n l)==sum(drop(n+1)l)]
main=interact$show.e.read  Reads a list in Haskell style from stdin, eg. [-7,1,5,2,-4,3,0]  and returns a Haskell style list of the indices, eg. [3,6]  The result is [], if there is no index. Please tell me, if your spec wants a different behavior. ### Edits: • (95 → 83): list comprehension is more breve ## C - 96 a[99],*p=a,s;main(){for(;scanf("%d",p)>0;s+=*p++ );for(;p>a;s-=*p)(s-=*--p)||printf("%d\n",p-a);}  Note that this prints the equilibrium indices in reverse order. Sample usage: $ ./equilibrium <<< "-7 1 5 2 -4 3 0"
6
3


## Ruby (83 77)

a=*$<.map(&:to_i) p (0...a.size).select{|x|a[0..x].reduce(:+)==a[x..-1].reduce(:+)}  Edit: Shorter version as suggested by Ventero: a=$<.map &:to_i
p (0...a.size).select{|x|eval"#{a[0..x]*?+}==#{a[x..-1]*?+}"}


Input is one number per line, output is comma separated list of indexes in square brackets.

• You don't need the parentheses in the first line, and you can save a few chars by using join + eval to get the sums: p (0...a.size).select{|x|eval"#{a[0..x]*?+}==#{a[x..-1]*?+}"} (note that this is for Ruby 1.9, since it uses character literals as strings) – Ventero May 9 '11 at 17:57
• Great suggestions, thanks! Kind of annoying that Array#sum isn't in Ruby core. – Lars Haugseth May 9 '11 at 20:48