-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclosest.cpp
executable file
·46 lines (36 loc) · 961 Bytes
/
closest.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include<iostream>
//Given a list of unsorted numbers, can you find the numbers that have the smallest absolute difference between them?
//If there are multiple pairs, find them all.
#include<algorithm>
#include<cmath>
#include<cstdint>
int main(int argc, char *argv[]){
unsigned int n;
int64_t *a;
std::cin>>n;
if( n == 0) return 0;
a = new int64_t[n];
if( n == 1){
delete[] a;
std::cout<<a[0];
return 0;
}
for(unsigned int i = 0 ; i < n; i++){
std::cin>>a[i];
}
std::sort(a,a+n);
int64_t min = abs(a[1] - a[0]);
for(unsigned int i = 1 ; i < n;i++){
int64_t curMin = abs(a[i-1] - a[i]);
if(curMin < min){
min = curMin;
}
}
for(unsigned int i = 1; i < n; i++){
if(abs(a[i-1] - a[i]) == min){
std::cout<<a[i-1]<<" "<<a[i]<<" ";
}
}
delete[] a;
return 0;
}