object Main {

  def main(args: Array[String]): Unit = {
    val sc = new java.util.Scanner(System.in)

    val N, K = sc.nextInt()
    val H = Array.fill(N)(sc.nextInt()).sorted

    var l = 1
    var r = K
    var min = H(K - 1) - H(0)
    while (r < N) {
      min = Math.min(min, H(r) - H(l))
      l += 1
      r += 1
    }
    println(min)
  }

}