Last active
April 27, 2017 11:58
-
-
Save Obre/db0476f6085a822d15b96b2e35d28583 to your computer and use it in GitHub Desktop.
This file contains 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
// 70%, bad performance | |
function solution(A) { | |
var leftSide | |
var rightSide | |
for (i = 0, arLen = A.length; i < arLen; i++) { | |
leftSide = 0; | |
rightSide = 0; | |
for (j = 0; j < i; j++) { | |
leftSide += A[j]; | |
} | |
for (k = A.length-1; k > i; k--) { | |
rightSide += A[k]; | |
} | |
if (leftSide === rightSide) { | |
return i; | |
} | |
}return -1 | |
} | |
// 100% | |
function solution(A) { | |
var rightSum = A.reduce((acc, val) => acc + val, 0), leftSum = 0; | |
for (var i = 0; i < A.length; i++) { | |
rightSum -= A[i]; | |
if (rightSum === leftSum) return i; | |
leftSum += A[i]; | |
} | |
return -1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment