function Main(input) {
  var tmp = input.trim().split('\n')
  var N = parseInt(tmp[0].split(' ')[0])
  var K = parseInt(tmp[0].split(' ')[1])
  tmp.shift()
  var h = tmp

  var H = h.sort(function (a, b) { return parseInt(a) - parseInt(b) })

  var result = Number.MAX_SAFE_INTEGER

  for (var j = 0; j <= N - K; j++) {
    var v = H[j + K - 1] - H[j]
    if (result > v) {
      result = v
    }
  }

  console.log(result)
  return result
}

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