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++ (GCC 9.2.1) (judge verdict: Accepted)

#include <iostream>
#include <bits/stdc++.h>
#define mod 1000000007
#define point complex<long long>
#define pi acos(-1)
#define pb push_back


typedef long long ll;

using namespace std;

void Fastio(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
}

long long adotb(point a, point b){
    return (conj(a)*b).real();
}

long long acrossb(point a, point b){
    return (conj(a)*b).imag();
}

long long sinseta(point a, point b){
    return abs(acrossb(a,b)/(abs(a)*abs(b)));
}
ll fact[1000006], inv[1000006];

ll fp(ll base, ll exp)
{
    if (exp == 0)
        return 1;
    ll ans = fp(base, exp / 2);
    ans = (ans * ans) % mod;
    if (exp % 2 != 0)
        ans = (ans * (base % mod)) % mod;
    return ans;
}

void calcFacAndInv(ll n)
{
    fact[0] = inv[0] = 1;
    for (ll i = 1; i <= n; i++)
    {
        fact[i] = (i * fact[i - 1]) % mod;
        inv[i] = fp(fact[i], mod - 2);
    }
}

ll ncr(ll n, ll r)
{
    return ((fact[n] * inv[r]) % mod * inv[n - r]) % mod;
}

ll npr(ll n, ll r)
{
    return (fact[n] * inv[n - r]) % mod;
}

ll lcm(ll a, ll b){
    return (a*b)/__gcd(a,b);
}
long double  NCR(int n, int m){
    if(n<m)return 0;
    long double res = 1;
    m = min(m, n - m);
    for(int i = 0; i < m; i++){
        res = res * ( n - i) / (i + 1);
 }
    return res;
}
ll stolong(string a){
    ll res=0;
    while(a[0]=='0'){
        a.erase(a.begin());
    }
    if(a=="")a="0";
    for(int i=0; i<a.size(); i++){
        res*=10; res+=a[i]-'0';
    }
    return res;
}
const int MAXN=1e7+2;
int spf[MAXN];
void sieve()
{
    spf[1] = 1;
    for (int i=2; i<MAXN; i++)
        spf[i] = i;

    for (int i=4; i<MAXN; i+=2)
        spf[i] = 2;

    for (int i=3; i*i<MAXN; i++)
    {
        if (spf[i] == i)
        {
            for (int j=i*i; j<MAXN; j+=i)
                if (spf[j]==j)
                    spf[j] = i;
        }
    }
}

set<int> getFactorization(int x)
{
    set<int> ret;
    while (x != 1)
    {
        ret.insert(spf[x]);
        x = x / spf[x];
    }
    return ret;
}
class data{
    public:
    ll x, y, d;
    /*bool operator <(data& d)const {
        return l<d.l || (l==d.l && r>=d.r);
    }*/

};

double distance(point a, point b){
    return abs(b-a);
}

string ltobin(int x){
    string res="";
    while(x){
        res+=('0'+(x%2)); x/=2;
    }
    reverse(res.begin(), res.end());
    return res;
}
ll bintol(string a){
    ll res=0;
    reverse(a.begin(), a.end());
    for(int i=0; i<a.size(); i++){
        if(a[i]=='1')res+=pow(2,i);
    }
    return res;
}
void primeFactors(int n)
{
    while (n % 2 == 0 && n)
    {
        cout<<2<<" ";
        n = n/2;
    }
    for (int i = 3; i <= sqrt(n); i = i + 2)
    {
        while (n % i == 0)
        {
            cout<<i<<" ";
            n = n/i;
        }
    }
    if (n > 2)
        cout<<n<<" ";
}

vector<int> Sieve(int n)
{
    int p[1000005];
    for(int i=2; i<=n; i++){
        for(int y=i+i; y<=n; y+=i){if(!p[y])p[y]=i;}
    }
    vector<int>v;
    for(int i=2; i<=n; i++){
        if(!p[i])v.push_back(i);
    }
    return v;
}

int main()
{
    Fastio();
    int ttt=1; //cin>>ttt;
    while(ttt--){
        ll n,m; cin>>n>>m;
        ll arr[n]={0};
        for(int i=1; i<=n; i++){cin>>arr[i];}
        sort(arr,arr+n+1);
        ll res=1e18;
        for(int i=m; i<=n; i++){
            res=min(res, arr[i]-arr[i-m+1]);
        }
        cout<<res<<'\n';
    }

    return 0;
}