#include <bits/stdc++.h>
using namespace std;
using namespace std;
vector<int> v;
int ans = 100000;
int main() {
  int a, d;
  cin >> a >> d;
  for (int i = 0; i < a; i++) {
    int x;
    scanf("%d", &x);
    v.push_back(x);
  }
  sort(v.begin(), v.end());
  for (int i = 0; i < v.size(); i++) {
    int cnt = i - 0;
    int p = d - cnt;
    if (cnt > d) break;
    ans = min(ans, (v[a - p - 1] - v[i]));
  }
  cout << ans << endl;
}
