# Pair with minimum absolute difference after solving each query

Given Q queries and an empty list.

The queries can be of two types:

- addToList(x) : Add x to your list.
- 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**:

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 of the elements so that insertion or deletion can be done in O(log(n)) with keeping the elements in a 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 of 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 of all, insert two sentinel value in the multi-set. Let’s say (-10^9+7 and 10^9+7) and initialize the map with the difference of these sentinel value, i.e 2*10^7+7 and set its frequency to be 1.

Now we have two type of queries:

**Insertion Query:**For an insertion query, first insert the*value*in set. 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 difference term 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).**Deletion Query:**In case of deletion of a value from the set. First, find the*left*and*right*elements of the element 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)).

`// C++ implementation of above approach ` `#include <bits/stdc++.h> ` `#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<ll>& s, map<ll, ll>& 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<ll>& s, map<ll, ll>& 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<ll> s; ` ` ` ` ` `// define map to store difference in sorted ` ` ` `map<ll, ll> mp; ` ` ` ` ` `// initialize set with sentinel values ` ` ` `s.insert(-MOD); ` ` ` `s.insert(MOD); ` ` ` ` ` `// maintain freq of differnce in map so, here intially ` ` ` `// 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; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

0 4 2 4

Explanation of each query:

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.**addToList(1):**-
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.**addToList(5):** As here we are inserting 3. So, first of all, 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**addToList(3):****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).-
Similar as of above query, here we are deleting element. So, first of all, find the left and right element to 3 i.e. left = 1, right = 5. As we going to delete 3 which is in between 1 & 5 so minimum difference will be affected only of 3 and 5,**removeFromList(3):**before deleting ==>{1, 3, 5}, after deleting ==> {1, 5}. So, map[3-1]--, map[5-3]-- and add map[5-1]++.

## Recommended Posts:

- Pair with minimum absolute difference | BST
- Pair of prime numbers with a given sum and minimum absolute difference
- Find the minimum absolute difference in two different BST's
- Minimum absolute difference between N and a power of 2
- Find the node whose absolute difference with X gives minimum value
- Difference Array | Range update query in O(1)
- Segment Tree | Set 2 (Range Minimum Query)
- Sort an array according to absolute difference with given value using Functors
- Find the node whose absolute difference with X gives maximum value
- Iterative Segment Tree (Range Minimum Query)
- Largest subset where absolute difference of any two element is a power of 2
- Check whether the sum of absolute difference of adjacent digits is Prime or not
- Arrange first N natural numbers such that absolute difference between all adjacent elements > 1
- Count maximum elements of an array whose absolute difference does not exceed K
- Range Minimum Query (Square Root Decomposition and Sparse Table)

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.