Skip to content

Instantly share code, notes, and snippets.

@omarzina
Created March 15, 2016 04:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save omarzina/d5224d81f7c38eee6f1c to your computer and use it in GitHub Desktop.
Save omarzina/d5224d81f7c38eee6f1c to your computer and use it in GitHub Desktop.
100% solution to codility sample test in java - https://codility.com/demo/take-sample-test/
class Solution {
public int solution(int[] A) {
int N = A.length;
double sum = 0;
for (double i : A) {
sum += i;
}
if (N == 1) return 0;
double left = 0;
double right = sum;
for (int i = 0; i < N; i++) {
left = i == 0 ? 0 : left + A[i - 1];
right = right - A[i];
if (left == right) {
return i;
}
}
return -1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment