fun main(args: Array<String>) {
   	val(n, k) = readLine()!!.split(" ").map{ i -> i.toInt() }
    var h =  Array(n, {readLine()!!.toInt()})
    var min = 10e9.toInt()
  
    h.sortDescending()
   
    for ( i in 0..n-k ){
        var sub = h[i] - h [i + k - 1]
        
        if( sub < min){
           	min = sub
        }
    }
    println(min)
}