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 pointer as 0 ; Query to perform anticlockwise rotation ; Query to answer range sum ; If pointing 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
def rotatedSumQuery ( arr , n , query , Q ) : NEW_LINE INDENT prefix = [ 0 ] * ( 2 * n ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT prefix [ i ] = arr [ i ] NEW_LINE prefix [ i + n ] = arr [ i ] NEW_LINE DEDENT for i in range ( 1 , 2 * n ) : NEW_LINE INDENT prefix [ i ] += prefix [ i - 1 ] ; NEW_LINE DEDENT start = 0 ; NEW_LINE for q in range ( Q ) : NEW_LINE INDENT if ( query [ q ] [ 0 ] == 1 ) : NEW_LINE INDENT k = query [ q ] [ 1 ] NEW_LINE start = ( start + k ) % n ; NEW_LINE DEDENT elif ( query [ q ] [ 0 ] == 2 ) : NEW_LINE INDENT L = query [ q ] [ 1 ] NEW_LINE R = query [ q ] [ 2 ] NEW_LINE if ( start + L == 0 ) : NEW_LINE INDENT print ( prefix [ start + R ] ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( prefix [ start + R ] - prefix [ start + L - 1 ] ) NEW_LINE DEDENT DEDENT DEDENT DEDENT arr = [ 1 , 2 , 3 , 4 , 5 , 6 ] ; NEW_LINE Q = 5 NEW_LINE query = [ [ 2 , 1 , 3 ] , [ 1 , 3 ] , [ 2 , 0 , 3 ] , [ 1 , 4 ] , [ 2 , 3 , 5 ] ] NEW_LINE n = len ( arr ) ; NEW_LINE rotatedSumQuery ( arr , n , query , Q ) ; NEW_LINE