import java.util.Scanner
import scala.util.control.Breaks

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

    val N, K = sc.nextInt
    var h = Array.ofDim[Int](N)
    (0 until N).foreach(i => {
      h(i) = sc.nextInt
    })

    h = h.sorted
    var ans = Int.MaxValue
    (0 until N-K+1).foreach(i => {
      ans = Math.min(ans, h(i+K-1) - h(i))
    })

    println(ans)
  }
}