Equilibrium index

From Fōrmulæ wiki
Jump to: navigation, search

This page is the answer to the task Equilibrium index in the Rosetta Code.

Description (from Rosetta Code)

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 :

3 is an equilibrium index, because:

6 is also an equilibrium index, because:

(sum of zero elements is zero)

7 is not an equilibrium index, because it is not a valid index of sequence .

Task

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

Assume that the sequence may be very long.

Program

EquilibriumIndexCode.png

Case 1.

In Fōrmulæ, indexes are 1-based so the output of this program will be shifted up by one compared to solutions in languages with 0-based arrays.

EquilibriumIndexOutput1.png

Case 2.

EquilibriumIndexOutput2.png