Created
April 24, 2016 19:00
-
-
Save peterschmiz/2d99cb885fa2c03192ab853cc8dffc0f 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
function solution(A) { | |
var p, idx, | |
leftSum = 0, | |
rightSum = 0, | |
totalSum = A.reduce(function(pv, cv) { return pv + cv; }, 0), | |
lastMin, | |
currentMin, | |
N = A.length; | |
if (N === 2) { | |
return Math.abs(A[0] - A[1]); | |
} | |
if (N === 1) { | |
return Math.abs(A[0]); | |
} | |
lastMin = Math.abs(totalSum - A[0] - A[0]); | |
for (p = 1; p <= N - 1; p++) { | |
leftSum += A[p - 1]; | |
rightSum = totalSum - leftSum; | |
currentMin = Math.abs(leftSum - rightSum); | |
lastMin = (currentMin < lastMin) ? currentMin : lastMin; | |
} | |
return lastMin; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment