import java.util.Scanner

object Main {
  def main(args: Array[String]): Unit = {
    val sc = new Scanner(System.in)
    val Array(n, k) = sc.nextLine.split(" ").map(_.toInt)
    val trees = List.fill(n)(sc.nextLine.toInt).sorted
    println((0 to n - k).map(i => trees(i + k - 1) - trees(i)).min)
  }
}
