Queries to update array elements in a range [ L , R ] to satisfy given conditions | Function to prlet the array ; Function to perform the query in range [ L , R ] such that arr [ i ] += i - L + 1 ; Initialize array ; Traverse the query array ; Stores range in 1 - based index ; Update arr1 [ L ] ; Update arr1 [ R + 1 ] ; Update arr2 [ R + 1 ] ; Calculate prefix sum ; Traverse the array , arr2 [ ] ; Copy arr2 [ ] leto arr [ ] ; Given array ; Size of the array ; Stores count of query ; Function Call
function prletArray ( arr , N ) { for ( let i = 0 ; i < N ; i ++ ) { document . write ( arr [ i ] + " " ) ; } } function modifyArray ( arr , N , Q , cntQuery ) { let arr1 = new Array ( N + 2 ) . fill ( 0 ) ; let arr2 = new Array ( N + 2 ) . fill ( 0 ) ; for ( let i = 0 ; i < cntQuery ; i ++ ) { let L = Q [ i ] [ 0 ] + 1 , R = Q [ i ] [ 1 ] + 1 ; arr1 [ L ] ++ ; arr1 [ R + 1 ] -- ; arr2 [ R + 1 ] -= R - L + 1 ; } for ( let i = 1 ; i <= N ; i ++ ) arr1 [ i ] += arr1 [ i - 1 ] ; for ( let i = 1 ; i <= N ; i ++ ) arr2 [ i ] += arr2 [ i - 1 ] + arr1 [ i ] ; for ( let i = 1 ; i <= N ; i ++ ) arr [ i - 1 ] = arr2 [ i ] ; prletArray ( arr , N ) ; } let arr = [ 0 , 0 , 0 , 0 ] ; let N = arr . length ; let Q = [ [ 1 , 3 ] , [ 0 , 1 ] ] ; let cntQuery = Q . length ; modifyArray ( arr , N , Q , cntQuery ) ;