function solution(A) {
let sumAll = A.reduce((accu, curr) => accu + curr, 0); // sum of the array
let minDiff = Number.MAX_VALUE;

for (let i = 0, sumPart = 0, diff, n = A.length - 1; i < n; i++) {
sumPart += A[i];
// |sum(0~P-1) - sum(P~N-1)| equals to |2*sum(0~P-1) - sum(0~N-1)|
diff = Math.abs(sumPart * 2 - sumAll);
if (diff < minDiff) {
minDiff = diff;
}
}

return minDiff;
}


'IT General' 카테고리의 다른 글

Codility #10-FrogRiverOne  (0) 2018.09.18
Codility #9-PermCheck  (0) 2018.09.18
Codility #7-Brackets  (0) 2018.08.18
Codility #7-Nesting  (0) 2018.08.18
Codility #3-PermMissingElem  (0) 2018.08.18

+ Recent posts