import std.stdio, std.math, std.algorithm, std.array, std.string, std.conv, std.container, std.range;
T Read(T)() { return readln.chomp.to!(T); }
T[] Reads(T)() { return readln.split.to!(T[]); }
alias read = Read!(int);
alias reads = Reads!(int);


void main() {
    int n, k; readf("%d %d\n", &n,&k);
    int[] h = new int[n];
    foreach (ref hi ; h)
        hi = read();
    h.sort();

    int minh = 1145141919;
    foreach (i; 0 .. n - k + 1)
        minh = min(minh, h[i + k - 1] - h[i]);
    writeln(minh);
}
