Skip to content

Instantly share code, notes, and snippets.

@abhishekbedi1432
Forked from mourner/codility-demo.js
Created July 18, 2017 11:00
Show Gist options
  • Save abhishekbedi1432/6601ca6540b529551f261eccb70eeb81 to your computer and use it in GitHub Desktop.
Save abhishekbedi1432/6601ca6540b529551f261eccb70eeb81 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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment