-
Notifications
You must be signed in to change notification settings - Fork 80
/
max_min.cpp
46 lines (31 loc) · 842 Bytes
/
max_min.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 <bits/stdc++.h>
using namespace std;
static bool sortFn(int a, int b){return a < b;}
static int diff = INT_MAX;
int maxMin(int k, vector<int> arr) {
sort(arr.begin(), arr.end(), sortFn);
for(int i = 0; i <= arr.size()-k; i++)
diff = min(arr[i+k-1]-arr[i], diff);
return diff;
}
int main()
{
ofstream fout(getenv("OUTPUT_PATH"));
int n;
cin >> n;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
int k;
cin >> k;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
vector<int> arr(n);
for (int i = 0; i < n; i++) {
int arr_item;
cin >> arr_item;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
arr[i] = arr_item;
}
int result = maxMin(k, arr);
fout << result << "\n";
fout.close();
return 0;
}