[codility]Equi

// you can also use includes, for example:
// #include <algorithm>
int solution(const vector<int> &A) {
    // write your code in C++98
    //...calculate the sum of the array first
    long long sum = 0;
    for(int i = 0; i < A.size(); ++i)
        sum += A[i];
    //...and then traverse it forward, to find out the reqiured index
    long long leftSum = 0;
    long long rightSum = sum;
    for(int i = 0; i < A.size(); ++i)
    {
        rightSum -= A[i];
        if(leftSum == rightSum) return i;
        leftSum += A[i];
    }
    //...return result
    return -1;
}

你可能感兴趣的:([codility]Equi)