Created
March 15, 2016 04:20
-
-
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/
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
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