Skip to content

Instantly share code, notes, and snippets.

@risingBirdSong
Last active December 30, 2019 19:46
Show Gist options
  • Save risingBirdSong/5f00e2b05a10f2a9fbbdd682655a8a39 to your computer and use it in GitHub Desktop.
Save risingBirdSong/5f00e2b05a10f2a9fbbdd682655a8a39 to your computer and use it in GitHub Desktop.
function findEvenIndex(arr)
{
for(var i=1; i<arr.length-1; i++) {
if(arr.slice(0, i).reduce((a, b) => a+b) === arr.slice(i+1).reduce((a, b) => a+b)) {
return i;
}
}
return -1;
}
function findEvenIndex(arr)
{
var left = 0, right = arr.reduce(function(pv, cv) { return pv + cv; }, 0);
for(var i = 0; i < arr.length; i++) {
if(i > 0) left += arr[i-1];
right -= arr[i];
if(left == right) return i;
}
return -1;
}
const sum = (a, from, to) => a.slice(from, to).reduce((a, b) => a + b, 0)
const findEvenIndex = a => a.findIndex((el, i) => sum(a, 0, i) === sum(a, i + 1));
@risingBirdSong
Copy link
Author

risingBirdSong commented Dec 30, 2019

A solution from other warriors that i like better than my solution...

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