#include<iostream>
#include<algorithm>
using namespace std;
int main() {
	int N, K, h[100000], dist = 2147483647;
	cin >> N >> K;
	for (int i = 0;i < N;++ i) cin >> h[i];
	sort(h, h+N); // 先にソートするよ！
	for (int i = K - 1;i < N;dist = min(dist, h[i] - h[++i - K]));
	cout << dist;
	return 0;
}