Created
March 9, 2010 20:27
-
-
Save vys/327081 to your computer and use it in GitHub Desktop.
Perfect solution to http://codility.com/demo/take-sample-test/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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