Skip to content

Instantly share code, notes, and snippets.

@ViliusKraujutis
Last active April 22, 2016 19:20
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save ViliusKraujutis/98454e6e460fa9fa48a8714a497a1960 to your computer and use it in GitHub Desktop.
Codility solution https://codility.com/demo/results/demoGYMPA6-Q73/ Take your own here: https://codility.com/demo/take-sample-test/ This is a demo task. You can read about this task and its solutions in this blog post: http://dev.tasubo.com/2012/09/tips-for-tasks-on-codility.html
class Solution {
public int solution(int[] A) {
long left = 0;
long right = 0;
long sum = sum(A);
for (int i = 0; i < A.length; i++) {
right = sum - left - A[i];
if (left == right)
return i;
left += A[i];
}
return -1;
}
private long sum(int[] A) {
long sum = 0;
for (int i = 0; i < A.length; i++) {
sum += A[i];
}
return sum;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment