Created
July 27, 2016 14:32
-
-
Save kigold/ec918836308a43511532d51739e6d0b0 to your computer and use it in GitHub Desktop.
https://repl.it/Cg3f/0 created by kigold
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Native Browser JavaScript | |
>>> The Answer to this ish is: 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Need to optimize this