Skip to content

Instantly share code, notes, and snippets.

@mourner
Created January 7, 2012 09:49
Show Gist options
  • Save mourner/1574304 to your computer and use it in GitHub Desktop.
Save mourner/1574304 to your computer and use it in GitHub Desktop.
Codility demo tests solutions
// 100-score solution for http://codility.com/demo/take-sample-test/
function equi(array) {
var i,
len = array.length,
sum = 0,
leftSum = 0,
rightSum;
for (i = 0; i < len; i++) {
sum += array[i];
}
for (i = 0; i < len; i++) {
rightSum = sum - leftSum - array[i];
if (leftSum === rightSum) {
return i;
}
leftSum += array[i];
}
return -1;
}
// 100-score solution for http://codility.com/demo/take-sample-test/ps/
function ps(array) {
var i,
len = array.length,
memo = {},
result = 0,
value;
for (i = 0; i < len; i++) {
value = array[i];
if (!memo[value]) {
result = i;
memo[value] = true;
}
}
return result;
}
@quinncnl
Copy link

it's outdated

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