import Data.List
import Data.Maybe
import Control.Monad
import qualified Data.Vector.Unboxed as V

main :: IO ()
main = do
  [n,k] <- map read.words <$> getLine
  hs <- V.fromList . sort <$> (replicateM n $ (readLn :: IO Int))
  print $ minimum [ (hs V.! (i+k-1)) - (hs V.! i) | i <- [0..n-k]]