RosettaCodeData/Task/Equilibrium-index/00-TASK.txt
2023-07-01 13:44:08 -04:00

32 lines
1.1 KiB
Text

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 &nbsp; <big><math>A</math></big>:
::::: &nbsp; <big><math>A_0 = -7</math></big>
::::: &nbsp; <big><math>A_1 = 1</math></big>
::::: &nbsp; <big><math>A_2 = 5</math></big>
::::: &nbsp; <big><math>A_3 = 2</math></big>
::::: &nbsp; <big><math>A_4 = -4</math></big>
::::: &nbsp; <big><math>A_5 = 3</math></big>
::::: &nbsp; <big><math>A_6 = 0</math></big>
3 &nbsp; is an equilibrium index, because:
::::: &nbsp; <big><math>A_0 + A_1 + A_2 = A_4 + A_5 + A_6</math></big>
6 &nbsp; is also an equilibrium index, because:
::::: &nbsp; <big><math>A_0 + A_1 + A_2 + A_3 + A_4 + A_5 = 0</math></big>
(sum of zero elements is zero)
7 &nbsp; is not an equilibrium index, because it is not a valid index of sequence <big><math>A</math></big>.
;Task;
Write a function that, given a sequence, returns its equilibrium indices (if any).
Assume that the sequence may be very long.
<br><br>