import java.util.Scanner

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.nextLong()).sorted

    val acc = for (i <- k - 1 until n) yield {
      hs(i) - hs(i - k + 1)
    }

    println(acc.min)
  }

  def debug(args: Any*): Unit = {
    Console.err.println(args.mkString(" "))
  }
}
