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[0], arr[1], …, 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
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 <bits/stdc++.h> using namespace std; // Function to perform the queries void PerformQueries(vector<int>& a, vector<pair<long long, int> >& 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<pair<long long, int> > 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<Integer> 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 |
5 1 -1
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
Recommended Posts:
- Count of even and odd set bit Array elements after XOR with K for Q queries
- Find sum of all unique elements in the array for K queries
- Array range queries for elements with frequency same as value
- Queries for GCD of all numbers of an array except elements in a given range
- Range Queries for Frequencies of array elements
- Queries for counts of array elements with values in given range
- Array range queries to find the number of perfect square elements with updates
- Queries for number of distinct elements from a given index till last index in an array
- Find the XOR of the elements in the given range [L, R] with the value K for a given set of queries
- Queries for elements having values within the range A to B using MO's Algorithm
- Queries for number of distinct elements in a subarray | Set 2
- Find the Initial Array from given array after range sum queries
- Count of elements which are power of 2 in a given range subarray for Q queries
- Queries for count of even digit sum elements in given range using MO's Algorithm
- Queries for elements greater than K in the given index range using Segment Tree
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.

