Task description
Equilibrium index of a sequence is an index such that the sum of elements at lower indexes is equal to the sum of elements at higher indexes. For example, in a sequence A:
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 A.
If you still have doubts, this is a precise definition: the integer k is an equilibrium index of a sequence if and only if and.
Assume the sum of zero elements is equal zero. Write a function
int equi(int[] A);
that given a sequence, returns its equilibrium index (any) or -1 if no equilibrium indexes exist. Assume that the sequence may be very long.
O(n)
test | time | result |
---|---|---|
example Test from the task description | 0.001 s. | OK |
extreme_empty Empty array | 0.004 s. | OK |
extreme_first | 0.004 s. | OK |
extreme_large_numbers Sequence with extremly large numbers testing arithmetic overflow. | 0.004 s. | OK |
extreme_last | 0.004 s. | OK |
extreme_single_zero | 0.004 s. | OK |
extreme_sum_0 sequence with sum=0 | 0.001 s. | OK |
simple | 0.001 s. | OK |
single_non_zero | 0.004 s. | OK |
combinations_of_two multiple runs, all combinations of {-1,0,1}^2 | 0.001 s. | OK |
combinations_of_three multiple runs, all combinations of {-1,0,1}^3 | 0.001 s. | OK |
small_pyramid | 0.004 s. | OK |
large_long_sequence_of_ones | 0.008 s. | OK |
large_long_sequence_of_minus_ones | 0.020 s. | OK |
medium_pyramid | 0.004 s. | OK |
large_pyramid Large performance test, O(n^2) solutions should fail. | 0.052 s. | OK |