'use strict';
const main = input => {
  const x = input[0].split(' ').map(val => parseInt(val)), n = x[0], k = x[1];
  input.shift();
  const h = input.map(val => parseInt(val, 10));
  h.pop();
  h.sort((x,y) => x-y);
  // console.log(h);
  let min = 10e9;
  for(let i=0; i<n-k+1; i++){
    if (min > h[i+k-1] - h[i]) min = h[i+k-1] - h[i];
  }
  console.log(min);
  
}
main(require("fs").readFileSync("/dev/stdin", "utf8").split('\n'));