import java.util.*
fun main(args: Array<String>) {

    val sc = Scanner(System.`in`)
    val N = sc.nextInt()
    val K = sc.nextInt()
    val H = LongArray(N){0L}
    for (i in 0 until N) {
        val h = sc.nextLong()
        H[i] = h
    }
    H.sort()
    var ans = 1000000001L
    for (i in 0 .. N-K) {
        val dh = H[K+i-1]- H[i]
        if (ans > dh) ans = dh
    }

    println(ans)
}
