Queries On Array with disappearing and reappearing elements | Function to perform the queries ; Size of the array with 1 - based indexing ; Number of queries ; Iterating through the queries ; If m is more than the size of the array ; Count of turns ; Find the remainder ; If the remainder is 0 and turn is odd then the array is empty ; If the remainder is 0 and turn is even then array is full and is in its initial state ; If the remainder is not 0 and the turn is even ; Current size of the array ; Current size of the array ; Print the result ; The initial array , - 1 is for 1 base indexing ; Queries in the form of the pairs of ( t , M )
function PerformQueries ( a , vec ) { let ans = new Array ( ) ; let n = a . length - 1 ; let q = vec . length ; for ( let i = 0 ; i < q ; ++ i ) { let t = vec [ i ] [ 0 ] ; let m = vec [ i ] [ 1 ] ; if ( m > n ) { ans . push ( - 1 ) ; continue ; } let turn = Math . floor ( t / n ) ; let rem = t % n ; if ( rem == 0 && turn % 2 == 1 ) { ans . push ( - 1 ) ; continue ; } if ( rem == 0 && turn % 2 == 0 ) { ans . push ( a [ m ] ) ; continue ; } if ( turn % 2 == 0 ) { let cursize = n - rem ; if ( cursize < m ) { ans . push ( - 1 ) ; continue ; } ans . push ( a [ m + rem ] ) ; } else { let cursize = rem ; if ( cursize < m ) { ans . push ( - 1 ) ; continue ; } ans . push ( a [ m ] ) ; } } for ( let i of ans ) document . write ( i + " " ) ; } let a = [ - 1 , 1 , 2 , 3 , 4 , 5 ] ; let vec = [ [ 1 , 4 ] , [ 6 , 1 ] , [ 3 , 5 ] ] ; PerformQueries ( a , vec ) ;