#include <bits/stdc++.h>
using namespace std;
int v1[300100][19], v2[300100][19];
int mm[300100];
int a[300100];
int n, kth;
void init() {
  mm[0] = -1;
  for (int i = 1; i <= n; ++i) {
    mm[i] = ((i & (i - 1)) == 0) ? mm[i - 1] + 1 : mm[i - 1];
    v1[i][0] = v2[i][0] = a[i];
  }
  for (int j = 1; j <= mm[n]; ++j)
    for (int i = 1; i + (1 << j) - 1 <= n; ++i) {
      v1[i][j] = max(v1[i][j - 1], v1[i + (1 << (j - 1))][j - 1]);
      v2[i][j] = min(v2[i][j - 1], v2[i + (1 << (j - 1))][j - 1]);
    }
}
int cal(int x, int y) {
  int k = mm[y - x + 1];
  int MAX = max(v1[x][k], v1[y - (1 << k) + 1][k]);
  int MIN = min(v2[x][k], v2[y - (1 << k) + 1][k]);
  return MAX - MIN;
}
int bit[300100];
void add(int pos, int val) {
  while (pos <= n) {
    bit[pos] += val;
    pos += pos & (-pos);
  }
}
int sum(int pos) {
  int ret = 0;
  while (pos > 0) {
    ret += bit[pos];
    pos -= pos & (-pos);
  }
  return ret;
}
int seg(int l, int r) { return sum(r) - sum(l - 1); }
bool judge(int x) {
  memset(bit, 0, sizeof(bit));
  int v = cal(1, kth);
  if (v > x) return false;
  add(kth + 1, 1);
  add(1, 1);
  for (int i = kth + 1; i <= n; ++i) {
    v = cal(i - kth + 1, i);
    if (v > x) continue;
    int lb = 0, ub = i - kth + 1;
    while (ub > lb + 1) {
      int mid = (lb + ub) >> 1;
      if (cal(mid, i) <= x)
        ub = mid;
      else
        lb = mid;
    }
    if (seg(ub, i - kth + 1) > 0) {
      if (i < n)
        add(i + 1, 1);
      else
        return true;
    }
  }
  return false;
}
int main() {
  scanf("%d%d", &n, &kth);
  for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
  sort(a + 1, a + 1 + n);
  if (kth == n) {
    printf("%d\n", a[n] - a[1]);
    exit(0);
  }
  if (kth == 1) {
    puts("0");
    exit(0);
  }
  init();
  int lb = -1, ub = 1000000000;
  while (ub > lb + 1) {
    int mid = (lb + ub) >> 1;
    if (judge(mid))
      ub = mid;
    else
      lb = mid;
  }
  printf("%d\n", ub);
  return 0;
}
