Range sum queries for anticlockwise rotations of Array by K indices | Function to execute the queries ; Construct a new array of size 2 * N to store prefix sum of every index ; Copy elements to the new array ; Calculate the prefix sum for every index ; Set start poleter as 0 ; Query to perform anticlockwise rotation ; Query to answer range sum ; If poleting to 1 st index ; Display the sum upto start + R ; Subtract sum upto start + L - 1 from sum upto start + R ; Driver code ; Number of query ; Store all the queries
function rotatedSumQuery ( arr , n , query , Q ) { let prefix = [ ] ; for ( let i = 0 ; i < n ; i ++ ) { prefix [ i ] = arr [ i ] ; prefix [ i + n ] = arr [ i ] ; } for ( let i = 1 ; i < 2 * n ; i ++ ) prefix [ i ] += prefix [ i - 1 ] ; let start = 0 ; for ( let q = 0 ; q < Q ; q ++ ) { if ( query [ q ] [ 0 ] == 1 ) { let k = query [ q ] [ 1 ] ; start = ( start + k ) % n ; } else if ( query [ q ] [ 0 ] == 2 ) { let L , R ; L = query [ q ] [ 1 ] ; R = query [ q ] [ 2 ] ; if ( start + L == 0 ) document . write ( prefix [ start + R ] + " " ) ; else document . write ( prefix [ start + R ] - prefix [ start + L - 1 ] + " " ) ; } } } let arr = [ 1 , 2 , 3 , 4 , 5 , 6 ] ; let Q = 5 ; let query = [ [ 2 , 1 , 3 ] , [ 1 , 3 ] , [ 2 , 0 , 3 ] , [ 1 , 4 ] , [ 2 , 3 , 5 ] ] ; let n = arr . length ; rotatedSumQuery ( arr , n , query , Q ) ;