Skip to content

Instantly share code, notes, and snippets.

@kigold
Created July 27, 2016 14:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kigold/ec918836308a43511532d51739e6d0b0 to your computer and use it in GitHub Desktop.
Save kigold/ec918836308a43511532d51739e6d0b0 to your computer and use it in GitHub Desktop.
https://repl.it/Cg3f/0 created by kigold
function solution(A) {
var length = A.length;
if (length == 1){
return 0;
}
if (length < 1){
return -1;
}
if (A.slice(1).reduce((a,b) => a+b) === 0){
return 0
}
if (A.slice(0,length-1).reduce((a,b) => a+b) === 0){
return length-1
}
for (var i=1;i<length-1;i++){
//left Array
var sumL = A.slice(0,i).reduce((a,b) => a+b);
var sumU = A.slice(i+1).reduce((a,b) => a+b);
if (sumL == sumU) {
return i;
}
}
return -1;
}
//A = [-1, 3, -4, 5, 1, -6, 2, 1];
A = [1];
console.log("The Answer to this ish is: " + String(solution(A)));
Native Browser JavaScript
>>> The Answer to this ish is: 0
@kigold
Copy link
Author

kigold commented Jul 27, 2016

Need to optimize this

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