import java.util.Scanner

import scala.annotation.tailrec

object Util {
  @tailrec
  def loop(start: Int, end: Int)(f: Int => Unit) {
    if (start < end) {
      f(start)
      loop(start + 1, end)(f)
    }
  }
}

object Main {
  def main(args: Array[String]): Unit = {
    //nextLineとnextを同時に使ったら死ぬ
    val sc = new Scanner(System.in)
    val n, k = sc.nextInt()
    val hs = Array.fill(n)(sc.nextInt()).sorted
    var ans = Long.MaxValue
    for (i <- 0 to (n - k)) {
      ans = math.min(ans, hs(i + k - 1) - hs(i))
    }

    println(ans)
  }
}
