import Control.Monad
import Data.List (sortBy) 

solve::Int->[Int]->Int
solve k hn = min (sortTree !! (k-1) - sortTree !! 0) (last sortTree - sortTree !! (len - k))
              where 
                sortTree = sortBy compare hn
                len = length hn

main = do
  [n, k] <- map read . words <$> getLine
  hn <- replicateM n $ read <$> getLine::IO[Int]
  print $ solve k hn