TapeEquilibrium - codility - JavaScript
Logically, the code looks correct but from performance perspective, there are few things that can be improved. Let's look at the 2 points:  


Time Complexity: O(n) - 1 loop, 1 reduce and 1 Math.min. All run in O(n) time.
Space Complexity: O(n) - Two arrays - defArr and l which have size n


There is nothing can be done for time complexity but space complexity can be reduced to constant space. No need to have additional arrays. One can simply loop and store intermediate values in temporary variables.

I have re-written the same. Inside the loop, at each index, the difference is calculated and compared to a min variable. If the new min is lesser than existing one; then update min otherwise do next iteration.



A = []
A[0] = 3
A[1] = 1
A[2] = 2
A[3] = 4
A[4] = 3

function solution(A) {
  let sum = A.reduce((total, value) =&gt; total + value, 0)
  let min = Number.POSITIVE_INFINITY
  let cumulativeSum = 0
  for (let i = 0; i &lt; A.length - 1; ++i) {
    cumulativeSum = cumulativeSum + A[i]
    sum = sum - A[i]
    diff = Math.abs(sum - cumulativeSum)
    if (diff &lt; min) {
      min = diff
    }
  }
  return min
}

console.log(solution(A))




Hope it helps. Revert for any doubts/clarifications.