Skip to content

Instantly share code, notes, and snippets.

@vys
Created March 9, 2010 20:27
Show Gist options
  • Save vys/327081 to your computer and use it in GitHub Desktop.
Save vys/327081 to your computer and use it in GitHub Desktop.
// This solution scored 100/100
int equi ( const vector<int> &A ) {
// write your code here
int size = A.size();
if (size == 0) return -1;
long long *c_left = new long long[size];
c_left[0] = A[0];
for(int i = 1; i < size; i++) {
c_left[i] = c_left[i-1] + A[i];
}
long long c_right = c_left[size-1]-A[0];
if (c_right == 0) return 0;
for (int k = 1; k < size; k++) {
c_right = c_right - A[k];
if (c_left[k-1] == c_right) return k;
}
return -1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment