A zero-indexed array A consisting of N integers is given. An equilibrium index of this array is any integer P such that 0 ≤ P < N and the sum of elements of lower indices is equal to the sum of elements of higher indices, i.e.

    A[0] + A[1] + ... + A[P−1] = A[P+1] + ... + A[N−2] + A[N−1].

Sum of zero elements is assumed to be equal to 0. This can happen if P = 0 or if P = N−1.
int solution(int A[], int N) {
    // return 3 for [3,2,-5,1]
    int sum_prefix = 0;
    int sum_suffix = 0;
int i, j;
for (i = 0; i < N; i++) {
    // Check if is is the equilibrium point
    sum_prefix = 0;
    for (j = 0; j < i; j++) {
       sum_prefix += A[j];
    }
    sum_suffix = 0;
    for (j = i+1; j < N; j++) {
        sum_suffix += A[j];
       // Compute suffix sum
    }
    if (sum_prefix == sum_suffix)
        return i;
}
return -1;
}

3 thoughts on “”

  1. Taxi Utrecht

    Taxi Utrecht is een jonge en dynamische taxicentrale in Utrecht.
    Wij zijn gespecialiseerd in het aanbieden van op maat gemaakte services op het gebied van taxivervoer in en rondom Utrecht.

    Taxi Utrecht

  2. There’s something to it, and it’s a good idea. I am ready to support you.
    China and France participate in expressed non-fulfilment with the rejuvenated defense concordat between the Merged States, the United Sovereignty and Australia – although for personal reasons. Beijing in effect wants to be with the Asia-Pacific Employment Pact.
    More newsflash here site

Leave a Reply

Your email address will not be published.