# Queries On Array with disappearing and reappearing elements

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 consisting 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:
5
1
-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:
5
4

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: The main approach is that it is required to check whether the array is empty or full and it can be seen by dividing the number of turns with 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 the even turns the array is expanding and using this observation it will be checked that M is out 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 intial 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 intial 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 intial 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 intial 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 `

Output:

```5
1
-1
```

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : AnkitRai01, 29AjayKumar

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.