Perform given queries on Queue according to the given rules | Function to perform all the queries operations on the given queue ; Stores the count query of type 1 ; Event E1 : increase countE1 ; Event E2 : add the X in set ; Event E3 : Find position of X ; Initial position is ( position - countE1 ) ; If X is already removed or popped out ; Finding the position of X in queue ; Traverse set to decrease position of X for all the number removed in front ; Print the position of X ; Driver Code
function solve ( n , m , queries ) { let countE1 = 0 ; let removed = new Set ( ) ; for ( let i = 0 ; i < m ; i ++ ) { if ( queries [ i ] [ 0 ] == 1 ) ++ countE1 ; else if ( queries [ i ] [ 0 ] == 2 ) removed . add ( queries [ i ] [ 1 ] ) ; else { let position = queries [ i ] [ 1 ] - countE1 ; if ( removed . has ( queries [ i ] [ 1 ] ) != false position <= 0 ) document . write ( " " + " " ) ; else { for ( let it of removed ) { if ( it > queries [ i ] [ 1 ] ) break ; position -- ; } document . write ( position + " " ) ; } } } } let N = 5 , Q = 3 ; let queries = new Array ( Q ) ; for ( let i = 0 ; i < Q ; i ++ ) { queries [ i ] = new Array ( 2 ) ; } queries [ 0 ] [ 0 ] = 1 ; queries [ 0 ] [ 1 ] = 0 ; queries [ 1 ] [ 0 ] = 3 ; queries [ 1 ] [ 1 ] = 3 ; queries [ 2 ] [ 0 ] = 2 ; queries [ 2 ] [ 1 ] = 2 ; solve ( N , Q , queries ) ;