import Control.Monad
import Data.List

main :: IO ()
main = do
  [n, k] <- map read . words <$> getLine
  hs <- replicateM n readLn
  print $ solve k hs

solve :: Int -> [Int] -> Int
solve k hs = minimum $ zipWith (-) hs2 hs1
  where
    hs1 = sort hs
    hs2 = drop (k - 1) hs1