Skip to content

Instantly share code, notes, and snippets.

@gartenfeld
Last active October 3, 2015 21:49
Show Gist options
  • Save gartenfeld/f91aa62ffab86a08dbef to your computer and use it in GitHub Desktop.
Save gartenfeld/f91aa62ffab86a08dbef to your computer and use it in GitHub Desktop.
An alternative to .push()
function solution(A) {
var n = A.length - 1,
lSum = 0,
rSum = 0,
diffs = new Array(n),
diff,
min = Infinity;
for (var p = 0; p < n; p++) {
lSum += A[p];
rSum += A[n - p];
diffs[p] = diffs[p] || 0;
diffs[n - p - 1] = diffs[n - p - 1] || 0;
diffs[p] += lSum;
diffs[n - p - 1] -= rSum;
}
for (var i = 0; i < diffs.length; i++) {
diff = Math.abs(diffs[i]);
min = diff < min ? diff : min;
}
return min;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment