Skip to content

Instantly share code, notes, and snippets.

@eestein
Created March 21, 2017 20:36
Show Gist options
  • Save eestein/4653cc5cdc356890da03efde7ad56925 to your computer and use it in GitHub Desktop.
Save eestein/4653cc5cdc356890da03efde7ad56925 to your computer and use it in GitHub Desktop.
100% solution for codility sample test
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;
}
}
@eestein
Copy link
Author

eestein commented Mar 21, 2017

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 for OverflowException)

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment