local inp = io.read()
local n,k
for i=1,string.len(inp),1 do
  if inp:sub(i,i)==' ' then
    n = tonumber(inp:sub(1,i-1))
    k = tonumber(inp:sub(i+1,string.len(inp)))
  end
end
local h = {}
for i=1,n,1 do
  h[i] = tonumber(io.read())
end
table.sort(h)
local ans=1000000000
for i=1,n-k+1,1 do
  if ans>h[i+k-1]-h[i] then
    ans = h[i+k-1]-h[i]
  end
end
print(ans)