import java.util.Scanner

object Main {
  def main(args: Array[String]): Unit = {
    val sc = new Scanner(System.in)
    val N, K = Integer.parseInt(sc.next())
    val h = new Array[Long](N)
    for (i <- 0 until N) {
      h(i) = Integer.parseInt(sc.next())
    }

    println(solve(N, K, h))
  }

  def solve(N: Int, K: Int, h: Array[Long]): String = {
    val sortedH = h.sorted

    var ans = sortedH(N - 1)
    for (i <- 0 to N - K) {
      ans = Math.min(ans, sortedH(K - 1 + i) - sortedH(i))
    }
    ans.toString
  }
}