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
< ? php function rangesum ( $ n , $ l , $ r ) { $ arr = array_fill ( 0 , $ n , 0 ) ; $ c = 1 ; $ i = 0 ; while ( $ c <= $ n ) { $ arr [ $ i ++ ] = $ c ; $ c += 2 ; } $ c = 2 ; while ( $ c <= $ n ) { $ arr [ $ i ++ ] = $ c ; $ c += 2 ; } $ sum = 0 ; for ( $ i = $ l - 1 ; $ i < $ r ; $ i ++ ) { $ sum += $ arr [ $ i ] ; } return $ sum ; } $ n = 12 ; $ l = 1 ; $ r = 11 ; echo ( rangesum ( $ n , $ l , $ r ) ) ; ? >