Advertisement
Guest User

Untitled

a guest
Jan 27th, 2017
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.85 KB | None | 0 0
  1. // you can use includes, for example:
  2. // #include <algorithm>
  3.  
  4. // you can write to stdout for debugging purposes, e.g.
  5. // cout << "this is a debug message" << endl;
  6.  
  7. int solution(vector<int> &A) {
  8.     // write your code in C++14 (g++ 6.2.0)
  9.     signed long long sumLow = 0;
  10.     signed long long sumHigh = 0;
  11.    
  12.    
  13.     // calculate sum of higher elements for P=0
  14.     for ( unsigned int i = 1 ; i < A.size() ; i++ )
  15.     {
  16.         sumHigh += A[i];
  17.     }
  18.    
  19.     // check indices until we find an equilibrum index
  20.     for ( unsigned int p = 0 ; p < A.size() ; p++ )
  21.     {
  22.         if ( sumLow == sumHigh )
  23.         {
  24.             return p;
  25.         }
  26.        
  27.         if ( p+1 < A.size() )
  28.         {
  29.             // prepare data for next cycle
  30.             sumLow += A[p];
  31.             sumHigh -= A[p+1];
  32.         }
  33.     }
  34.    
  35.     return -1;
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement