let rec seq a b = if a >= b then [] else a :: seq (a+1) b
let () =
  Scanf.scanf "%d %d" @@ fun n k ->
  let h = Array.init n (fun _ -> Scanf.scanf " %d" ((+) 0)) in
  Array.sort (-) h;
  seq 0 (n+1-k) |> List.map (fun i -> h.(i+k-1) - h.(i))
  |> List.fold_left min h.(n-1)
  |> Printf.printf "%d\n"
    
