import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;

T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;

int calc(int k, int[] h) {
    h.sort();
    int ans = int.max;
    for (int i = k - 1; i < h.length; i++) {
        ans = min(ans, h[i] - h[i - k + 1]);
    }
    return ans;
}

void main() {
    auto nk = readints;
    int n = nk[0], k = nk[1];
    int[] h;
    for (int i = 0; i < n; i++) h ~= readint;
    writeln(calc(k, h));
}
