You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

18 lines
386 B
C++

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 50;
int a[N];
int main()
{
int k, n;
scanf("%d%d", &n, &k);
for (int i = 0; i < n; i++)
scanf("%d", a + i);
sort(a, a + n);
int ans = a[n - 1] - a[0];
for (int i = k - 1; i < n; i++)
ans = min(ans, a[i] - a[i - k + 1]);
printf("%d", ans);
return 0;
}