Skip to content

Instantly share code, notes, and snippets.

@johndavedecano
Created October 1, 2017 08:00
Show Gist options
  • Save johndavedecano/7e31fef34a01ac4958e0502c9e9c8c16 to your computer and use it in GitHub Desktop.
Save johndavedecano/7e31fef34a01ac4958e0502c9e9c8c16 to your computer and use it in GitHub Desktop.
EXAMS - Tape Equilibrium
function solution(A) {
var p, idx;
var leftSum = 0, rightSum = 0;
var totalSum = 0;
var lastMin, currentMin;
var N = A.length;
if (N == 2) { return Math.abs(A[0] - A[1]); }
if (N == 1) { return Math.abs(A[0]); }
for (idx=0; idx < N; idx++) {
totalSum = totalSum + A[idx];
}
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