-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAngry.cpp
28 lines (26 loc) · 925 Bytes
/
Angry.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
#include <cmath>
#include <cstdio>
#include <vector>
#include <map>
#include <iostream>
#include <algorithm>
using namespace std;
#define MAXVAL 1000000001 // 10^9 is max for this problem
int main() {
int n, k;
cin >> n >> k;
vector<int> candies;
for(int i = 0; i < n; i++) {
int num;
cin >> num;
candies.push_back(num);
}
// Sort the vector. Logic is this: You pick one min and one max from this vector, which is the 0th and k-1'th element.
// Then, you subtract them. Then you repeat this and find the minimum of this operation. That's the answer.
sort(candies.begin(), candies.begin()+n);
int result = MAXVAL; // so that the first minimum operation always turns into result
for(int i = 0; i < n-k; i++)
result = min(result, candies[k+i-1] - candies[i]); // k+i-1 because, i stops at n-k-1, this will stop at n-2
cout << result;
return 0;
}