Skip to content

Instantly share code, notes, and snippets.

@stowball
Created December 19, 2015 12:15
Show Gist options
  • Select an option

  • Save stowball/a3ab5678f37e00b95f3d to your computer and use it in GitHub Desktop.

Select an option

Save stowball/a3ab5678f37e00b95f3d to your computer and use it in GitHub Desktop.
function equilibrium(numberArray) {
var lower, higher, equilibriums = [];
var lowerSum, higherSum;
function sum(arrayParts) {
return arrayParts.reduce(function (prev, curr) {
return prev + curr;
}, 0);
}
for (var i = 1; i < numberArray.length - 1; i++) {
lower = numberArray.slice(0, i - 1);
higher = numberArray.slice(i);
lowerSum = sum(lower);
higherSum = sum(higher);
if (lowerSum === higherSum) {
equilibriums.push(i + 1);
}
}
if (!equilibriums) {
return -1;
} else {
return equilibriums;
}
}
equilibrium([-1, 3, -4, 5, 1, -6, 2, 1]);
// Should return [3,5] as 0+1+2 === 4+5+6+7 the same for 5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment