import Foundation

extension String {
    func wordsMap<T>(_ f: (Substring) -> T?) -> [T] {
        return self.split(separator: " ").compactMap(f)
    }
}

private func readWords<T>(_ f: (Substring) -> T?) -> [T] {
    return readLine()!.wordsMap(f)
}

prefix operator *
private prefix func *<T>(a: inout IndexingIterator<[T]>) -> T {
    return a.next()!
}

extension Optional {
    func pleaseAC(){
        print(self!)
    }
}

readLine().map { line -> Any in 
    let nk = line.wordsMap { Int($0) }
    let n = nk[0]
    let k = nk[1]

    let H = (1...n).map { _ in Int(readLine()!)! }.sorted()
    var mini = Int.max
    for i in 0...(n-k) {
        mini = min(mini, H[i+k-1] - H[i])
    }

    return mini
}
.pleaseAC()
