Last active
April 22, 2016 19:20
-
-
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
This file contains hidden or 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
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