1. JavaScript
function solution(A) {
const N = A.length;
const totalSum = A.reduce((a, b) => a + b);
let leftSum = A[0];
let rightSum = totalSum - A[0];
let minDiff = Number.MAX_SAFE_INTEGER;
let diff;
for(let P=1; P<N; P++){
diff = Math.abs(leftSum - rightSum);
if(diff < minDiff){
minDiff = diff;
}
leftSum += A[P];
rightSum -= A[P];
}
return minDiff;
}
2. Python
def solution(A):
sum_right = sum(A)
sum_left = 0
result = None
for i in range(len(A)-1):
sum_right -= A[i]
sum_left += A[i]
if result == None:
result = abs(sum_right - sum_left)
else:
result = min(result, abs(sum_right - sum_left))
return result