"use strict";
process.stdin.resume();
process.stdin.setEncoding("utf8");

var lines = [];
var reader = require("readline").createInterface({
  input: process.stdin,
  output: process.stdout
});

reader.on("line", line => {
  lines.push(line);
});

reader.on("close", () => {
  let input = lines.shift().split(/\s/);
  let n = parseInt(input[0]),
    k = parseInt(input[1]);
  let h = lines.sort((a, b) => b - a);

  let min = Infinity;
  for (let i = k - 1; i < n; i++) {
    if (h[i - k + 1] - h[i] < min) min = h[i - k + 1] - h[i];
  }

  console.log(min);
});
