forked from Perilynn/HackerRank-Coding-Challenges
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroy.cpp
39 lines (32 loc) · 784 Bytes
/
roy.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
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <limits>
using namespace std;
int main() {
int size;
vector<int> numbers;
cin >> size;
//get ints
for (int i = 0; i < size; i++) {
int j;
cin >> j;
numbers.push_back(j);
}
//sort
sort(numbers.begin(), numbers.end());
//find min gap
int mingap = numeric_limits<int>::max();
for (int i = 0; i < size - 1; i++) {
if (numbers[i+1] - numbers[i] < mingap)
mingap = numbers[i+1] - numbers[i];
}
//print
for (int i = 0; i < size - 1; i++) {
if (numbers[i+1] - numbers[i] == mingap)
cout << numbers[i] << " " << numbers[i+1] << " ";
}
return 0;
}