// WA
"use strict";
function main(input) {
  const inputs = input.split("\n").filter(x => x !== "");
  let N = Number(inputs[0].split(" ")[0]);
  let K = Number(inputs[0].split(" ")[1]);
  let hs = inputs.slice(1).map(h => Number(h));

  let temp = hs.sort((a, b) => a - b);
  console.error(temp);

  temp = temp.map((h, index, hs) => {
    if (index + K - 1 < hs.length) {
      return hs[index + K - 1] - hs[index];
    }
    return null;
  });
  console.error(temp);

  temp = temp.filter(x => x !== null);

  console.error(temp);

  console.log(Math.min(...temp));
}

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