Skip to content

Instantly share code, notes, and snippets.

@Obre
Last active April 27, 2017 11:58
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 Obre/db0476f6085a822d15b96b2e35d28583 to your computer and use it in GitHub Desktop.
Save Obre/db0476f6085a822d15b96b2e35d28583 to your computer and use it in GitHub Desktop.
// 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