You are given an array A consisting of N integers. We define equilibrium index as a number P such as:
Example
Write a function which takes the array as argument and returns the set of equilibrium indexes the array has. (As usual, an "array" can be any ordered collection of elements you want).
Bonus points
If your code's worst case space and time complexities are both O(N).
- 0 <= P <N # A is zero-indexed
- A[0] + A[1] + ... + A[P−1] = A[P+1] + ... + A[N−2] + A[N−1]
Example
A = [9, -3, 5, -15, 4, 0, 7]
P = 3 is an equilibrium index because
>>> 9 - 3 + 5 == 4 + 0 + 7
True
P = 6 is another equilibrium index:
>>> 9 - 3 + 5 - 15 + 4 + 0 == 0 # note that 6 is the greatest index of A
True
ExerciseWrite a function which takes the array as argument and returns the set of equilibrium indexes the array has. (As usual, an "array" can be any ordered collection of elements you want).
Bonus points
If your code's worst case space and time complexities are both O(N).