#include <bits/stdc++.h>
using namespace std;
int main() {
  cin.tie(0), cout.tie(0);
  ios::sync_with_stdio(false);
  int n, a[105] = {}, d, cnt = 0, ans = 1e9;
  cin >> n >> d;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a, a + n);
  if (d == 0) {
    cout << a[n - 1] - a[0] << endl;
    return 0;
  }
  if (n == 1) {
    cout << 0 << endl;
    return 0;
  }
  for (int i = 0; i < d + 1; i++) {
    cnt = a[i + n - d - 1] - a[i];
    ans = min(ans, cnt);
  }
  cout << ans << endl;
}
