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


{\displaystyle A}

:

   




A

0


=
−
7


{\displaystyle A_{0}=-7}


   




A

1


=
1


{\displaystyle A_{1}=1}


   




A

2


=
5


{\displaystyle A_{2}=5}


   




A

3


=
2


{\displaystyle A_{3}=2}


   




A

4


=
−
4


{\displaystyle A_{4}=-4}


   




A

5


=
3


{\displaystyle A_{5}=3}


   




A

6


=
0


{\displaystyle A_{6}=0}


3   is an equilibrium index, because:

   




A

0


+

A

1


+

A

2


=

A

4


+

A

5


+

A

6




{\displaystyle 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


{\displaystyle 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


{\displaystyle A}

.



Task;
Write a function that, given a sequence, returns its equilibrium indices (if any).

Assume that the sequence may be very long.




/*REXX program calculates and displays the equilibrium index for a numeric array (list).*/
parse arg x                                      /*obtain the optional arguments from CL*/
if x=''  then x= copies(" 7 -7", 50)   7         /*Not specified?  Then use the default.*/
say '         array list: '     space(x)         /*echo the array list to the terminal. */
#= words(x)                                      /*the number of elements in the X list.*/
              do j=0  for #;   @.j= word(x, j+1) /*zero─start is for zero─based array.  */
              end   /*j*/                        /* [↑]  assign   @.0   @.1   @.3  ···  */
say                                              /*  ··· and also display a blank line. */
answer= equilibriumIDX();       w= words(answer) /*calculate the  equilibrium index.    */
say 'equilibrium'  word("(none) index: indices:",   1 + (w>0) + (w>1))        answer
exit                                             /*stick a fork in it,  we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
equilibriumIDX: $=;     do i=0  for #;           sum= 0
                                do k=0  for #;   sum= sum  +  @.k * sign(k-i);   end /*k*/
                        if sum==0  then $= $ i
                        end   /*i*/              /* [↑] Zero? Found an equilibrium index*/
                return $                         /*return equilibrium list (may be null)*/