-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxMin.cpp
More file actions
33 lines (29 loc) · 751 Bytes
/
MaxMin.cpp
File metadata and controls
33 lines (29 loc) · 751 Bytes
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
#include <bits/stdc++.h>
//Imports all the major libraries^
int elements, k;
int j, i, h;
int n;
unsigned long int tot = 10000000000000;
int cur;
std::vector<int> arr;
int main(){
std::cin >> elements;
std::cin >> k;
for(i = 0; i < elements; ++i){
std::cin >> n;
arr.push_back(n);
n = 0;
}
std::sort(arr.begin(), arr.end());
//After sorted we should create a for-loop that moves through groups of k, then find the min and max and subtract, we store it in tot and do it until the end
for(j = 0; j < elements - (k - 1); ++j){
cur = (arr[j + (k - 1)]) - (arr[j]);
if(cur < tot){
tot = cur;
}
cur = 0;
}
std::cout << tot;
return 0;
}
//Testing