object Main extends App {
  val sc = new java.util.Scanner(System.in)
  val N, K = sc.nextInt()
  val h = Array.fill(N)(sc.nextInt()).sorted
  println((0 to N - K).fold(Int.MaxValue)((acc, i) => (h(i + K - 1) - h(i)) min acc))
}
