Rearrange and update array elements as specified by the given queries | Python3 program to implement the above approach ; Function to perform the given operations ; Dequeue to store the array elements ; Insert all element of the array into the dequeue ; Stores the size of the queue ; Traverse each query ; Query for left shift . ; Extract the element at the front of the queue ; Pop the element at the front of the queue ; Push the element at the back of the queue ; Query for right shift ; Extract the element at the back of the queue ; Pop the element at the back of the queue ; Push the element at the front of the queue ; Query for update ; Query to get the value ; Driver Code ; All possible Queries
from collections import deque NEW_LINE def Queries ( arr , N , Q ) : NEW_LINE INDENT dq = deque ( ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT dq . append ( arr [ i ] ) NEW_LINE DEDENT sz = len ( Q ) NEW_LINE for i in range ( sz ) : NEW_LINE INDENT if ( Q [ i ] [ 0 ] == 0 ) : NEW_LINE INDENT front = dq [ 0 ] NEW_LINE dq . popleft ( ) NEW_LINE dq . appendleft ( front ) NEW_LINE DEDENT elif ( Q [ i ] [ 0 ] == 1 ) : NEW_LINE INDENT back = dq [ N - 1 ] NEW_LINE dq . popleft ( ) NEW_LINE dq . appendleft ( back ) NEW_LINE DEDENT elif ( Q [ i ] [ 0 ] == 2 ) : NEW_LINE INDENT dq [ Q [ i ] [ 1 ] ] = Q [ i ] [ 2 ] NEW_LINE DEDENT else : NEW_LINE INDENT print ( dq [ Q [ i ] [ 1 ] ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 , 5 ] NEW_LINE N = len ( arr ) NEW_LINE Q = [ [ 0 ] , [ 1 ] , [ 3 , 1 ] , [ 2 , 2 , 54 ] , [ 3 , 2 ] ] NEW_LINE Queries ( arr , N , Q ) NEW_LINE DEDENT