Created
March 21, 2017 20:36
-
-
Save eestein/4653cc5cdc356890da03efde7ad56925 to your computer and use it in GitHub Desktop.
100% solution for codility 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) { | |
long totalSum = 0; | |
long rightSum = 0, leftSum = 0; | |
Array.ForEach(A, i => totalSum += i); | |
for (int i = 0; i < A.Length; i++) | |
{ | |
leftSum += i > 0 ? A[i-1] : 0; | |
rightSum = totalSum - (leftSum + A[i]); | |
if (rightSum == leftSum) | |
return i; | |
} | |
return -1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is what got me 100%, if you have ideas for improvements let me know :)
Array.ForEach
and long types are being used to handle larger numbers (account forOverflowException
)You can read the task description and full results here:
https://codility.com/demo/results/demoSQ5HUA-N6D/
I'm not posting it here to avoid copyright infringement.