
import java.util.Scanner

object Main extends App {

  val sc = new Scanner(System.in)

  val n, k = sc.nextInt()

  val ar = (for {
    _ <- 0 until n
  } yield sc.nextInt())
    .sorted

  val ans = (for {
    idx <- 0 until  n - k + 1
  } yield ar(idx + k - 1) - ar(idx))
    .min

  println(ans)
}
