-
Notifications
You must be signed in to change notification settings - Fork 110
/
solution.cpp
31 lines (30 loc) · 850 Bytes
/
solution.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
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<double> sorted(n);
map<double, int> arr;
for (int i = 0; i < n; ++i) {
double x;
cin >> x;
sorted[i] = x;
arr[x] = i;
}
//we have to sort the prices first
sort(sorted.begin(), sorted.end());
double min = INT_MAX;
for (int i = 0; i < n - 1; ++i) {
//check the difference of the adjacent pairs, if it is less than last min then update min, if index of those pairs are in the same order as original array
double x = sorted[i + 1] - sorted[i];
if (x < min) {
int first = arr[sorted[i]];
int second = arr[sorted[i + 1]];
if (second < first) {
min = x;
}
}
}
cout << long(min);
return 0;
}