Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // you can use includes, for example:
- // #include <algorithm>
- // you can write to stdout for debugging purposes, e.g.
- // cout << "this is a debug message" << endl;
- int solution(vector<int> &A) {
- // write your code in C++14 (g++ 6.2.0)
- signed long long sumLow = 0;
- signed long long sumHigh = 0;
- // calculate sum of higher elements for P=0
- for ( unsigned int i = 1 ; i < A.size() ; i++ )
- {
- sumHigh += A[i];
- }
- // check indices until we find an equilibrum index
- for ( unsigned int p = 0 ; p < A.size() ; p++ )
- {
- if ( sumLow == sumHigh )
- {
- return p;
- }
- if ( p+1 < A.size() )
- {
- // prepare data for next cycle
- sumLow += A[p];
- sumHigh -= A[p+1];
- }
- }
- return -1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement