Codility is one of the most common services used to apply test codes (for job applications, for example). Here you can find a task sample to pratice before try the real test. The present sample is the Equi Task, and the propose is very simple.
Imagine an array with N elements. There is a P value (0 <= P <= N) who solve the problem below?
A + A + ... + A[P−1] = A[P+1] + ... + A[N−2] + A[N−1].
In other words, where is the equilibrium index of this array?
For example, consider the following array A consisting of N = 7 elements:
A = -7 A = 1 A = 5 A = 2 A = -4 A = 3 A = 0
P = 3 is an equilibrium index of this array, because:
A + A + A = A + A + A
The task is build one subroutine called equi who will receive the array should return the value of P, or -1 if there is no equilibrium index.
Easy? Well, there is another challenge: create a O(n) solution.
Here is my solution in Perl: