# Minimum distance between any special pair in the given array

Given an array arr[] of N integers, the task is to find the minimum possible absolute difference between indices of a special pair.

A special pair is defined as a pair of indices (i, j) such that if arr[i] ≤ arr[j], then there is no element X (where arr[i] < X < arr[j]) present in between indices i and j.
For example:
arr[] = {1, -5, 5}
Here, {1, 5} forms a special pair as there are no elements in the range (1 to 5) in between arr[0] and arr[2].

Print the minimum absolute difference abs(j – i) such that pair (i, j) forms a special pair.

Examples:

Input: arr[] = {0, -10, 5, -5, 1}
Output:
Explanation:
The elements 1 and 5 forms a special pair since there is no elements X in the range 1 < X < 5 in between them, and they are 2 indices away from each other.

Input: arr[] = {3, 3}
Output: 1

Naive Approach: The simplest approach is to consider every pair of elements of the array and check if they form a special pair or not. If found to be true, then print the minimum distance among all the pairs formed.

Time Complexity: O(N3
Auxiliary Space: O(1)

Efficient Approach: To optimize the above approach, the idea is to observe that we have to only consider the distance between the position of adjacent elements in the sorted array since those pair of elements will not have any value X between it. Below are the steps:

1. Store the initial indices of array elements in a Map.
2. Sort the given array arr[].
3. Now, find the distance between the indices of adjacent elements of the sorted array using the Map.
4. Maintain the minimum distance for each pair of adjacent elements in the step above.
5. After the completing the above steps, print the minimum distance formed.

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach  ` `#include   ` `using` `namespace` `std;  ` ` `  `// Function that finds the minimum  ` `// difference between two vectors  ` `int` `mindist(vector<``int``>& left,  ` `            ``vector<``int``>& right)  ` `{  ` `    ``int` `res = INT_MAX;  ` `    ``for` `(``int` `i = 0; i < left.size(); ++i) {  ` ` `  `        ``int` `num = left[i];  ` ` `  `        ``// Find lower bound of the index  ` `        ``int` `index  ` `            ``= lower_bound(right.begin(),  ` `                        ``right.end(), num)  ` `            ``- right.begin();  ` ` `  `        ``// Find two adjacent indices  ` `        ``// to take difference  ` `        ``if` `(index == 0)  ` `            ``res = min(res,  ` `                    ``abs``(num  ` `                        ``- right[index]));  ` ` `  `        ``else` `if` `(index == right.size())  ` `            ``res = min(res,  ` `                    ``abs``(num  ` `                        ``- right[index - 1]));  ` `        ``else` `            ``res = min(res,  ` `                    ``min(``abs``(num  ` `                            ``- right[index - 1]),  ` `                        ``abs``(num  ` `                            ``- right[index])));  ` `    ``}  ` ` `  `    ``// Return the result  ` `    ``return` `res;  ` `}  ` ` `  `// Function to find the minimum distance  ` `// between index of special pairs  ` `int` `specialPairs(vector<``int``>& nums)  ` `{  ` `    ``// Stores the index of each element  ` `    ``// in the array arr[]  ` `    ``map<``int``, set<``int``> > m;  ` `    ``vector<``int``> vals;  ` ` `  `    ``// Store the indexes  ` `    ``for` `(``int` `i = 0;  ` `        ``i < nums.size(); ++i) {  ` `        ``m[nums[i]].insert(i);  ` `    ``}  ` ` `  `    ``// Get the unique values in list  ` `    ``for` `(``auto` `p : m) {  ` `        ``vals.push_back(p.first);  ` `    ``}  ` ` `  `    ``int` `res = INT_MAX;  ` ` `  `    ``for` `(``int` `i = 0;  ` `        ``i < vals.size(); ++i) {  ` ` `  `        ``vector<``int``> vec(m[vals[i]].begin(),  ` `                        ``m[vals[i]].end());  ` ` `  `        ``// Take adjacent difference  ` `        ``// of same values  ` `        ``for` `(``int` `i = 1;  ` `            ``i < vec.size(); ++i)  ` `            ``res = min(res,  ` `                    ``abs``(vec[i]  ` `                        ``- vec[i - 1]));  ` ` `  `        ``if` `(i) {  ` `            ``int` `a = vals[i];  ` ` `  `            ``// Left index array  ` `            ``vector<``int``> left(m[a].begin(),  ` `                            ``m[a].end());  ` ` `  `            ``int` `b = vals[i - 1];  ` ` `  `            ``// Right index array  ` `            ``vector<``int``> right(m[b].begin(),  ` `                            ``m[b].end());  ` ` `  `            ``// Find the minimum gap between  ` `            ``// the two adjacent different  ` `            ``// values  ` `            ``res = min(res,  ` `                    ``mindist(left, right));  ` `        ``}  ` `    ``}  ` `    ``return` `res;  ` `}  ` ` `  `// Driver Code  ` `int` `main()  ` `{  ` `    ``// Given array  ` `    ``vector<``int``> arr{ 0, -10, 5, -5, 1 };  ` ` `  `    ``// Function Call  ` `    ``cout << specialPairs(arr);  ` ` `  `    ``return` `0;  ` `}  `

## Python3

 `# Python3 program for the above approach ` `import` `sys ` ` `  `# Function that finds the minimum ` `# difference between two vectors ` `def` `mindist(left, right): ` `     `  `    ``res ``=` `sys.maxsize ` `     `  `    ``for` `i ``in` `range``(``len``(left)): ` `        ``num ``=` `left[i] ` `         `  `        ``# Find lower bound of the index ` `        ``index ``=` `right.index(``min``( ` `                ``[i ``for` `i ``in` `right ``if` `num >``=` `i])) ` `         `  `        ``# Find two adjacent indices ` `        ``# to take difference ` `        ``if` `(index ``=``=` `0``): ` `            ``res ``=` `min``(res, ` `                  ``abs``(num ``-` `right[index])) ` `        ``elif` `(index ``=``=` `len``(right)): ` `            ``res ``=` `min``(res,  ` `                  ``min``(``abs``(num ``-` `right[index ``-``1``]), ` `                      ``abs``(num ``-` `right[index]))) ` ` `  `    ``# Return the result ` `    ``return` `res ` `     `  `# Function to find the minimum distance ` `# between index of special pairs ` `def` `specialPairs(nums): ` `     `  `    ``# Stores the index of each element ` `    ``# in the array arr[] ` `    ``m ``=` `{} ` `    ``vals ``=` `[] ` ` `  `    ``for` `i ``in` `range``(``len``(nums)): ` `        ``m[nums[i]] ``=` `i ` `         `  `    ``for` `p ``in` `m: ` `        ``vals.append(p) ` `     `  `    ``res ``=` `sys.maxsize ` `     `  `    ``for` `i ``in` `range``(``1``, ``len``(vals)): ` `        ``vec ``=` `[m[vals[i]]] ` `         `  `        ``# Take adjacent difference ` `        ``# of same values ` `        ``for` `i ``in` `range``(``1``, ``len``(vec)): ` `            ``res ``=` `min``(res, ` `                      ``abs``(vec[i] ``-` `vec[i ``-` `1``])) ` `             `  `        ``if` `(i): ` `            ``a ``=` `vals[i] ` `             `  `            ``# Left index array ` `            ``left ``=` `[m[a]] ` `             `  `            ``b ``=` `vals[i ``-` `1``] ` `             `  `            ``# Right index array ` `            ``right ``=` `[m[b]] ` `             `  `            ``# Find the minimum gap between ` `            ``# the two adjacent different ` `            ``# values ` `            ``res ``=` `min``(res, ` `                      ``mindist(left, right)) ``+` `1` `     `  `    ``return` `res ` ` `  `# Driver Code ` `if` `__name__ ``=``=` `"__main__"``: ` `     `  `    ``# Given array  ` `    ``arr ``=` `[ ``0``, ``-``10``, ``5``, ``-``5``, ``1` `] ` ` `  `    ``# Function call ` `    ``print``(specialPairs(arr)) ` ` `  `# This code is contributed by dadi madhav`

Output:

```2
```

Time Complexity: O(N*log N)
Auxiliary Space: O(N)

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.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.