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 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:
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 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++
#include <bits/stdc++.h>
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 << "\n" ;
}
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;
}
|
Java
import java.util.*;
class GFG
{
static void PerformQueries( int [] a, int [][] vec)
{
Vector<Integer> ans = new Vector<>();
int n = ( int ) a.length - 1 ;
int q = ( int ) vec.length;
for ( int i = 0 ; i < q; ++i)
{
long t = vec[i][ 0 ];
int m = vec[i][ 1 ];
if (m > n)
{
ans.add(- 1 );
continue ;
}
int turn = ( int ) (t / n);
int rem = ( int ) (t % n);
if (rem == 0 && turn % 2 == 1 )
{
ans.add(- 1 );
continue ;
}
if (rem == 0 && turn % 2 == 0 )
{
ans.add(a[m]);
continue ;
}
if (turn % 2 == 0 )
{
int cursize = n - rem;
if (cursize < m)
{
ans.add(- 1 );
continue ;
}
ans.add(a[m + rem]);
}
else
{
int cursize = rem;
if (cursize < m)
{
ans.add(- 1 );
continue ;
}
ans.add(a[m]);
}
}
for ( int i : ans)
System.out.print(i + "\n" );
}
public static void main(String[] args)
{
int [] a = { - 1 , 1 , 2 , 3 , 4 , 5 };
int [][] vec = { { 1 , 4 }, { 6 , 1 }, { 3 , 5 } };
PerformQueries(a, vec);
}
}
|
Python3
def PerformQueries(a, vec) :
ans = [];
n = len (a) - 1 ;
q = len (vec);
for i in range (q) :
t = vec[i][ 0 ];
m = vec[i][ 1 ];
if (m > n) :
ans.append( - 1 );
continue ;
turn = t / / n;
rem = t % n;
if (rem = = 0 and turn % 2 = = 1 ) :
ans.append( - 1 );
continue ;
if (rem = = 0 and turn % 2 = = 0 ) :
ans.append(a[m]);
continue ;
if (turn % 2 = = 0 ) :
cursize = n - rem;
if (cursize < m) :
ans.append( - 1 );
continue ;
ans.append(a[m + rem]);
else :
cursize = rem;
if (cursize < m) :
ans.append( - 1 );
continue ;
ans.append(a[m]);
for i in ans :
print (i) ;
if __name__ = = "__main__" :
a = [ - 1 , 1 , 2 , 3 , 4 , 5 ];
vec = [
[ 1 , 4 ],
[ 6 , 1 ],
[ 3 , 5 ]
];
PerformQueries(a, vec);
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static void PerformQueries( int [] a, int [,] vec)
{
List< int > ans = new List< int >();
int n = ( int ) a.Length - 1;
int q = ( int ) vec.GetLength(0);
for ( int i = 0; i < q; ++i)
{
long t = vec[i, 0];
int m = vec[i, 1];
if (m > n)
{
ans.Add(-1);
continue ;
}
int turn = ( int ) (t / n);
int rem = ( int ) (t % n);
if (rem == 0 && turn % 2 == 1)
{
ans.Add(-1);
continue ;
}
if (rem == 0 && turn % 2 == 0)
{
ans.Add(a[m]);
continue ;
}
if (turn % 2 == 0)
{
int cursize = n - rem;
if (cursize < m)
{
ans.Add(-1);
continue ;
}
ans.Add(a[m + rem]);
}
else
{
int cursize = rem;
if (cursize < m)
{
ans.Add(-1);
continue ;
}
ans.Add(a[m]);
}
}
foreach ( int i in ans)
Console.Write(i + "\n" );
}
public static void Main(String[] args)
{
int [] a = { -1, 1, 2, 3, 4, 5 };
int [,] vec = { { 1, 4 }, { 6, 1 }, { 3, 5 } };
PerformQueries(a, vec);
}
}
|
Javascript
<script>
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 + "<br>" );
}
let a = [-1, 1, 2, 3, 4, 5];
let vec = [
[1, 4],
[6, 1],
[3, 5]];
PerformQueries(a, vec);
</script>
|
Time Complexity: O(q)
Auxiliary Space: O(q)
Last Updated :
24 Feb, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...