![]() |
VOOZH | about |
Given Q queries. The queries are of three types and are described below:
The task is to write a program the performs the above queries.
Note: The numbers are distinct and at every call of query-3, there will be a minimum of 1 element in the list.
Examples:
Input:
Q = 5
Query of type 1: num = 3
Query of type 1: num = 5
Query of type 1: num = 6
Query of type 2: num = 6
Query of type 1: num = 2
Query of type 3:Output: 4
Since query of type 3 has been called once only, the answer at the instant is 4.
After first query of type-1, the list is {3}
After second query of type-1, the list is {3, 5}
After third query of type-1, the list is {3, 5, 6}
After fourth query of type-2, the list is {3, 5}
After fifth query of type-1, the list is {2, 3, 5}
On sixth query of type-3, the answer is 5-2.
A simple solution is to follow the below steps:
Time Complexity:
Auxiliary Space: O(N), as we need to use extra space for array.
An efficient solution is to use a self-balancing binary search tree (Implemented as set container in C++ or TreeSet in Java). The below steps are followed to solve the above problem.
Below is the implementation of the efficient approach:
4
Complexity Analysis: