# frozen_string_literal: true

n, k = gets.split.map(&:to_i)
heights = n.times.map { gets.to_i }.sort

min_diff = Float::INFINITY

heights.each_with_index {|min, i|
  max = heights[k + i -1]
  break unless max

  diff = max - min
  min_diff = diff if diff < min_diff
}

puts min_diff
