{-# LANGUAGE MultiWayIf       #-}
{-# LANGUAGE TypeApplications #-}

module Main where

import           Control.Monad
import           Data.Array
import           Data.Word     (Word64)
import           Data.Semigroup hiding (Arg(..), ArgMax, ArgMin)
import qualified Data.List.NonEmpty as N
import           Debug.Trace   (trace)

import qualified Data.List as L
import Data.Function (on)

-- abc115C

data Entry a = Entry
  { _id  :: Int
  , _val :: a
  } deriving Show

makeSortedArray
  :: (a -> a -> Ordering)
  -> Int
  -> [a]
  -> Array Int (Entry a)
makeSortedArray comp n vals =
  array (1,n) $ zip [1..]
    $ L.sortBy (comp `on` _val)
    $ zipWith Entry [1..] vals

main :: IO ()
main = do
  [_N,_K] <- words <$> getLine
  let n = read _N
      k = read _K
  hs  <- replicateM n (readLn @Int)
  let es = makeSortedArray compare n hs
      ans = search n k es
  print $ _val(es!(ans+k-1)) - _val(es!ans)

search :: Int -> Int -> Array Int (Entry Int) -> Int
search n k arr = ansIx
  where
    candidates = [1..n-k+1]
    f i = _val (arr!(i+k-1)) - _val (arr!i)
    Min (Arg ansIx _) = argMin f candidates

data Arg a b = Arg { _arg :: a, _ret :: b }
  deriving Show
instance (Eq b, Ord b) => Eq (Arg a b) where
  (==) = (==) `on` _ret
instance (Eq b, Ord b) => Ord (Arg a b) where
  compare = compare `on` _ret
type ArgMin a b = Min (Arg a b)
type ArgMax a b = Max (Arg a b)

argMin :: Ord b => (a -> b) -> [a] -> ArgMin a b
argMin f xs = sconcat $ N.map (\x -> Min (Arg x (f x))) (N.fromList xs)

