Equilibrium index

An equilibrium index of a sequence is an index into the sequence such that the sum of elements at lower indices is equal to the sum of elements at higher indices. For example, in a sequence A:
A0 = − 7
A1 = 1
A2 = 5
A3 = 2
A4 = − 4
A5 = 3
A6 = 0

so let start from A0 where before there is no value then we  will take it as 0

ok and then

A1 + A2 + A3 + A4 + A5+ A6   =1+5+2-4+3+0=8-4+3=7  

so 0 is not an equilibrium index 

next  A1  position 

A1 left side value is -7 and right side is 


A2 +A3 A4 + A5 + A6  

is
-6 so it is not equilibrium.    like that we need to check ....







3 is an equilibrium index, because:
A0 + A1 + A2 = A4 + A5 + A6
6 is also an equilibrium index, because:
A0 + A1 + A2 + A3 + A4 + A5 = 0
(sum of zero elements is zero)
7 is not an equilibrium index, because it is not a valid index of sequence A.
Write a function that, given a sequence, returns its equilibrium indices (if any). Assume that the sequence may be very long.

No comments:

Post a Comment