Range sum queries without updates | 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 ;
using System ; class GFG { public static void preCompute ( int [ ] arr , int n , int [ ] pre ) { pre [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) pre [ i ] = arr [ i ] + pre [ i - 1 ] ; } public static int rangeSum ( int i , int j , int [ ] pre ) { if ( i == 0 ) return pre [ j ] ; return pre [ j ] - pre [ i - 1 ] ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 } ; int n = arr . Length ; int [ ] pre = new int [ n ] ; preCompute ( arr , n , pre ) ; Console . WriteLine ( rangeSum ( 1 , 3 , pre ) ) ; Console . WriteLine ( rangeSum ( 2 , 4 , pre ) ) ; } } \