Sum of elements in range L | Function to find the sum between L and R ; array created ; fill the first half of array ; fill the second half of array ; find the sum between range ; Driver Code
function rangesum ( n , l , r ) { let arr = new Array ( n ) ; let c = 1 , i = 0 ; while ( c <= n ) { arr [ i ++ ] = c ; c += 2 ; } c = 2 ; while ( c <= n ) { arr [ i ++ ] = c ; c += 2 ; } let sum = 0 ; for ( i = l - 1 ; i < r ; i ++ ) { sum += arr [ i ] ; } return sum ; } let n = 12 ; let l = 1 , r = 11 ; document . write ( rangesum ( n , l , r ) ) ;