# Closest Numbers from a List of Unsorted Integers

In the following article, we discuss two approaches to find the closest numbers from a list of unsorted integers. Let us first understand what is meant by the term ‘closest numbers’. Closest numbers are the pair(s) of numbers which have the least difference between them.

Problem Statement

Given a list of distinct unsorted integers, we need to find the pair of elements that have the least difference between them. If there are multiple pairs, we need to find all of them. Furthermore, in the article wherever there is a mention of difference, it means absolute difference.

Examples

Input: [44, 42, 40, 23, 55]
Output: Pairs with minimum difference are:
44 and 42
42 and 40


Explanation − The pairs with minimum difference are (44, 42) and (42, 40). The absolute difference between44 and 42 is 2, and the absolute difference between 42 and 40 is also 2.

Input: [500, 200, 400, 300, 100]
Output: Pairs with minimum difference:
100 and 200
200 and 300
300 and 400
400 and 500


Explanation − In this case, all pairs have the same difference of 100, so all pairs are printed.

Input: [-13, 23, 79, -71, 37]
Output: Pairs with minimum difference:
23 and 37


Explanation − The pairs with minimum difference are (23, 37). The absolute difference between 23 and 37 is 14, which is the minimum absolute difference in this list.

## Naive Approach

The brute-force approach to solve this problem would be to compare every pair of elements in the list and calculate their absolute difference.

The approach consists of the following steps −

• Read the input list of integers

• Set min_diff to INT_MAX

• Create two variables, min_i and min_j, to store the indices of the pair with minimum difference

• Loop through the list, comparing each element with every other element

• If the absolute difference between the two elements is less than min_diff, update min_diff and store the indices of the two elements in min_i and min_j

• If the absolute difference is equal to min_diff, append the indices to a list of pairs

• After completing the loop, print the pairs with minimum absolute difference

### Example: C++ Program

The C++ program finds the pair of numbers with the absolute minimum difference from a list of distinct unsorted integers.

#include <iostream>
#include <vector>
#include <cmath>
#include <climits>

using namespace std;
int main() {
vector<int> nums = {4, 5, 1, 7};
int min_diff = INT_MAX;
int n = nums.size();
vector<pair<int, int>> p;
for (int i = 0; i < n-1; i++) {
int diff = 0;
for (int j = i+1; j < n; j++) {
diff = abs(nums[i] - nums[j]);
if (diff < min_diff) {
min_diff = diff;
p.clear();
p.push_back(make_pair(i, j));
}
else if (diff == min_diff) {
p.push_back(make_pair(i, j));
}
}
}

for (auto x : p) {
cout << nums[x.first] << ", " << nums[x.second] << endl;
}
return 0;
}


### Output

4, 5


## Time and Space Complexity Analysis

### Time complexity: O(n2)

The time complexity of the given code is O(n^2). This is because we are using two nested loops to compare each element of the list with every other element. The outer loop runs (n-1) times, and the inner loop runs (n - i – 1) times, where n is the size of the list. This results in a total of (n - 1) + (n - 2) + ... + 1 = n(n - 1) / 2 comparisons, which is O(n^2) time complexity.

### Space complexity: O(k)

The space complexity of the given code is O(k), where k is the number of pairs with minimum absolute difference. This is because we are storing the indices of the pairs in a vector of pairs. In the worst case, where all pairs have the same minimum absolute difference, k can be as large as n(n-1)/2, which is also the maximum number of pairs possible in a list of n elements. Therefore, the space complexity of the program is O(n^2).

## Optimized Approach

To optimize the solution, we can first sort the list, which would take O(nlog(n)) time, and then compare adjacent elements to find the minimum absolute difference.

The approach consists of the following steps −

• Sort the list of integers in ascending order.

• Initialize two variables 'min_diff' and 'pairs' to store the minimum absolute difference and the pairs with the same minimum absolute difference, respectively.

• Traverse the sorted list and compare adjacent elements to find the minimum absolute difference.

• If a smaller absolute difference is found, update the 'min_abs_diff' and clear the 'pairs' list and add the current pair to it.

• If the same absolute difference is found, add the current pair to the 'pairs' list.

• Finally, output the 'pairs' list.

### Example: C++ Program

This program code sorts the input list using the in-built sort() function. It then computes the difference between each adjacent pair of integers to find the answer.

#include <iostream>
#include <vector>
#include <algorithm>
#include <climits>

using namespace std;
int main(){
vector<int> nums = {12, 3, 19, 15, 6, 8, 1};
sort(nums.begin(), nums.end());
int minDiff = INT_MAX;
vector<pair<int, int>> p;

// iterate through the list and find the minimum absolute difference and pairs
for (int i = 1; i < nums.size(); i++) {
int diff = abs(nums[i] - nums[i - 1]);
if (diff < minDiff) {
minDiff = diff;
p.clear();
p.push_back(make_pair(nums[i - 1], nums[i]));
}
else if (diff == minDiff) {
p.push_back(make_pair(nums[i - 1], nums[i]));
}
}

for (auto x : p)    {
cout << x.first << ", " << x.second << endl;
}
cout << endl;
return 0;
}


### Output

1, 3
6, 8


## Time and Space Complexity Analysis

### Time Complexity: O(nlog(n))

The time complexity of this code is O(nlog(n)), where n is the number of integers in the input list. This is because the code first sorts the input list, which takes O(nlog(n)) time, and then iterates through the sorted list to find the pairs with the minimum absolute difference, which takes O(n) time. Since O(nlog(n)) is greater than O(n), the overall time complexity is O(nlog(n)).

### Space Complexity: O(k)

The space complexity of this code is O(k), where k is the number of pairs with the minimum absolute difference. This is because the code uses a vector to store the pairs with the minimum absolute difference, and the maximum size of this vector is k.

## Conclusion

The problem statement given is to find the pair of elements that have the smallest absolute difference between them in a list of distinct unsorted integers. This article discusses the problem statement, provides examples of multiple input and expected output senarios, and offers a brute force solution and an efficient solution approach to solve this problem. The article also includes the algorithm used and the C++ programs to implement the solution approach. Finally, the article also provides a detailed discussion on the time and space complexity of the solutions.

Updated on: 19-Apr-2023

571 Views