Range sum queries without updates | JavaScript Program to find sum between two indexes when there is no update . ; Returns sum of elements in arr [ i . . j ] It is assumed that i <= j ; Driver code ; Function call
let pre = new Array ( 1000 , 0 ) ; function preCompute ( arr , n ) { pre [ 0 ] = arr [ 0 ] ; for ( let i = 1 ; i < n ; i ++ ) pre [ i ] = arr [ i ] + pre [ i - 1 ] ; } function rangeSum ( i , j , pre ) { if ( i == 0 ) return pre [ j ] ; return pre [ j ] - pre [ i - 1 ] ; } let arr = [ 1 , 2 , 3 , 4 , 5 ] ; let n = arr . length ; preCompute ( arr , n ) ; document . write ( rangeSum ( 1 , 3 , pre ) + " " ) ; document . write ( rangeSum ( 2 , 4 , pre ) ) ;