Perform given queries on Queue according to the given rules
Last Updated :
21 Feb, 2022
Given a queue consisting of the first N natural numbers and queries Query[][] of the type {E, X}, the task is to perform the given queries on the given queue according to the following rules:
- If the value of E is 1, then pop the front element from the queue.
- If the value of E is 2, then remove the value X from the queue if it exists.
- If the value of E is 3, then find the position of X in the queue if it exists. Otherwise, print “-1”.
Examples:
Input: N = 5, Query[][] = {{1, 0}, {3, 3}, {2, 2}}
Output: 2
Explanation:
Initially queue looks like { 1, 2, 3, 4, 5 }. Following are the operations performed according to the queries given:
1st query E = 1, X = 0 -> 1 is popped out changing queue to { 2, 3, 4, 5 }.
2nd query E = 3, X = 3 -> The position of 3 is printed.
3rd query E = 2, X = 2 -> 2 is removed from queue changing queue to { 3, 4, 5 }.
Input: N = 5, Query[][] = {{1, 0}, {3, 1}}
Output: -1
Approach: The given problem can be solved by using Greedy Approach and Binary Search. Follow the steps below to solve the given problem.
- Initialize a variable say countE1 as 0 to count the number of occurrences of event E1.
- Increment the value of countE1 by 1 when the query is of type E1.
- Maintain a set data structure that stores the elements which are removed while performing queries operations.
- For the query of type 3 perform the following steps:
- Initialize a variable, say position to store the initial position of X.
- Find the value of X in the set to check whether X is already removed or not and if X is present in the set, the print -1. Otherwise, find the position of X.
- Traverse the set with iterator say it and if the value of it > X, then break out of the loop. Otherwise, decrease the position by 1.
- Print the final position of X store in position variable.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void solve( int n, int m, int ** queries)
{
int countE1 = 0;
set< int > removed;
for ( int i = 0; i < m; i++) {
if (queries[i][0] == 1)
++countE1;
else if (queries[i][0] == 2)
removed.insert(queries[i][1]);
else {
int position = queries[i][1]
- countE1;
if (removed.find(queries[i][1])
!= removed.end()
|| position <= 0)
cout << "-1\n" ;
else {
for ( int it : removed) {
if (it > queries[i][1])
break ;
position--;
}
cout << position << "\n" ;
}
}
}
}
int main()
{
int N = 5, Q = 3;
int ** queries = new int *[Q];
for ( int i = 0; i < Q; i++) {
queries[i] = new int [2];
}
queries[0][0] = 1;
queries[0][1] = 0;
queries[1][0] = 3;
queries[1][1] = 3;
queries[2][0] = 2;
queries[2][1] = 2;
solve(N, Q, queries);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void solve( int n, int m, int [][]queries)
{
int countE1 = 0 ;
HashSet<Integer> removed = new HashSet<Integer>();
for ( int i = 0 ; i < m; i++) {
if (queries[i][ 0 ] == 1 )
++countE1;
else if (queries[i][ 0 ] == 2 )
removed.add(queries[i][ 1 ]);
else {
int position = queries[i][ 1 ]
- countE1;
if (removed.contains(queries[i][ 1 ])
|| position <= 0 )
System.out.print( "-1\n" );
else {
for ( int it : removed) {
if (it > queries[i][ 1 ])
break ;
position--;
}
System.out.print(position+ "\n" );
}
}
}
}
public static void main(String[] args)
{
int N = 5 , Q = 3 ;
int [][]queries = new int [Q][ 2 ];
queries[ 0 ][ 0 ] = 1 ;
queries[ 0 ][ 1 ] = 0 ;
queries[ 1 ][ 0 ] = 3 ;
queries[ 1 ][ 1 ] = 3 ;
queries[ 2 ][ 0 ] = 2 ;
queries[ 2 ][ 1 ] = 2 ;
solve(N, Q, queries);
}
}
|
Python3
def solve(n, m, queries):
countE1 = 0
removed = set ()
for i in range ( 0 , m):
if (queries[i][ 0 ] = = 1 ):
countE1 + = 1
elif (queries[i][ 0 ] = = 2 ):
removed.add(queries[i][ 1 ])
else :
position = queries[i][ 1 ] - countE1
if (queries[i][ 1 ] in removed or position < = 0 ):
print ( "-1" )
else :
for it in removed:
if (it > queries[i][ 1 ]):
break
position - = 1
print (position)
if __name__ = = "__main__" :
N = 5
Q = 3
queries = [[ 0 for _ in range ( 2 )] for _ in range (Q)]
queries[ 0 ][ 0 ] = 1
queries[ 0 ][ 1 ] = 0
queries[ 1 ][ 0 ] = 3
queries[ 1 ][ 1 ] = 3
queries[ 2 ][ 0 ] = 2
queries[ 2 ][ 1 ] = 2
solve(N, Q, queries)
|
C#
using System;
using System.Collections.Generic;
using System.Linq;
public class GFG {
static void solve( int n, int m, int [,]queries)
{
int countE1 = 0;
HashSet< int > removed = new HashSet< int >();
for ( int i = 0; i < m; i++) {
if (queries[i, 0] == 1)
++countE1;
else if (queries[i, 0] == 2)
removed.Add(queries[i, 1]);
else {
int position = queries[i, 1]
- countE1;
if (removed.Contains(queries[i, 1])
|| position <= 0)
Console.WriteLine( "-1\n" );
else {
foreach ( int it in removed) {
if (it > queries[i, 1])
break ;
position--;
}
Console.WriteLine(position+ "\n" );
}
}
}
}
public static void Main ( string [] args) {
int N = 5, Q = 3;
int [,]queries = new int [Q, 2];
queries[0, 0] = 1;
queries[0, 1] = 0;
queries[1, 0] = 3;
queries[1, 1] = 3;
queries[2, 0] = 2;
queries[2, 1] = 2;
solve(N, Q, queries);
}
}
|
Javascript
<script>
function solve(n, m, queries)
{
let countE1 = 0;
let removed = new Set();
for (let i = 0; i < m; i++) {
if (queries[i][0] == 1)
++countE1;
else if (queries[i][0] == 2)
removed.add(queries[i][1]);
else {
let position = queries[i][1]
- countE1;
if (removed.has(queries[i][1])
!= false
|| position <= 0)
document.write( "-1" + "<br>" );
else {
for (let it of removed) {
if (it > queries[i][1])
break ;
position--;
}
document.write(position + "<br>" );
}
}
}
}
let N = 5, Q = 3;
let queries = new Array(Q);
for (let i = 0; i < Q; i++) {
queries[i] = new Array(2);
}
queries[0][0] = 1;
queries[0][1] = 0;
queries[1][0] = 3;
queries[1][1] = 3;
queries[2][0] = 2;
queries[2][1] = 2;
solve(N, Q, queries);
</script>
|
Time Complexity:
- For Query of type 1: O(1)
- For Query of type 2: O(log N)
- For Query of type 3: O(N2 log N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...