var toInt = x=>parseInt(x,10);
var toIntArr = arr=>arr.map(x=>toInt(x));
var abs = x=>x>0?x:-x;
function min(){var rest = arguments,val=rest[0];for(var i=1;i<rest.length;i++)if(rest[i]<val)val = rest[i];return val}
function max(){var rest = arguments,val=rest[0];for(var i=1;i<rest.length;i++)if(rest[i]>val)val = rest[i];return val}
Main(require("fs").readFileSync("/dev/stdin", "utf8"));
 
function Main(input){
	var input = input.split('\n');
	input = input.map(e => toIntArr(e.split(' ')));
	var n = input[0][0];
	var k = input[0][1];
	var hs = [];
	for(var i = 0; i < n; i++){
		hs[i] = input[i+1][0];
	}
	hs.sort((a,b)=>a-b);
	var ans = hs[k-1] - hs[0];
	//console.log(hs);
	for(var i = 1; i < n-k+1; i++){
		ans = min(ans,hs[k-1+i] - hs[i]);
		//console.log(k+i-1,i,ans);
	}
	
	console.log(ans);
}