'use strict'

function main(input) {
  input = input.split('\n')
  input[0] = input[0].split(' ')
  const n = ~~input[0][0]
  const k = ~~input[0][1]

  const tree = input
    .slice(1, -1)
    .map(v => ~~v)
    .sort((a, b) => a - b)

  // i-1+k本の差を収納
  const diff = Array.apply(null, { length: n - (k - 1) }).fill(-1)
  let min = Number.MAX_SAFE_INTEGER
  let tmp = 0
  for (let i = 0; i < diff.length; i++) {
    // diff.push()
    tmp = tree[i + k - 1] - tree[i]
    min = min > tmp ? tmp : min
  }
  console.log(min)
}
main(require('fs').readFileSync('/dev/stdin', 'utf8'))