# Pair with minimum absolute difference after solving each query

• Difficulty Level : Medium
• Last Updated : 10 Aug, 2021

Given Q queries and an empty list.
The queries can be of two types:

2. removeFromList(x) : Remove x from your list.

The task is, after each query, you have to print the minimum value of abs(list[i]-list[j]) where, 0<=i<=n, 0<=j<=n and i ≠ j and n is the total number of elements present in the list.
Examples

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

Input : Q = 4
addToList(1), insert 1 in our set
addToList(5), insert 5 in our set
addToList(3), insert 3 in our set
removeFromList(3), delete 3 in our set
Output
0, as we have only one element {1} in our set.
4, as we have {1, 5} minimum difference between all possible pairs is 4 (5-1)
2, as we have {1, 3, 5} minimum difference between all possible pairs is 2 (3-1)
4, as we have {1, 5} minimum difference between all possible pairs is 4 (5-1)

Method 1: The idea is to use set-in C++ to store all the elements so that insertion or deletion can be done in O(log(n)) with keeping the elements in sorted order. Now, to find the minimum difference, we have to iterate over the whole set and find the difference between only adjacent elements as elements are in sorted order i.e, the minimum difference will always be contributed by adjacent elements. This can be done in O(n) time complexity. So, the overall time complexity of this approach will be (q*log(n)+q*n).
Method 2:
We can use multiset to store and maintain all the elements and map to store the difference between the adjacent elements in sorted order, where, map’s key represents the difference between adjacent elements and the corresponding map’s value represent the frequency of occurrence of this difference.
Below is the complete algorithm:
First, insert two sentinel values in the multi-set. Let’s say (-10^9+7 and 10^9+7) and initialize the map with the difference of this sentinel value, i.e 2*10^7+7, and set its frequency to be 1.
Now we have two types of queries:

1. Insertion Query: For an insertion query, first insert the value inset. After inserting the element, we will have to also update the differences of adjacent elements in the map. To do this, find the left and right values of the newly inserted element in the set. Earlier when this new value was not inserted in the set, abs(right-left) is contributing to the different terms in the map. To update the map after inserting the new element, first, remove the previous difference abs(right-left) as a new value is inserted between the elements left and right and add two differences to the map. i.e, abs(right-x) and abs(x-left).
2. Deletion Query: In case of deletion of a value from the set. First, find the left and right elements of the elements that needed to be deleted, say x. Then according to the above algorithm, reduce the frequency of abs(right-x) and abs(left-x) and increment the frequency of abs(right-left).

So, each query can be solved in log(n) time complexity. Therefore, overall complexity will be O(q*log(n)).

## CPP

 `// C++ implementation of above approach``#include ``#define ll long long int``const` `ll MOD = 1e9 + 7;``using` `namespace` `std;` `// Function to add an element into the list``void` `addToList(``int` `x, multiset& s, map& mp)``{``    ``// firstly insert value in set``    ``s.insert(x);` `    ``// find left and right value of inserted value.``    ``ll left, right;` `    ``// now here is logic explained below``    ``left = *(--s.find(x));``    ``right = *(++s.find(x));``    ``mp[``abs``(left - x)]++;``    ``mp[``abs``(right - x)]++;``    ``mp[``abs``(left - right)]--;``    ``if` `(mp[``abs``(left - right)] == 0)``        ``mp.erase(``abs``(left - right));``}` `// Function to remove an element from the list``void` `removeFromList(``int` `x, multiset& s, map& mp)``{``    ``ll left, right;` `    ``// Find left element of number that we want to delete.``    ``left = *(--s.find(x));` `    ``// Find right element of number that we want to delete.``    ``right = *(++s.find(x));` `    ``// remove x from set``    ``s.erase(s.find(x));` `    ``// Again this will explain below in article.``    ``mp[``abs``(left - x)]--;``    ``if` `(mp[``abs``(left - x)] == 0)``        ``mp.erase(``abs``(left - x));``    ``mp[``abs``(right - x)]--;``    ``if` `(mp[``abs``(right - x)] == 0)``        ``mp.erase(``abs``(right - x));` `    ``mp[``abs``(left - right)]++;``}` `// Driver code``int` `main()``{` `    ``int` `q = 4;` `    ``// define set to store all values.``    ``multiset s;` `    ``// define map to store difference in sorted``    ``map mp;` `    ``// initialize set with sentinel values``    ``s.insert(-MOD);``    ``s.insert(MOD);` `    ``// maintain freq of difference in map so, here initially``    ``// difference b/w sentinel value and its freq is one.``    ``mp[2 * MOD] = 1;` `    ``// 1st query 1 1 i.e, include 1 in our set``    ``addToList(1, s, mp);` `    ``// As now we have only one element {-10^9+7, 1, 10^9+7}``    ``// (except two are sentinel values)``    ``// so minimum among all pair is zero``    ``cout << 0 << endl;` `    ``// 2nd query 1 5 i.e, include 5 in our set.``    ``addToList(5, s, mp);` `    ``// find smallest difference possible``    ``// as it should be in beginning of map``    ``cout << mp.begin()->first << endl;` `    ``// 3rd query 1 3 i.e, include 3 in our set.``    ``addToList(3, s, mp);` `    ``// find smallest difference possible``    ``// as it should be in beginning of map``    ``cout << mp.begin()->first << endl;` `    ``// 4th query 2 3 i.e, remove 3 from our list``    ``removeFromList(3, s, mp);` `    ``cout << mp.begin()->first << endl;` ` ``return` `0;``}`
Output:
```0
4
2
4```

Explanation of each query:

1. addToList(1): As now we have only one element {-10^9+7, 1, 10^9+7} (except two are sentinel values), so minimum among all pair is zero.
2. addToList(5): After inserting 1 and 5 to the set, set becomes {-10^9+7, 1, 5, 10^9+7} and map: mp[5-1]=1 where key represents difference and its value represents frequency.
3. addToList(3): As here we are inserting 3. So, first, we find the left and right element of 3 in the set after insertion i.e. left = 1, right = 5. As 3 came in between 1 and 5, so we remove map[5-1]–. (as 3 came in between 1 and 5 so 5-1 will no longer minimum) And we include these differences map[3-1]++ and map[5-3]++ (because 3 came between 1 and 5, so possible minimum have two cases).
4. removeFromList(3): Similar to of above query, here we are deleting the element. So, first, find the left and right elements to 3 i.e. left = 1, right = 5. As we are going to delete 3 which is in between 1 & 5 so the minimum difference will be affected only by 3 and 5,
```before deleting ==>{1, 3, 5},
after deleting ==> {1, 5}.
So, map[3-1]--, map[5-3]-- and add map[5-1]++.```

Time Complexity : O(qlogN), where ‘q’ is the total number of queries.
Auxiliary Space: O(q).

My Personal Notes arrow_drop_up