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).



Sample input:
5 3
10
15
11
14
12


Sample output:
2

Submission status: Runtime Error

Solution code:
import pandas as pd
N, K = list(map(int, input().split()))
 
h = []
for i in range(N):
    h.append(int(input()))

h_sort = sorted(h)
df = pd.DataFrame(h_sort)
df2 = df.diff()
df3 = df2.rolling(K - 1).sum()
df4 = df3.dropna(how='all')

print(int(min(df4[0])))

Submission status: Runtime Error

Solution code:
import numpy as np
n, k = map(int, input().split())
hs = []
for i in range(n):
    hs.append(int(input()))
sorted_hs = np.sort(hs)
print(min([min(np.diff(sorted_hs[i::k-1])) for i in range(k)]))

Submission status: Runtime Error

Solution code:
N,K = map(int,input().split())
tree_list = [int(input()) for _ in range(N)]
tree_list = sorted(tree_list)
ans = 10**9
for i in range(N-K+1):
    compare = tree_list[i+2]-tree_list[i]
    if ans > compare:
        ans = compare
print(ans)

Submission status: Runtime Error

Solution code:
a, b = (int(i) for i in input().split()) 

list=[]
 
for i in range(a):
    list.append(int(input()))

new_list = sorted(list)

sabun = None

for i in range(len(new_list)):

    if b - i < 1:
        break

    bbb = new_list[i+b-1] - new_list[i]

    if sabun == None:
        sabun = bbb
        continue

    if sabun > bbb:
        sabun = bbb
    
print(sabun)

Submission status: Runtime Error

Solution code:
import statistics
import math
import numpy as np

N, K = map(int, input().split())
h = [int(input()) for i in range(N)]

h.sort()
median = statistics.median(h)
result = 10 ** 9

idx = np.abs(np.asarray(h) - median).argmin()
if h[idx] - h[idx-(K-1)] <= h[idx+(K-1)] - h[idx]:
    result = h[idx] - h[idx-(K-1)]
else:
    result = h[idx+(K-1)] - h[idx]
print(result)

Submission status: Runtime Error

Solution code:
import numpy as np
n, k = map(int, input().split())
a = [int(input()) for x in range(n)]
a.sort()
a = np.array(a)
print(np.min(a[k - 1:] - a[:k]))

Submission status: Runtime Error

Solution code:
NK = list(map(int, input().split()))
a = [0]*NK[0]
for i in range(len(a)):
    a[i] = int(input())
a.sort()

b = [0]*NK[1]
offsets = [0]*(NK[0]-NK[1]+1)
for i in range(len(offsets)):
    for j in range(NK[1]):
        b[j] = a[i+j]
    offsets[i] = b[K-1] - b[0]

print(min(offsets))

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
h = [int(input()) for i in range(n)]
h.sort()
ls = []
for i in range(k):
    ls.append(h[i+k-1] - h[i])
    if i+k > n:
        break
print(min(ls))

Submission status: Runtime Error

Solution code:
import numpy as np
from sys import stdin
N,K=[int(x) for x in stdin.readline().rstrip().split()]
data=[]
for i in range(N):
    data.append(int(input()))
data.sort()
data.reverse()
ans=float("inf")
for i in range(N-K+1):
    tmp=data[i]-data[i+K-1]
    if ans > tmp:

Submission status: Runtime Error

Solution code:
n,k = map(int, input().split())
h = [int(input()) for i in range(n)] 
h.sort()

crr_min = 10^9

for i in range(0,n-k+1):
    tmp = h[n-k+i] - h[0+i]
    if tmp<crr_min:
        crr_min=tmp

print(crr_min)

Submission status: Runtime Error

Solution code:
import numpy as np
N,K = map(int,input().split())
list = []
list_tmp = [0]*(K-1)
for i in range(N):
 list.append(int(input()))
 if i < N-K:
  list_tmp.append(list[i])
list = sorted(list);
list_tmp = sorted(list_tmp);
tmp = list[-K:];
tmp_list =  list_tmp[K:]
tmp = np.array(tmp)
tmp_list = np.array(tmp_list)
print(min(tmp - tmp_list))

Submission status: Runtime Error

Solution code:
#coding:utf-8
s=[int(i) for i in input().split()]
n=[]
for i in range(s[0]):
  n.append(int(input()))
n.sort()
sa=abs(n[0]-n[s[1]]-1)
for i in range(s[1]):
  print(n[1],n[i+s[1]-1])
  if(sa>abs(n[i]-n[i+s[1]-1])):
    sa=abs(n[i]-n[i+s[1]-1])
print(sa)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split(" "))

h = [int(input()) for i in range(N)]

sort_h = sorted(h)
diff_h = 0
ans = []
for i in range(K):
    diff_h = sort_h[i+(K-1)] - sort_h[i] 
    ans.append(diff_h)

print(min(ans))

Submission status: Runtime Error

Solution code:
#coding:utf-8
s=[int(i) for i in input().split()]
n=[]
for i in range(s[0]):
  n.append(int(input()))
n.sort()
print(n)
sa=abs(n[0]-n[s[1]]-1)
for i in range(s[1]):
  print(n[1],n[i+s[1]-1])
  if(sa>abs(n[i]-n[i+s[1]-1])):
    sa=abs(n[i]-n[i+s[1]-1])
print(sa)

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
h_list = [int(input()) for _ in range(n)]
h_list.sort()
i = 0
j = k-1
ans = 10**9

while j <= n:
    diff = abs(h_list[i]-h_list[j])
    ans = min(ans,diff)
    i  = j
    j += k-1
print(ans)

Submission status: Runtime Error

Solution code:
#coding:utf-8
s=[int(i) for i in input().split()]
n=[]
for i in range(s[0]):
  n.append(int(input()))
n.sort()
sa=abs(n[0]-n[s[1]]-1)
for i in range(s[1]-1):
  if(sa>abs(n[i]-n[i+s[1]-1])):
    sa=abs(n[i]-n[i+s[1]-1])
print(sa)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
 
tmp=input()
n = int(tmp.split(" ")[0])
k = int(tmp.split(" ")[1])

h=[]

for i in range(n):
    h.append(int(input()))

h.sort()

x=int(0)
y=int(h[k-1]-h[0])

for i in range(k):
    x=h[i+k-1]-h[i]
    if x < y:
        y=x


print(y)

Submission status: Runtime Error

Solution code:
N,K = map(int,input().split())
tree_list = []
for i in range(N):
    tree_list.append(int(input()))
tree_list = sorted(tree_list)
ans = 10**9
for i in range(N-K+1):
    compare = tree_list[i+2]-tree_list[i]
    if ans > compare:
        ans = compare
    if ans == 0:
      break
print(ans)

Submission status: Runtime Error

Solution code:
N,K = map(int,input().split())
tree_list = []
for i in range(N):
    tree_list.append(int(input()))
tree_list = sorted(tree_list)
ans = 10**9
for i in range(N-K+1):
    compare = tree_list[i+2]-tree_list[i]
    if ans > compare:
        ans = compare
print(ans)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
h = []
for i in range(N):
  h.append(int(input()))
h.sort()
mini = 1e9
for i in range(N-K+1):
  sub = h[i+K-1] - h[i]
  if(sub < mini){
    mini = sub
print(mini)

Submission status: Runtime Error

Solution code:
N,K = input().split()
N = int(N)
K = int(K)

trees = []
for i in range(N) :
	trees.append(int(input()))

trees.sort()
min_diff = 99999999999
a,b,c = trees[0:K]
for i in range(N-K+1) :
	max_v = c
	min_v = a
	diff = max_v - min_v
	if (min_diff > diff) :
		min_diff = diff
	if (min_diff == 0) :
		break

	a,b,c = b,c,trees[i+K]


print(min_diff)

Submission status: Runtime Error

Solution code:
n,k =map(int,input().split())
trees = sorted([int(input()) for _ in range(n)])
ans = float('inf')
for i in range(n):
  if i+2==n:
    break
  else:
    temp = abs(trees[i]-trees[i+k-1])
    if temp<ans:
      ans = temp
      if ans==0:
        break

print(ans)

Submission status: Runtime Error

Solution code:
N, K = map(int, input())
h = []
for i in range(N):
  h.append(int(input()))
h.sort()
mini = 1e9
for i in range(N-K+1):
  sub = h[i+K-1] - h[i]
  if(sub < mini){
    mini = sub
print(mini)

Submission status: Runtime Error

Solution code:
N, K = list(map(int, input().split()))

h = []
for i in range(N):
    h.append(int(input()))

h_sort = sorted(h)

h_sort_diff = []

for i in range(len(h_sort) - 1):
    tmp = h_sort[i + 1] - h_sort[i]
    h_sort_diff.append(tmp)

ans = []
for i in range(len(h_sort_diff) - 1):
    tmp = 0
    for j in range(K - 1):
        tmp += h_sort_diff[i + j]
    ans.append(tmp)

print(min(ans))

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
h = []
for i in range(n):
    h.append(int(input()))
h.sort()
a = []
for i in range(n-2):
    a.append(h[i+k-1] - h[i])
print(min(a))

Submission status: Runtime Error

Solution code:
m = input()
n = int(m.split()[0])
k = int(m.split()[1])
t = []
for i in range(n):
    t.append(int(input()))
t.sort()
a = None
for i in range(len(t)-k+1):
    w = abs(-1*t[i] + t[i+k])
    if i == 0:
        a = w
    elif a > w:
        a = w
print(a)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
hList=[]
for i in range(n):
  hList.append(int(input()))
hList.sort()
//print(hList)
ans=100000000000000
for i in range(n-k+1):
  tmp=hList[i+k-1]-hList[i]
  if(ans>tmp):
    ans=tmp
print(ans)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
hList=[]
for i in range(n):
  hList.append(int(input()))
hList.sort()
//print(hList)
ans=100000000000000
for i in range(n-k+1):
  tmp=hList[i+k-1]-hList[i]
  if(ans>tmp):
    ans=tmp
print(ans)

Submission status: Runtime Error

Solution code:
N, K = map(int,input().split())
h = [int(input()) for i in range(N)].sort()
# print(h)

MIN = 10**10
for i in range(N-K):
	if h[i+K-1] - h[i] <= MIN:
		MIN = h[i+K-1] - h[i]
#	print(i,h[i],h[i+K-1])
print(MIN)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())

h = []
for i in range(n):
    h.append(int(input()))
h.sort()
ans = 1000000000000000000000000000
for i in range(n-2):
    a = h[i+k-1] - h[i] 
    ans = min(ans, a)
print(ans)

Submission status: Runtime Error

Solution code:
#coding:utf-8
n,k = map(int,input().split())
ls = []
for i in range(n):
    ls.append(int(input()))
ls = sorted(ls)
ans = 10000000
for i in range(n-k+1):
    mx = max(ls[i:k+1])
    mn = min(ls[i:k+1])
    ans = min(ans,mx - mn)
print (ans)

Submission status: Runtime Error

Solution code:
def c_christmas_eve(N, K, H):
    H.sort()
    ans = float('inf')
    for j in range(N - K + 1):
        h_max = H[j + 2]
        h_min = H[j]
        ans = min(ans, h_max - h_min)
    return ans

N, K = [int(i) for i in input().split()]
H = [int(input()) for _ in range(N)]
print(c_christmas_eve(N, K, H))

Submission status: Runtime Error

Solution code:
N,K = map(int,input().split(" "))
height = []
for i in range(N):
    height.append(int(input()))
height.sort()
min = 10**9
for j in range(K):
    light = height[j:j+K]
    hmin = light[0]
    hmax = light[K-1]
    hei = hmax-hmin
    if hei <= min:
        min = hei
print(min)

Submission status: Runtime Error

Solution code:
N,K = input().split()
N = int(N)
K = int(K)

trees = []
for i in range(N) :
	trees.append(int(input()))

trees.sort()
min_diff = 999999
for i in range(N-K+1) :
	arr = trees[i:i+K]
	max_v = max(arr)
	min_v = min(arr)
	diff = max_v - min_v
	if (min_diff > diff) :
		min_diff = diff
	if (min_diff = 0) :
		break


print(min_diff)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
h = list(map(int, [input() for i in range(n)]))
h.sort()
res = 10**10
for i in range(k-1, n):
    res = min(res, h[i]-[i-k+1])
print(res)

Submission status: Runtime Error

Solution code:
#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll mod = 1000000009;
const ll INF = (ll)1000000007 * 1000000007;
typedef pair<int, int> P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef long double ld;
typedef complex<ld> Point;
const ld eps = 1e-11;
const ld pi = acos(-1.0);
typedef pair<ll, ll> LP;
typedef pair<ld, ld> LDP;
typedef unsigned long long ul;
int h[1 << 17];
int main(){
	int n; cin >> n; int k; cin >> k;
	rep(i, n) {
		cin >> h[i];
	}
	sort(h, h + n);
	int out = mod;
	rep(i, n - k+1) {
		out = min(out, h[i + k] - h[i]);
	}
	cout << out << endl;
	return 0;
}