object ABC115 {
  def main(args:Array[String]):Unit= {
    val sc = new java.util.Scanner(System.in)
    val N, K = sc.nextInt
    val h = Array.fill(N)(sc.nextInt()).sorted
    println(answer(K,1000000001,h,0))
  }
  def answer(K:Int,a:Int,l:Array[Int],m:Int):Int={
    l match{
      case xs if xs.length==K-1 => a
      case xs if xs(1)==m => answer(K,a,xs.drop(1),m)
      case xs => answer(K,Math.min(a,xs(K-1)-xs(0)),xs.drop(1),xs(0))
    }
  }
}