Computing the prefix sum for all the elements in the array
let prefix = Array . from ( { length : n + 1 } , ( _ , i ) => 0 ) ; prefix [ 0 ] = arr [ 0 ] ; for ( let i = 1 ; i < n ; i ++ ) prefix [ i ] = prefix [ i - 1 ] + arr [ i ] ;