function main(input) {
  var N = input[0].split(" ")[0]-0;
  var K = input[0].split(" ")[1]-0;

  var map = {};
  var maxsame = 0;
  var min = max = input[1];
  for(var i=1;i<=N;i++){
    var t = input[i]-0;
    if(!map[t])map[t] = 1;
    else map[t]++;
    maxsame = Math.max(maxsame, map[t]);
    min = Math.min(t, min);
    max = Math.max(t, max);
  }
  if(maxsame>=K) console.log(0);
  else{
    var ans = max-min;
    var keys = Object.keys(map).sort((a,b)=>a-b);
    var nummap = [];
    nummap[0] = map[keys[0]];
    for(var i=1;i<keys.length;i++){
      nummap[i] = map[keys[i]] + nummap[i-1];
    }
    start:for(var i=0;i<keys.length;i++){
      var first = keys[i];
      var tmpnum = map[first];
      var lastIdx = bsearch(nummap, K+nummap[i], i);
      if(lastIdx>N) continue;
      var last = keys[lastIdx];
      console.log(lastIdx, last);
      ans = Math.min(last-first, ans);
    }
    console.log(ans);
  }
}

function bsearch(arr, targ, min, max){
  var min=min||0,max=max||arr.length;
  var mid = ~~((min+max)/2);
  while(min<max){
    mid = ~~((min+max)/2);
    if(arr[mid]<targ){
      min = mid+1;
    }else if(arr[mid]>targ){
      max = mid-1;
    }else{
      return mid;
    }
  }
  return max;
}

main(require('fs').readFileSync('/dev/stdin', 'utf8').trim().split("\n"))
