Problem:
Score : 300 points

Problem Statement

In some other world, today is Christmas Eve.

There are N trees planted in Mr. Takaha's garden. The height of the i-th tree (1 \leq i \leq N) is h_i meters.

He decides to choose K trees from these trees and decorate them with electric lights. To make the scenery more beautiful, the heights of the decorated trees should be as close to each other as possible.

More specifically, let the height of the tallest decorated tree be h_{max} meters, and the height of the shortest decorated tree be h_{min} meters. The smaller the value h_{max} - h_{min} is, the better. What is the minimum possible value of h_{max} - h_{min}?

Constraints

2 \leq K < N \leq 10^5

1 \leq h_i \leq 10^9

h_i is an integer.

Input

Input is given from Standard Input in the following format:

N K
h_1
h_2
:
h_N

Output

Print the minimum possible value of h_{max} - h_{min}.

Sample Input 1

5 3
10
15
11
14
12

Sample Output 1

2

If we decorate the first, third and fifth trees, h_{max} = 12, h_{min} = 10 so h_{max} - h_{min} = 2. This is optimal.

Sample Input 2

5 3
5
7
5
7
7

Sample Output 2

0

If we decorate the second, fourth and fifth trees, h_{max} = 7, h_{min} = 7 so h_{max} - h_{min} = 0. This is optimal.

There are not too many trees in these sample inputs, but note that there can be at most one hundred thousand trees (we just can't put a sample with a hundred thousand lines here).

Solution in C# (Mono 4.6.2.0) (judge verdict: Accepted)

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using static System.Console;
using static System.Math;
using static Extension;
class AC {
    /*------------------------------------------------------------------*/
    int N, K;
    int[] h;
    void Solve() {
        io.i(out N, out K);
        io.ini(out h, N);
        Array.Sort(h);
        var ans = int.MaxValue;
        (h.Length - K + 1).Rep(i => {
            var min = h[i];
            var max = h[i + K - 1];
            ans = MIN(ans, max - min);
        });
        ans.Prt();
    }
    /*------------------------------------------------------------------*/
    void Test() {
        //--- TEST ---//
    }
    public static void Main(string[] args) => new AC().Stream();
    IO io = new IO();
    private void Stream() {
        /*
        var exStdIn = new System.IO.StreamReader("stdin.txt");
        SetIn(exStdIn);
        Test();
        */
        Solve();
        io.Flush();
    }
}
class IO {
    #region INPUT
    #region FastRead
    private const int Size = 1 << 12;
    private readonly Stream _stream = OpenStandardInput();
    private readonly byte[] _buffer = new byte[Size];
    private int _ptr = 0, _buffLen = 0;
    private bool HasNextByte() {
        if (_ptr < _buffLen) return true;
        _ptr = 0;
        _buffLen = _stream.Read(_buffer, 0, Size);
        return _buffLen > 0;
    }
    private byte ReadByte() {
        if (HasNextByte()) return _buffer[_ptr++];
        else
            //inputの末尾の改行がないとerror
            throw new EndOfStreamException();
    }
    private bool IsAscii(int c) => '!' <= c && c <= '~';
    private char NextChar() {
        var b = ReadByte();
        while (!IsAscii(b)) b = ReadByte();
        return (char)b;
    }
    private string Next() {
        var sb = new StringBuilder();
        for (var b = NextChar(); IsAscii(b); b = (char)ReadByte()) sb.Append(b);
        return sb.ToString();
    }
    public string String => Next();
    public char Char => NextChar();
    public int Int => int.Parse(Next());
    public long Long => long.Parse(Next());
    public double Double => double.Parse(Next());
    #endregion
    #region LazyRead
    private void scan<T>(ref T x) {
        if (x is int) __refvalue(__makeref(x), int) = int.Parse(Next());
        else if (x is long) __refvalue(__makeref(x), long) = long.Parse(Next());
        else if (x is double) __refvalue(__makeref(x), double) = double.Parse(Next());
        else if (x is char) __refvalue(__makeref(x), char) = NextChar();
        else __refvalue(__makeref(x), string) = Next();
    }
    public void i<T>(out T t) {
        t = default(T);
        scan(ref t);
    }
    public void i<T, TU>(out T t, out TU u) {
        i(out t);
        i(out u);
    }
    public void i<T, TU, TV>(out T t, out TU u, out TV v) {
        i(out t, out u);
        i(out v);
    }
    public void i<T, TU, TV, TW>(out T t, out TU u, out TV v, out TW w) {
        i(out t, out u);
        i(out v, out w);
    }
    public void i<T, TU, TV, TW, TX>(out T t, out TU u, out TV v, out TW w, out TX x) {
        i(out t, out u, out v);
        i(out w, out x);
    }
    public void i<T, TU, TV, TW, TX, TY>(out T t, out TU u, out TV v, out TW w, out TX x, out TY y) {
        i(out t, out u, out v);
        i(out w, out x, out y);
    }
    public void ini<T>(out T[] a, int n) {
        a = new T[n];
        for (int i = 0; i < n; ++i) i<T>(out a[i]);
    }
    public void ini<T, TU>(out T[] a, out TU[] b, int n) {
        a = new T[n];
        b = new TU[n];
        for (int i = 0; i < n; ++i) i<T, TU>(out a[i], out b[i]);
    }
    public void ini<T, TU, TV>(out T[] a, out TU[] b, out TV[] c, int n) {
        a = new T[n];
        b = new TU[n];
        c = new TV[n];
        for (var i = 0; i < n; ++i) i<T, TU, TV>(out a[i], out b[i], out c[i]);
    }
    public void ini<T>(out T[,] a, int h, int w) {
        a = new T[h, w];
        for (int i = 0; i < h; ++i) for (int j = 0; j < w; ++j) i<T>(out a[i, j]);
    }
    #endregion
    #endregion
    #region OUTPUT
    private readonly StreamWriter _sw = new StreamWriter(OpenStandardOutput()) {
#if DEBUG
        AutoFlush = true
#else
        AutoFlush = false
#endif
    };
    public void Flush() => Out.Flush();
    public void YN(bool f) => yn(f, "YES", "NO");
    public void Yn(bool f) => yn(f, "Yes", "No");
    public void yn(bool f, string y = "yes", string n = "no") => WriteLine(f ? y : n);
    #endregion
    public IO() { SetOut(_sw); }
}
static class LinqExtension {
    public struct IndexedValue<T> {
        public int Index;
        public T Value;
        public IndexedValue(int index, T value) {
            this.Index = index;
            this.Value = value;
        }
        public override string ToString() {
            return string.Format("Index: {0}, Value: {1}", Index, Value);
        }
    }
    public struct Pair<T> {
        public T Fst;
        public T Snd;
        public Pair(T fst, T snd) {
            Fst = fst;
            Snd = snd;
        }
        public override string ToString() {
            return string.Format("({0}, {1})", Fst, Snd);
        }
    }
    public static IEnumerable<IndexedValue<T>> Indexed<T>(this IEnumerable<T> source) {
        return source.Select((value, index) => new IndexedValue<T>(index, value));
    }

    public static void Iter<T>(this IEnumerable<T> source, Action<T> action) {
        foreach (var s in source) action(s);
    }

    public static IEnumerable<Pair<T>> PairWise<T>(this IEnumerable<T> source) {
        return source.Zip(source.Skip(1), (fst, snd) => new Pair<T>(fst, snd));
    }

    public static T Then<T>(this T source, Action<T> action) {
        if (source == null) return source;
        action(source);
        return source;
    }

}
static class Extension {
    public static void FOR(int a, int b, int n, Action<int> f) {
        for (var i = a; i < b; i += n) f(i);
    }
    public static void FOR(int a, int b, Action<int> f) => FOR(a, b, 1, f);
    public static void REP(int a, Action<int> f) => FOR(0, a, 1, f);
    public static void REP(int a, int n, Action<int> f) => FOR(0, a, n, f);
    public static void FOREQ(int a, int b, Action<int> f) => FOR(a, b + 1, 1, f);
    public static void FOREQ(int a, int b, int n, Action<int> f) => FOR(a, b + 1, n, f);
    public static void REPEQ(int a, Action<int> f) => FOR(0, a + 1, 1, f);
    public static void REPEQ(int a, int n, Action<int> f) => FOR(0, a + 1, n, f);

    public static void Rep(this int a, Action<int> f) => FOR(0, a, 1, f);
    public static void Rep(this int a, int n, Action<int> f) => FOR(0, a, n, f);
    public static void RepEQ(this int a, Action<int> f) => FOR(0, a + 1, 1, f);
    public static void RepEQ(this int a, int n, Action<int> f) => FOR(0, a + 1, n, f);
    public static void Prt<T>(this T x) => WriteLine(x);
    public static void Prt<T>(this IEnumerable<T> xs) => xs.Iter(x => WriteLine(x));
    public static void Prt_<T>(this IEnumerable<T> xs) {
        xs.Iter(x => WriteLine($"{x} "));
        WriteLine();
    }

    public static T MIN<T>(params T[] xs) => xs.Min();
    public static T MAX<T>(params T[] xs) => xs.Max();

}