# Queries On Array with disappearing and reappearing elements

• Difficulty Level : Expert
• Last Updated : 24 Feb, 2022

Given an array, arr[] of size N. Each second, an integer disappears for N seconds and after N seconds, it reappears at its original position. Integer disappear in the order from left to right arr, arr, …, arr[N – 1]. After all the integers disappear, they start reappearing until all integers reappear. Once N elements appear again, the process starts again.
Now given Q queries, each consists of two integers t and M. The task is to determine the Mth element from the left at tth second. If the array does not exist till M, then print -1.

Examples:

Input: arr[] = {1, 2, 3, 4, 5}, Q = {{1, 4}, {6, 1}, {3, 5}}
Output:

-1
At time,
t1 -> {2, 3, 4, 5}
t2 -> {3, 4, 5}
t3 -> {4, 5}
t4 -> {5}
t5 -> {}
t6 -> {1}

Input: arr[] = {5, 4, 3, 4, 5}, Q = {{2, 3}, {100000000, 2}}
Output:

4

Approach: The main approach is that it is required to check whether the array is empty or full and that can be seen by dividing the number of turns by the size of the array. If the remainder is 0, then it can be either one of the cases ( empty or fill ).
By observation, it is seen that in the odd turn the array is reducing and in even turns the array is expanding and using this observation it will be checked that M is outside of the index or inside the array.
Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach``#include ``using` `namespace` `std;` `// Function to perform the queries``void` `PerformQueries(vector<``int``>& a,``                    ``vector >& vec)``{` `    ``vector<``int``> ans;` `    ``// Size of the array with``    ``// 1-based indexing``    ``int` `n = (``int``)a.size() - 1;` `    ``// Number of queries``    ``int` `q = (``int``)vec.size();` `    ``// Iterating through the queries``    ``for` `(``int` `i = 0; i < q; ++i) {` `        ``long` `long` `t = vec[i].first;``        ``int` `m = vec[i].second;` `        ``// If m is more than the``        ``// size of the array``        ``if` `(m > n) {``            ``ans.push_back(-1);``            ``continue``;``        ``}` `        ``// Count of turns``        ``int` `turn = t / n;` `        ``// Find the remainder``        ``int` `rem = t % n;` `        ``// If the remainder is 0 and turn is``        ``// odd then the array is empty``        ``if` `(rem == 0 and turn % 2 == 1) {``            ``ans.push_back(-1);``            ``continue``;``        ``}` `        ``// If the remainder is 0 and turn is``        ``// even then array is full and``        ``// is in its initial state``        ``if` `(rem == 0 and turn % 2 == 0) {``            ``ans.push_back(a[m]);``            ``continue``;``        ``}` `        ``// If the remainder is not 0``        ``// and the turn is even``        ``if` `(turn % 2 == 0) {` `            ``// Current size of the array``            ``int` `cursize = n - rem;` `            ``if` `(cursize < m) {``                ``ans.push_back(-1);``                ``continue``;``            ``}``            ``ans.push_back(a[m + rem]);``        ``}``        ``else` `{` `            ``// Current size of the array``            ``int` `cursize = rem;``            ``if` `(cursize < m) {``                ``ans.push_back(-1);``                ``continue``;``            ``}``            ``ans.push_back(a[m]);``        ``}``    ``}` `    ``// Print the result``    ``for` `(``int` `i : ans)``        ``cout << i << ``"\n"``;``}` `// Driver code``int` `main()``{` `    ``// The initial array, -1 is for``    ``// 1 base indexing``    ``vector<``int``> a = { -1, 1, 2, 3, 4, 5 };` `    ``// Queries in the form of the pairs of (t, M)``    ``vector > vec = {``        ``{ 1, 4 },``        ``{ 6, 1 },``        ``{ 3, 5 }``    ``};` `    ``PerformQueries(a, vec);` `    ``return` `0;``}`

## Java

 `// Java implementation of the approach` `import` `java.util.*;` `class` `GFG``{` `    ``// Function to perform the queries``    ``static` `void` `PerformQueries(``int``[] a, ``int``[][] vec)``    ``{` `        ``Vector ans = ``new` `Vector<>();` `        ``// Size of the array with``        ``// 1-based indexing``        ``int` `n = (``int``) a.length - ``1``;` `        ``// Number of queries``        ``int` `q = (``int``) vec.length;` `        ``// Iterating through the queries``        ``for` `(``int` `i = ``0``; i < q; ++i)``        ``{` `            ``long` `t = vec[i][``0``];``            ``int` `m = vec[i][``1``];` `            ``// If m is more than the``            ``// size of the array``            ``if` `(m > n)``            ``{``                ``ans.add(-``1``);``                ``continue``;``            ``}` `            ``// Count of turns``            ``int` `turn = (``int``) (t / n);` `            ``// Find the remainder``            ``int` `rem = (``int``) (t % n);` `            ``// If the remainder is 0 and turn is``            ``// odd then the array is empty``            ``if` `(rem == ``0` `&& turn % ``2` `== ``1``)``            ``{``                ``ans.add(-``1``);``                ``continue``;``            ``}` `            ``// If the remainder is 0 and turn is``            ``// even then array is full and``            ``// is in its initial state``            ``if` `(rem == ``0` `&& turn % ``2` `== ``0``)``            ``{``                ``ans.add(a[m]);``                ``continue``;``            ``}` `            ``// If the remainder is not 0``            ``// and the turn is even``            ``if` `(turn % ``2` `== ``0``)``            ``{` `                ``// Current size of the array``                ``int` `cursize = n - rem;` `                ``if` `(cursize < m)``                ``{``                    ``ans.add(-``1``);``                    ``continue``;``                ``}``                ``ans.add(a[m + rem]);``            ``}``            ``else``            ``{` `                ``// Current size of the array``                ``int` `cursize = rem;``                ``if` `(cursize < m)``                ``{``                    ``ans.add(-``1``);``                    ``continue``;``                ``}``                ``ans.add(a[m]);``            ``}``        ``}` `        ``// Print the result``        ``for` `(``int` `i : ans)``            ``System.out.print(i + ``"\n"``);``    ``}` `    ``// Driver code``    ``public` `static` `void` `main(String[] args)``    ``{` `        ``// The initial array, -1 is for``        ``// 1 base indexing``        ``int``[] a = { -``1``, ``1``, ``2``, ``3``, ``4``, ``5` `};` `        ``// Queries in the form of the pairs of (t, M)``        ``int``[][] vec = { { ``1``, ``4` `}, { ``6``, ``1` `}, { ``3``, ``5` `} };` `        ``PerformQueries(a, vec);` `    ``}``}` `// This code is contributed by 29AjayKumar`

## Python3

 `# Python3 implementation of the approach` `# Function to perform the queries``def` `PerformQueries(a, vec) :` `    ``ans ``=` `[];` `    ``# Size of the array with``    ``# 1-based indexing``    ``n ``=` `len``(a) ``-` `1``;` `    ``# Number of queries``    ``q ``=` `len``(vec);` `    ``# Iterating through the queries``    ``for` `i ``in` `range``(q) :` `        ``t ``=` `vec[i][``0``];``        ``m ``=` `vec[i][``1``];` `        ``# If m is more than the``        ``# size of the array``        ``if` `(m > n) :``            ``ans.append(``-``1``);``            ``continue``;` `        ``# Count of turns``        ``turn ``=` `t ``/``/` `n;` `        ``# Find the remainder``        ``rem ``=` `t ``%` `n;` `        ``# If the remainder is 0 and turn is``        ``# odd then the array is empty``        ``if` `(rem ``=``=` `0` `and` `turn ``%` `2` `=``=` `1``) :``            ``ans.append(``-``1``);``            ``continue``;` `        ``# If the remainder is 0 and turn is``        ``# even then array is full and``        ``# is in its initial state``        ``if` `(rem ``=``=` `0` `and` `turn ``%` `2` `=``=` `0``) :``            ``ans.append(a[m]);``            ``continue``;` `        ``# If the remainder is not 0``        ``# and the turn is even``        ``if` `(turn ``%` `2` `=``=` `0``) :` `            ``# Current size of the array``            ``cursize ``=` `n ``-` `rem;` `            ``if` `(cursize < m) :``                ``ans.append(``-``1``);``                ``continue``;``    ` `            ``ans.append(a[m ``+` `rem]);``            ` `        ``else` `:` `            ``# Current size of the array``            ``cursize ``=` `rem;``            ` `            ``if` `(cursize < m) :``                ``ans.append(``-``1``);``                ``continue``;``        ` `            ``ans.append(a[m]);` `    ``# Print the result``    ``for` `i ``in` `ans :``        ``print``(i) ;` `# Driver code``if` `__name__ ``=``=` `"__main__"` `:` `    ``# The initial array, -1 is for``    ``# 1 base indexing``    ``a ``=` `[ ``-``1``, ``1``, ``2``, ``3``, ``4``, ``5` `];` `    ``# Queries in the form of the pairs of (t, M)``    ``vec ``=` `[``        ``[ ``1``, ``4` `],``        ``[ ``6``, ``1` `],``        ``[ ``3``, ``5` `]``    ``];` `    ``PerformQueries(a, vec);` `# This code is contributed by AnkitRai01`

## C#

 `// C# implementation of the approach``using` `System;``using` `System.Collections.Generic;` `class` `GFG``{` `    ``// Function to perform the queries``    ``static` `void` `PerformQueries(``int``[] a, ``int``[,] vec)``    ``{` `        ``List<``int``> ans = ``new` `List<``int``>();` `        ``// Size of the array with``        ``// 1-based indexing``        ``int` `n = (``int``) a.Length - 1;` `        ``// Number of queries``        ``int` `q = (``int``) vec.GetLength(0);` `        ``// Iterating through the queries``        ``for` `(``int` `i = 0; i < q; ++i)``        ``{` `            ``long` `t = vec[i, 0];``            ``int` `m = vec[i, 1];` `            ``// If m is more than the``            ``// size of the array``            ``if` `(m > n)``            ``{``                ``ans.Add(-1);``                ``continue``;``            ``}` `            ``// Count of turns``            ``int` `turn = (``int``) (t / n);` `            ``// Find the remainder``            ``int` `rem = (``int``) (t % n);` `            ``// If the remainder is 0 and turn is``            ``// odd then the array is empty``            ``if` `(rem == 0 && turn % 2 == 1)``            ``{``                ``ans.Add(-1);``                ``continue``;``            ``}` `            ``// If the remainder is 0 and turn is``            ``// even then array is full and``            ``// is in its initial state``            ``if` `(rem == 0 && turn % 2 == 0)``            ``{``                ``ans.Add(a[m]);``                ``continue``;``            ``}` `            ``// If the remainder is not 0``            ``// and the turn is even``            ``if` `(turn % 2 == 0)``            ``{` `                ``// Current size of the array``                ``int` `cursize = n - rem;` `                ``if` `(cursize < m)``                ``{``                    ``ans.Add(-1);``                    ``continue``;``                ``}``                ``ans.Add(a[m + rem]);``            ``}``            ``else``            ``{` `                ``// Current size of the array``                ``int` `cursize = rem;``                ``if` `(cursize < m)``                ``{``                    ``ans.Add(-1);``                    ``continue``;``                ``}``                ``ans.Add(a[m]);``            ``}``        ``}` `        ``// Print the result``        ``foreach` `(``int` `i ``in` `ans)``            ``Console.Write(i + ``"\n"``);``    ``}` `    ``// Driver code``    ``public` `static` `void` `Main(String[] args)``    ``{` `        ``// The initial array, -1 is for``        ``// 1 base indexing``        ``int``[] a = { -1, 1, 2, 3, 4, 5 };` `        ``// Queries in the form of the pairs of (t, M)``        ``int``[,] vec = { { 1, 4 }, { 6, 1 }, { 3, 5 } };` `        ``PerformQueries(a, vec);` `    ``}``}` `// This code is contributed by 29AjayKumar`

## Javascript

 ``

Output:

```5
1
-1```

Time Complexity: O(q)

Auxiliary Space: O(q)

My Personal Notes arrow_drop_up