RosettaCodeData/Task/Equilibrium-index/00DESCRIPTION

32 lines
1.1 KiB
Text
Raw Permalink Normal View History

2016-12-05 22:15:40 +01:00
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.
2013-04-10 16:57:12 -07:00
2016-12-05 22:15:40 +01:00
For example, in a sequence &nbsp; <big><math>A</math></big>:
2013-04-10 16:57:12 -07:00
2016-12-05 22:15:40 +01:00
::::: &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>
2013-04-10 16:57:12 -07:00
2016-12-05 22:15:40 +01:00
3 &nbsp; is an equilibrium index, because:
2013-04-10 16:57:12 -07:00
2016-12-05 22:15:40 +01:00
::::: &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>
2013-04-10 16:57:12 -07:00
(sum of zero elements is zero)
2016-12-05 22:15:40 +01:00
7 &nbsp; is not an equilibrium index, because it is not a valid index of sequence <big><math>A</math></big>.
2013-04-10 16:57:12 -07:00
2016-12-05 22:15:40 +01:00
;Task;
2013-04-10 16:57:12 -07:00
Write a function that, given a sequence, returns its equilibrium indices (if any).
2016-12-05 22:15:40 +01:00
2013-04-10 16:57:12 -07:00
Assume that the sequence may be very long.
2016-12-05 22:15:40 +01:00
<br><br>