n, k = gets.split(' ').map(&:to_i)
arr = []
n.times do
  arr << gets.to_i
end
arr.sort!
arr.reverse!
min = 1000000001
(0...(n - k + 1)).each do |i|
  min = [min, arr[i] - arr[i + k - 1]].min
end
puts min