'use strict'

function main(input){
  const lines = input.split('\n');
  const N = parseInt(lines[0].split(' ')[0]);
  const K = parseInt(lines[0].split(' ')[1]);
  var hs = [];
  for(var i=0;i<N;i++){
    hs.push(parseInt(lines[i+1]));
  }
  const sorted_hs = hs.sort(function(a,b){return a-b;})
  var result = Math.max(...hs)-Math.min(...hs);
  for(var i=0;i<N-K+1;i++){
    var part_hs = sorted_hs.slice(i,i+K);
    var current_diff = Math.max(...part_hs) - Math.min(...part_hs);
    result = Math.min(result, current_diff)
  }
  //console.log(sorted_hs);
  console.log(result);
}

main(require('fs').readFileSync('/dev/stdin', 'utf8'));