[<AutoOpen>]
module Program

open System

let read f = stdin.ReadLine() |> f
let reads f = stdin.ReadLine().Split() |> Array.toList |> List.map f

let f s xs =
    let d = List.head xs - List.last xs
    if d < s then d else s

[<EntryPoint>]
let main _ =
    let [n;k] = reads int
    let hs = Array.map (fun _ -> read int) [|1..n|]
    let xs = Array.sortDescending hs
    [
        for i = 0 to n - k do
            let j = i + (k - 1)
            yield xs.[i] - xs.[j]
    ]
    |> List.min
    |> printfn "%i"
    0