Queries to add, remove and return the difference of maximum and minimum.
Last Updated :
05 Sep, 2022
Given Q queries. The queries are of three types and are described below:
- Add the number num to the list.
- Remove the number num from the list.
- Return the difference between the maximum and minimum number in the list.
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:
- store the numbers in an array of vectors.
- For the query of type-1 add an element to the array.
- For a query of type-2 remove the element from the vector or array.
- For the query of type-3, traverse the array and find the minimum and maximum in the array and return the difference of them.
Time Complexity:
- Type-1 query: O(1) as insertion of an element will cost O(1) time.
- Type-2 query: O(N) as in worst case we would need to traverse the whole array to search and remove the element.
- Type-3 query: O(N) as we need to traverse the array to find minimum and maximum element.
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.
- Insert the element in the container using insert() function.
- Delete the element from the container using erase() function.
- The minimum value will always be at the beginning and the maximum at the end always. They can be retrieved by using begin() and rbegin() function.
Below is the implementation of the efficient approach:
C++
#include <bits/stdc++.h>
using namespace std;
set< int > s;
void performQueryOne( int num)
{
s.insert(num);
}
void performQueryTwo( int num)
{
s.erase(num);
}
int performQueryThree()
{
int mini = *(s.begin());
int maxi = *(s.rbegin());
return maxi - mini;
}
int main()
{
int num = 3;
performQueryOne(num);
num = 5;
performQueryOne(num);
num = 6;
performQueryOne(num);
num = 5;
performQueryTwo(num);
num = 2;
performQueryOne(num);
cout << performQueryThree();
return 0;
}
|
Java
import java.util.*;
class GFG
{
static SortedSet<Integer> t = new TreeSet<Integer>();
static void performQueryOne( int num)
{
t.add(num);
}
static void performQueryTwo( int num)
{
t.remove(num);
}
static int performQueryThree()
{
int mini = t.first();
int maxi = t.last();
return maxi - mini;
}
public static void main(String[] args)
{
int num = 3 ;
performQueryOne(num);
num = 5 ;
performQueryOne(num);
num = 6 ;
performQueryOne(num);
num = 5 ;
performQueryTwo(num);
num = 2 ;
performQueryOne(num);
System.out.println(performQueryThree());
}
}
|
Python3
def performQueryOne(num):
s.add(num)
def performQueryTwo(num):
s.remove(num)
def performQueryThree():
mini = min (s)
maxi = max (s)
return maxi - mini
if __name__ = = "__main__" :
s = set ()
num = 3
performQueryOne(num)
num = 5
performQueryOne(num)
num = 6
performQueryOne(num)
num = 5
performQueryTwo(num)
num = 2
performQueryOne(num)
print (performQueryThree())
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static SortedSet< int > t = new SortedSet< int >();
static void performQueryOne( int num)
{
t.Add(num);
}
static void performQueryTwo( int num)
{
t.Remove(num);
}
static int performQueryThree()
{
int mini = t.Min;
int maxi = t.Max;
return maxi - mini;
}
public static void Main(String[] args)
{
int num = 3;
performQueryOne(num);
num = 5;
performQueryOne(num);
num = 6;
performQueryOne(num);
num = 5;
performQueryTwo(num);
num = 2;
performQueryOne(num);
Console.WriteLine(performQueryThree());
}
}
|
Javascript
<script>
var s = new Set();
function performQueryOne( num)
{
s.add(num);
}
function performQueryTwo( num)
{
s. delete (num);
}
function performQueryThree()
{
var tmp = [...s].sort((a,b)=>a-b);
var mini = tmp[0];
var maxi = tmp[tmp.length-1];
return maxi - mini;
}
var num = 3;
performQueryOne(num);
num = 5;
performQueryOne(num);
num = 6;
performQueryOne(num);
num = 5;
performQueryTwo(num);
num = 2;
performQueryOne(num);
document.write( performQueryThree());
</script>
|
Complexity Analysis:
- Time Complexity: O(log N) for every query, as we are using tree set and insert, remove and getting minimum or maximum element will take logN time.
- Auxiliary Space: O(N), as we are using extra space for the tree set.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...