Created
June 12, 2017 18:38
-
-
Save BeKnowDo/210bdf3a377077b839b55f4de38eff99 to your computer and use it in GitHub Desktop.
Equilibrium Index
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
// you can write to stdout for debugging purposes, e.g. | |
// console.log('this is a debug message'); | |
function solution(A) { | |
let sum = null, | |
previous_sum = 0, | |
next_sum = null; | |
if(!A || A.length === 0) { | |
return -1; | |
} else if(A.lengh === 1) { | |
sum = A[0]; | |
} else { | |
sum = A.reduce((total, num)=>{ | |
return total + num; | |
}) | |
} | |
for(let i = 0; i < A.length; i++) { | |
next_sum = sum - previous_sum - A[i]; | |
if(previous_sum === next_sum) { | |
return i; | |
} | |
previous_sum += A[i]; | |
} | |
return -1; | |
} | |
solution([-7, 1, 5, 2, -4, 3, 0]); | |
document.querySelector('#result').innerHTML = solution([-7, 1, 5, 2, -4, 3, 0]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment