Queries On Array with disappearing and reappearing elements | C ++ implementation of the approach ; 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 ; Driver code ; The initial array , - 1 is for 1 base indexing ; Queries in the form of the pairs of ( t , M )
#include <bits/stdc++.h> NEW_LINE using namespace std ; void PerformQueries ( vector < int > & a , vector < pair < long long , int > > & vec ) { vector < int > ans ; int n = ( int ) a . size ( ) - 1 ; int q = ( int ) vec . size ( ) ; for ( int i = 0 ; i < q ; ++ i ) { long long t = vec [ i ] . first ; int m = vec [ i ] . second ; if ( m > n ) { ans . push_back ( -1 ) ; continue ; } int turn = t / n ; int rem = t % n ; if ( rem == 0 and turn % 2 == 1 ) { ans . push_back ( -1 ) ; continue ; } if ( rem == 0 and turn % 2 == 0 ) { ans . push_back ( a [ m ] ) ; continue ; } if ( turn % 2 == 0 ) { int cursize = n - rem ; if ( cursize < m ) { ans . push_back ( -1 ) ; continue ; } ans . push_back ( a [ m + rem ] ) ; } else { int cursize = rem ; if ( cursize < m ) { ans . push_back ( -1 ) ; continue ; } ans . push_back ( a [ m ] ) ; } } for ( int i : ans ) cout << i << " STRNEWLINE " ; } int main ( ) { vector < int > a = { -1 , 1 , 2 , 3 , 4 , 5 } ; vector < pair < long long , int > > vec = { { 1 , 4 } , { 6 , 1 } , { 3 , 5 } } ; PerformQueries ( a , vec ) ; return 0 ; }