fun main(args: Array<String>) {
    val (n,k) = readLine()!!.split(" ").map { num ->num.toInt() };    
    val h = Array(n, {readLine()!!.toInt() })
  
    h.sort();
    var ret = h[h.size-1] - h[0];
  
    for (i in 0..n-k){
  	  val r = h[i + k - 1] - h[i];
      if(r < ret){
        ret = r;
      }
    }
  
    println(ret);
}