
import java.util
import java.util.Scanner
import scala.util.Failure
import scala.util.Success
import scala.util.Try

object Main extends App {
  val sc = new Scanner(System.in)
  def next(): String = sc.next()
  def nextInt(): Int = next().toInt
  def nextLong(): Long = next().toLong

  def task(): Unit = {
    val n, k = nextInt()
    val hs = Array.fill(n)(nextInt()).sorted
    println(hs)
    val map = new util.TreeMap[Int, Int]
    for (i <- 0 until k) {
      Try(map.get(hs(i))) match {
        case Failure(_) => map.put(hs(i), 1)
        case Success(v) => map.put(hs(i), v + 1)
      }
    }

    var ans = map.lastKey() - map.firstKey()
    for (i <- k until n) {
      val prev = map.remove(hs(i - k))
      if (prev != 1) {
        map.put(hs(i - k), prev - 1)
      }

      Try(map.get(hs(i))) match {
        case Failure(_) => map.put(hs(i), 1)
        case Success(v) => map.put(hs(i), v + 1)
      }

      ans = Math.min(ans, map.lastKey() - map.firstKey())
    }
    println(ans)
  }

  task()
}
