Rearrange and update array elements as specified by the given queries | 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
function Queries ( arr , N , Q ) { let dq = [ ] ; for ( let i = 0 ; i < N ; i ++ ) { dq . push ( arr [ i ] ) ; } let sz = Q . length ; for ( let i = 0 ; i < sz ; i ++ ) { if ( Q [ i ] [ 0 ] == 0 ) { let front = dq [ 0 ] ; dq . shift ( ) ; dq . push ( front ) ; } else if ( Q [ i ] [ 0 ] == 1 ) { let back = dq [ dq . length - 1 ] ; dq . pop ( ) ; dq . unshift ( back ) ; } else if ( Q [ i ] [ 0 ] == 2 ) { dq [ Q [ i ] [ 1 ] ] = Q [ i ] [ 2 ] ; } else { document . write ( dq [ Q [ i ] [ 1 ] ] + " " ) ; } } } let arr = [ 1 , 2 , 3 , 4 , 5 ] ; let N = arr . length ; let Q = [ [ 0 ] , [ 1 ] , [ 3 , 1 ] , [ 2 , 2 , 54 ] , [ 3 , 2 ] ] ; Queries ( arr , N , Q ) ;