object Main extends App {
  
  val sc = new java.util.Scanner(System.in)
  val n, k = sc.nextInt()
  val h = Array.fill(n)(sc.nextInt()).sorted
  var ans = 100000
  for (i <- 0 to h.length - k) yield {
    val h2 = h.slice(i, i+k)
    val diff = h2.max - h2.min
    if (diff < ans) ans = diff
  }
  println(ans)
}