I did a sample on Codility and finished the test task.
The problem description is very short:
The 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=-7, A=1, A=5 ,A=2, A=-4, A=3, A=0$$
\$3\$ is an equilibrium index, because:
\$6\$ is also an equilibrium index, because:
(The 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, here is a precise definition: The integer \$k\$ is an equilibrium index of a sequence \$A,A\dots,A[n-1]\$ if and only if \$0\le k\$ and \$\sum(A[0\dots(k-1)])=\sum(A[(k+1)\dots(n-1)])\$. Assume the sum of zero elements is equal to zero.
The code I wrote gives the correct result, but, I only scored 8% on correctness and a 54% on performance. Here is my code:
Can somebody explain why my code scored so low? And what to do to improve?