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 (Python):
N, K = map(int, input().split())
h = []
for i in range(N):
    h.append(int(input()))
    
h.sort()

ans = 10**9
for i in range(K-1, N):
    diff = h[i] - h[i-(K-1)]
    if diff < ans:
        ans = diff

print(ans)

Solution (Python):
n,k=map(int,input().split())
h=sorted([int(input()) for _ in '1'*n])
intervals=[]
for i in range(n-k+1):
	h_min=h[i]
	h_max=h[i+k-1]
	intervals.append(h_max-h_min)
print(min(intervals))

Solution (Python):
n, k = map(int, input().split())
h = sorted(list(int(input()) for _ in range(n)))
ans = []

for i in range(n-k+1):
    ans.append(h[i+k-1] - h[i])

print(min(ans))

Solution (Python):
n,k=map(int,input().split())
h=sorted([int(input()) for _ in range(n)],reverse=True)
h_min=1145141919
for i in range(0,n-k+1):
    h_min=min(h_min,h[i]-h[i+k-1])

print(h_min)

Solution (Python):
n, k = map(int, input().split())
h = sorted([int(input()) for _ in range(n)])

ans = min(h[i+k-1] - h[i] for i in range(n-k+1))

print(ans)

Solution (Python):
import heapq

n, k = map(int, input().split())

h = []
for _ in range(n):
    heapq.heappush(h, int(input()))

q = [heapq.heappop(h) for _ in range(k-1)]

ans = 1e9
for i in range(len(h)):
    p = i % (k-1)
    s = q[p]
    e = heapq.heappop(h)
    q[p] = e
    ans = min(ans, e - s)

print(ans)

Solution (Python):
n, k = map(int, input().split())
h = []
d = [0 for _ in range(n)]
ans = 1e9

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

h = sorted(h)

for i in range(n - k + 1):
    diff = h[i + k - 1] - h[i]
    ans = min(ans, diff)

print(ans)

Solution (Python):
import heapq

n, k = map(int, input().split())
h = []
d = [0 for _ in range(n)]
ans = float('inf')

for _ in range(n):
    heapq.heappush(h, int(input()))

h = [heapq.heappop(h) for _ in range(n)]


for i in range(n - k + 1):
    diff = h[i + k - 1] - h[i]
    ans = min(ans, diff)

print(ans)

Solution (Python):
N, K = map(int, input().split())
h = sorted([int(input()) for _ in range(N)])

lst = []
for i in range(N - K + 1):
    v = h[i + K - 1] - h[i]
    lst.append(v)

print(min(lst))

Solution (Python):
n, k = map(int, input().split())
h = []
ans = float('inf')

for _ in range(n):
    h.append(int(input()))
h = sorted(h)

for i in range(n - k + 1):
    diff = h[i + k - 1] - h[i]
    ans = min(ans, diff)

print(ans)

Solution (Python):
n, k = map(int, input().split())
h = []
d = [0 for _ in range(n)]
ans = float('inf')

for _ in range(n):
    h.append(int(input()))
h = sorted(h)

for i in range(1, n):
    d[i] = d[i - 1] + h[i] - h[i - 1]

for i in range(n - k + 1):
    diff = d[i + k - 1] - d[i]
    ans = min(ans, diff)

print(ans)

Solution (Python):
N, K = [int(hk) for hk in input().split()]
H = []
for n in range(N):
  H.append(int(input()))
H.sort()
dh_mn = 10**9
for i in range(N-K+1):
  dh = H[K+i-1]-H[i]
  if dh < dh_mn:
    dh_mn = dh
print(dh_mn)

Solution (Python):

import sys


def solve(N: int, K: int, H: "List[int]"):
    H.sort()

    min_hdif = float("inf")
    for i in range(N-K+1):
        min_hdif = min(min_hdif,abs(H[i]-H[i+K-1]))

    print(min_hdif)



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    K = int(next(tokens))  
    h = [ int(next(tokens)) for _ in range(N) ]  
    solve(N, K, h)

if __name__ == '__main__':
    main()

Solution (Python):
N, K = map(int, input().split())
h = sorted([int(input()) for _ in range(N)])

ans = 10 ** 9 - 1

for i in range(N-K+1):
    tmp = h[i+K-1] - h[i]
    ans = min(ans, tmp)

print(ans)

Solution (Python):
N, K = map(int, input().split())
h = sorted([int(input()) for _ in range(N)])
ans = 10 ** 9
for i in range(N - K + 1):
	ans = min(ans, h[i + K - 1] - h[i])
print(ans)

Solution (Python):
N, K = map(int, input().split())
H = sorted([int(input()) for _ in range(N)])

ans = float('inf')
for i in range(N - K + 1):
    ans = min(ans, H[i + K - 1] - H[i])
print(ans)

Solution (Python):
N, K = map(int, input().split())
H = sorted([int(input()) for _ in range(N)])

ans = float('inf')
for i in range(N - K + 1):
    ans = min(ans, H[i + K - 1] - H[i])
print(ans)

Solution (Python):
n,k = map(int,input().split())
height = [int(input()) for i in range(n)]
height.sort()
ans =[]
for i in range(n-k+1):
  ans.append(height[i+k-1]-height[i])
print(min(ans))

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

h.sort()
ans = 9999999999999999999
for i in range(0,N+1-K):
    ans = min(ans, h[i+K-1] - h[i])


print(ans)

Solution (Python):
N, K = map(int, input().split())
h = [int(input()) for _ in range(N)]
h.sort(reverse=True)
cnt = 10**9
for i in range(N-K+1):
  rng = h[i]-h[i+K-1]
  if rng < cnt:
    cnt = rng
print(cnt)

Solution (Python):
n,k = map(int,input().split())
h = sorted([int(input()) for i in range(n)])
d = [h[i+k-1]-h[i] for i in range(n-k+1)]
print(min(d))

Solution (Python):
n,k = map(int, input().split())
H = [int(input()) for _ in range(n)]
H.sort()

dif = 10**10
for i in range(n-k+1):
    dif = H[i+k-1]-H[i] if H[i+k-1]-H[i] < dif else dif
print(dif)

Solution (Python):


n,k = map(int, input().split())
h = sorted([int(input()) for i in range(n)])

d = [h[i+k-1]-h[i] for i in range(n-k+1)]
x = min(d)

print(x)

Solution (Python):
N, K = map(int, input().split())
h = []
min_d = 10 ** 10

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

for i in range(N-K+1) : 
    d = h[i+K-1] -h[i]
    min_d = min(d, min_d)
    
print(min_d)

Solution (Python):
a=list(map(int,input().split()))
b=list(sorted([int(input()) for i in range(a[0])]))
ans=[]
for i in range(a[0]-a[1]+1):
  ans+=[abs(b[a[1]-1+i]-b[i])]
print(min(ans))

Solution (Python):
n, k = map(int, input().split(' '))
trees = sorted([ int(input()) for _ in range(n) ])

ans = None
for i in range(n-k+1):
  if ans is None:
    ans = trees[i+k-1] - trees[i]
  else:
    ans = min(ans, trees[i+k-1] - trees[i])
    
print(ans)

Solution (Python):
N,K = map(int,input().split())
h_list = []
for i in range(N):
    h_list.append(int(input()))
h_list.sort()
ans = 10**10
for i in range(N-K+1):
    ans = min(ans,h_list[i+K-1]-h_list[i])
print(ans)

Solution (Python):
from __future__ import print_function
import numpy as np
import sys
import math,string,itertools,fractions,heapq,collections,re,array,bisect,copy,functools


import random

def eprint(*args, **kwargs):
    print(*args, file=sys.stderr, **kwargs)

n,k = map(int, input().split())
h = np.zeros(n, dtype = int)
for i in range(n):
    h[i] = int(input())

h.sort()

ll = np.zeros(n-k+1,dtype=int)
for i in range(n-(k-1)):
    ll[i] = abs(h[i] - h[i+(k-1)])
    

print(ll.min())

Solution (Python):
N, K = map(int, input().split())
H = [int(input()) for _ in range(N)]

h = sorted(H)


d = []
for i in range(K - 1, N):
	d += [h[i] - h[i - K + 1]]


print(min(d))

Solution (Python):

n, k = [int(i) for i in input().split()]
h = []
for ni in range(n):
  h.append(int(input()))
h.sort()
ans = max(h)
for ni in range(n-k+1):
  tmp = h[ni+k-1] - h[ni]
  if tmp < ans:
    ans = tmp
print(ans)

Solution (Python):
n,k=map(int,input().split())
a=[]
for i in range(n):
    a.append(int(input()))
a.sort()
i=0
ans=10**10
while k+i-1<n:
    ans=min(ans,a[k+i-1]-a[i])
    i+=1
print(ans)

Solution (Python):

"""
Created on Tue May 14 20:13:03 2019

@author: avina
"""

n,k = map(int, input().split())
l = []
for i in range(n):
    l.append(int(input()))
l.sort()
i = 0
mins = 1e10
while i < n - k +1:
  mins = min(mins, l[i+k-1] - l[i])
  i+=1
print(mins)

Solution (Python):
N, K = map(int, input().split())
h = [int(input()) for i in range(N)]
h.sort()
print(min(h[i+K-1] - h[i] for i in range(N-K+1)))

Solution (Python):
N, K = [int(x) for x in input().split()]
H = [int(input()) for _ in range(N)]

def main():
    h = H
    h.sort()
    length = len(h)
    ans = float('inf')
    for i in range(length):
        end_index = i + K - 1
        if end_index > length - 1:
            break
        ans = min(ans, h[end_index] - h[i])

    print(ans)

if __name__ == '__main__':
    main()

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

h.sort()

ans = 10 ** 9
for i in range(N-(K-1)):
    tmp = h[i+(K-1)] - h[i]
    if tmp < ans:
        ans = tmp

print(ans)

Solution (Python):
N, K = map(int,input().split())
H = []
H_dif = []

for i in range(N):
    H.append(int(input()))

H.sort()

for j in range(N-K+1):
    H_dif.append(H[j+K-1] - H[j])

print(min(H_dif))

Solution (Python):


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

sorted_tree = sorted(h)
sorted_tree2 = sorted_tree[K-1:]

min_diff = 1e9
for (min_h, max_h) in zip(sorted_tree, sorted_tree2):
    if (max_h - min_h) < min_diff:
        min_diff = max_h - min_h

print(min_diff)

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

Solution (Python):
N, K = [int(i) for i in input().split()]
hs = [int(input()) for _ in range(N)]
hs.sort()
diffs = []
for lh in range(N):
    rh = lh + K - 1
    if rh >= N:
        break
    diffs.append(hs[rh] - hs[lh])
print(min(diffs))

Solution (Python):
n,k = map(int,input().split())
f = [int(input()) for i in range(n)]
li = []
f.sort()
for i in range(n-k+1) :
    li.append(f[i+k-1]-f[i])
print(min(li))

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

ans = 10**10

for i in range(n-k+1):
  if h[i+k-1] - h[i] < ans:
    ans = h[i+k-1] - h[i]
print(ans)

Solution (Python):


n,k=map(int,input().split())
l=[]
for i in range(n):
    t=int(input())
    l.append(t)
l.sort()

ans=100000000000
for i in range(len(l)-k+1):
    if(ans>l[i+k-1]-l[i]):
        ans=l[i+k-1]-l[i]
print(ans)

Solution (Python):
N,K=map(int,input().split())
h=[int(input()) for i in range(N)]
h.sort()
print(min(h[i+K-1]-h[i] for i in range(N-K+1)))

Solution (Python):
import sys
input = sys.stdin.readline

n, k = map(int, input().split())





h = [int(input()) for _ in range(n)]

h.sort()

ans = 10**9
for i in range(n-k+1):
    temp = h[i+k-1] - h[i]
    ans = min(ans, temp)

print(ans)

Solution (Python):



n, k = map(int, input().split())
h = [0]*n

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

h.sort()

ans = 10**9
for i in range(n-k+1):
    temp = h[i+k-1] - h[i]
    if ans > temp:
        ans = temp
print(ans)

Solution (Python):
import sys
input = sys.stdin.readline

n, k = map(int, input().split())
h = [0]*n

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

h.sort()

ans = 10**9
for i in range(n-k+1):
    temp = h[i+k-1] - h[i]
    if ans > temp:
        ans = temp
print(ans)

Solution (Python):
N, K = map(int, input().split())
h = [int(input()) for i in range(N)]
h.sort()
INF = 10 ** 9
ans = INF

for i in range(N-(K-1)):
  tmp = h[i+(K-1)] - h[i]
  ans = min(tmp, ans)
print(ans)

Solution (Python):
N, K = map(int, input().split())
H = list(sorted([int(input()) for _ in range(N)]))
ans = H[-1]
for s, e in zip(H, H[K - 1:]):
  ans = min(e - s, ans)

print(ans)

Solution (Python):
n,k=map(int, input().split())
h_list=[]
min_df=0

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

h_list.sort()

for j in range(n-k+1):
  if j == 0:
    min_df = h_list[k-1] - h_list[0]
  else:
    df = h_list[j+k-1] - h_list[j]
    if df < min_df:
      min_df = df

print(min_df)

Solution (Python):
N, K = map(int, input().split())
H = [int(input()) for _ in range(N)]

H.sort(reverse=True)

min_score = pow(10, 9)
for i in range(N - K + 1):
    min_score = min(min_score, H[i] - H[i + K - 1])

print(min_score)

Solution (Python):
N, K = map(int, input().split())
H = [int(input()) for _ in range(N)]
H.sort()

min_diff = 1000000000
for i in range(N-K+1):
    min_diff = min(min_diff, H[i+K-1] - H[i])

print(min_diff)

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

H = [int(input()) for n in range(N)]

H.sort(reverse=False)

min_h = 1000000000
for i in range(N - K + 1):
    min_h = min(min_h, H[i + K - 1] - H[i])

print(min_h)

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

Solution (Python):

N,K = map(int, input().split())
h_list = [int(input()) for _ in range(N)]
h_list.sort()
diff_list = []
for i in range(N-K+1):
    diff_list.append(h_list[i+K-1] - h_list[i])

print(min(diff_list))

Solution (Python):
N, K = map(int, input().split())
h = []
for i in range(N):
    h.append(int(input()))
h = sorted(h)

min_diff = 1000000000
for i in range(N - K + 1):
    diff = h[i + K - 1] - h[i]
    if min_diff > diff:
        min_diff = diff
print(min_diff)

Solution (Python):
N, K = map(int, input().split())
h = []
for i in range(N):
    h.append(int(input()))
h = sorted(h)

min_diff = 1000000000
for i in range(N - K + 1):
    diff = h[i + K - 1] - h[i]
    if min_diff > diff:
        min_diff = diff
print(min_diff)

Solution (Python):
N, K = map(int,input().split())
H = [int(input()) for i in range(N)]
H = sorted(H)
ans = 1000000000
for i in range(N-K+1):
    res = H[i+K-1] - H[i]
    ans = min(res,ans)
print(ans)

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

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

for i in range(n-k+1):
    ans.append(h[k+i-1]-h[i])

print(min(ans))

Solution (Python):
N,K = map(int,input().split())
h = [int(input()) for _ in range(N)]

s_h = sorted(h,reverse=True)

i = 0
minD = 1000000000
while i+K <= N:
    diff = s_h[i] - s_h[i+K-1]
    minD = min(diff,minD)

    i = i + 1

print(minD)

Solution (Python):
n,k=map(int,input().split())
p=[int(input()) for i in range(n)]
p.sort()
q=9999999999999
for i in range (n-k+1):
    if p[k+i-1]-p[i]<q:
        q=p[k+i-1]-p[i]
print(q)

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

h.sort(reverse=True)
diff = [h[i] - h[i+K-1] for i in range(N-K+1)]

print(min(diff))

Solution (Python):
N,K = [int(it) for it in input().split()]
li = [int(input()) for i in range(N)]
li.sort()

s = 1000000000
for i in range( N-K+1 ):
  s = min(s,li[i+K-1]-li[i])
print (s)

Solution (Python):
N,K = map(int,input().split())
H = [int(input()) for _ in range(N) ]
H.sort()

tmp = [H[i+K-1]-H[i] for i in range(N-K+1)]

print(min(tmp))

Solution (Python):
N, K = [int(a) for a in input().split(" ")]
hs = sorted([int(input()) for i in range(N)])
ans = 10**10
for left, right in zip(range(0,N-K+2), range(K-1,N)):
    ans = min(ans, hs[right]-hs[left])
print(ans)

Solution (Python):
N,K = list(map(int,input().split()))
H = [int(input()) for i in range(N)]
H.sort()
result = 10000000000
for i,h in enumerate(H):
    if K + i - 1 > N -1:
        break
    if result > H[K + i - 1] - H[i]:
        result = H[K + i - 1] - H[i]
print(result)

Solution (Python):
N, K = map(int, input().split())
h = [int(input()) for i in range(N)]
h.sort()
diff = [0]*(N-K+1)
for i in range(N-K+1):
  diff[i] = h[i+K-1] - h[i]

print(min(diff))

Solution (Python):
N, M = list(map(int, input().split()))
K = [int(input()) for i in range(N)]
K = sorted(K)
print(min([K[i+M-1]-K[i] for i in range(N-M+1)]))

Solution (Python):
N, M = list(map(int, input().split()))
K = [int(input()) for i in range(N)]
K = sorted(K)

result = 10 ** 10
for i in range(N-M+1):
  result = min([result, K[i+M-1]-K[i]])
  
print(result)

Solution (Python):
n,k = [int(i) for i in input().split()]
p = [int(input()) for _ in range(n)]
p.sort()

ans = float('inf')

for i in range(n-(k-1)):
  ans = min(ans,p[i+(k-1)]-p[i])
  
print(ans)

Solution (Python):
n,k=map(int,input().split())
arr=[int(input()) for _ in range(n)]
arr=sorted(arr)
ans=10**10
for i in range(n-k+1):
  ans=min(ans,arr[i+k-1]-arr[i])
print(ans)

Solution (Python):
N, K = map(int, input().split())
H = sorted([int(input()) for _ in range(N)])
ret = 10 ** 9
for i in range(N-K+1):
    ret = min(H[i+K-1] - H[i], ret)
print(ret)

Solution (Python):
import sys
input = sys.stdin.readline

n, k = map(int, input().split())
l = [int(input()) for _ in range(n)]
l.sort()

m = 10 ** 9 + 1
for i in range(len(l) - k + 1):
    m = min(m, l[i + k - 1] - l[i])

print(m)

Solution (Python):
N, K = map(int, input().split())
h = sorted([int(input()) for _ in range(N)])
print(min(h[i+K-1] - h[i] for i in range(N-K+1)))

Solution (Python):
N,K= list(map(int,input().split(" ")))
nums = []
for i in range(N):
    nums.append(int(input()))
nums.sort()
r = 0
l = 0
ans = []
for i,num in enumerate(nums[:-(K-1)]):
    r = nums[i+K-1]
    l = num
    
    ans.append(r-l)
print(min(ans))

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

trees = []
for _ in range(n):
  trees.append(int(input()))
  
  
trees.sort()
ans = 10**9+1
for i in range(n-k+1):
  if ans > trees[i+k-1] - trees[i]:
    ans = trees[i+k-1] - trees[i]
    
print(ans)

Solution (Python):
import numpy as np
from copy import deepcopy

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

H = sorted(H)

solve = []
for i in range(N-K+1):
  solve.append(H[i+K-1] - H[i])
  
ans = min(solve)
print(ans)

Solution (Python):
N, K = map(int, input().split())
h = [int(input()) for _ in range(N)]

h.sort()
ans = float('inf')
for i in range(N - K + 1):
    ans = min(ans, h[i + K - 1] - h[i])
print(ans)

Solution (Python):
N,K = map(int, input().split())
H = [0]*N
for i in range(N):
  H[i] = int(input())

H.sort()
ans = float('inf')
for i in range(N-K+1):
  diff = abs(H[i] - H[i+K-1])
  if diff < ans:
    ans = diff
print(ans)

Solution (Python):
N, K = map(int,input().split())
h = []
for i in range(N):
    x = int(input())
    h.append(x)

h.sort()
m = []
for i in range(N-K+1):
    m.append(h[i+K-1] - h[i])

print(min(m))

Solution (Python):
N, K = map(int, input().split())
h = [int(input()) for i in range(N)]
h.sort()
print(min(h[i+K-1]-h[i] for i in range(N-K+1)))

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

Solution (Python):
n,k=map(int,input().split())
h_list=[]
for i in range(n):
    h_list.append(int(input()))
h_list.sort()
i=0
Min=10**9
while i+k-1<=n-1:
    d=h_list[i+k-1]-h_list[i]
    if d<Min:
        Min=d
    i=i+1
print(Min)

Solution (Python):
n,k=map(int,input().split())
H=[int(input()) for i in range(n)]
H.sort()
ans=H[k-1]-H[0]
for i in range(k,n):
  ans=min(ans,H[i]-H[i-k+1])
print(ans)

Solution (Python):

N,k = map(int,input().rstrip().split())
h = sorted([ int(input()) for _ in range(N) ])
list = []
for i in range(N-k+1):
    list.append(h[i+k-1] - h[i])
print(min(list))

Solution (Python):

import sys
from math import *
from itertools import *
from collections import *
from functools import *
try:
    from math import gcd
except Exception:
    from fractions import gcd


def solve(N: int, K: int, h: "List[int]"):
    h.sort()
    ret = float('inf')
    for l, r in zip(h, h[K-1:]):
        ret = min(ret, r - l)
    return ret


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    K = int(next(tokens))  
    h = [ int(next(tokens)) for _ in range(N) ]  
    result = solve(N, K, h)
    print(result)

if __name__ == '__main__':
    main()

Solution (Python):
import heapq
import queue

N, K = map(int, input().split())

trees = []
heapq.heapify(trees)

for _ in range(N):
    heapq.heappush(trees, int(input()))

ans = 1 + 10**9

q = queue.Queue(maxsize=K-1)

for _ in range(K-1):
    q.put(heapq.heappop(trees))

for _ in range(N-K+1):
    curmin = q.get()
    nextmin = heapq.heappop(trees)

    ans = min(ans, nextmin-curmin)

    q.put(nextmin)

print(ans)

Solution (Python):
N, K = [int(item) for item in input().split()]
arr = [int(input()) for i in range(N)]
arr.sort()
m = 10 ** 9 + 1
for i in range(N - K + 1):
  m = min(m, (arr[i+K-1] - arr[i]))
print(m)

Solution (Python):
n, k = map(int, input().split())
h = []
for _ in range(n):
    h.append(int(input()))
h.sort()
k = k - 1
ans = 1e10
for i in range(n-k):
    ans = min(ans, h[i+k] - h[i])
print(ans)

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

h.sort()

ans = float('inf')

for i in range(n-k+1):
    ans = min(ans, h[i+k-1]-h[i])

print(ans)

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

h.sort()

ans = float('inf')

for i in range(n-k+1):
    ans = min(ans, h[i+k-1]-h[i])

print(ans)

Solution (Python):
N,K = map(int,input().split())
H = [int(input()) for i in range(N)]
H.sort()

woods = []
for i in range(N-K+1):
    woods.append(-H[i]+H[i+K-1])
print(min(woods))

Solution (Python):
import sys

r=input().split()
n=int(r[0])
k=int(r[1])
h=sorted([int(x.strip()) for x in sys.stdin.readlines()])

d=[h[x+k-1]-h[x] for x in range(n-k+1)]
ans=min(d)
            
print(ans)

Solution (Python):
n, k = [int(c) for c in input().split()]
h = [int(input()) for _ in range(n)]

h.sort()

min_d = 1000000000
for i in range(n-k+1):
    d = h[i+k-1] - h[i]
    if d < min_d:
        min_d = d

print(min_d)

Solution (Python):


N,K= map(int,input().split())
trees =[]

for _ in range(N):
    trees.append(int(input()))
trees.sort()
ans=10**10
for i in range(N-K+1):
    min_=trees[i]
    max_=trees[i+K-1]
    ans=min(ans,max_-min_)
print(ans)

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

h.sort()
ans = 10 ** 9

for i in range(n-k+1):
    ans = min(ans, h[i+k-1] - h[i])

print(ans)

Solution (Python):
N, K = map(int, input().split())
H = [int(input()) for _ in range(N)]
H.sort()
ans = H[-1]
for i in range(N-K+1):
    ans = min(ans, H[i+K-1]-H[i])
print(ans)

Solution (Python):
import sys

input = sys.stdin.readline
N, K = list(map(int, input().split()))

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

h.sort()
diff = [h[i+K-1]-h[i] for i in range(N-K+1)]

print(min(diff))

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

h = sorted(h)

ans = 10 ** 9
for j in range(n-k+1):
    if h[j+k-1] - h[j] < ans:
        ans = h[j+k-1] - h[j]

print(ans)

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

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

h.sort()









answer = 10**9
for i in range(N):
    if i + K -1 >= N:
        break
    
    
    

    if answer > h[i+K-1]-h[i]:
        answer = h[i+K-1]-h[i]





print(answer)

Solution (Python):
N, K = map(int, input().split())
H = [None] * N

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

H = sorted(H)
ans = 10e9

for i in range(N-K+1):
    tmp = H[i+K-1] - H[i]
    ans = min(ans, tmp)

print(ans)

Solution (Python):
N, K = map(int, input().split())
p = [int(input()) for i in range(N)]

p.sort()

ans = []
for i in range(K - 1, len(p)):
    ans.append(p[i] - p[i - K + 1])

print(min(ans))

Solution (Python):
import sys
def main():
  if 'get_ipython' not in globals():  
    
    N,K = map(int, input().split())
    h = []
    for _ in range(N):
      h.append(int(input()))
  else:
    N = 5
    K = 3
    h = [10,15,11,14,12]
    
    
    

  h.sort()  

  ans = (h[K-1] - h[0])
  
  max_i = len(h)-K+1
  for i in range(1,max_i):
    tmp = (h[i+K-1] - h[i])
    if ans > tmp:
      ans = tmp
    if ans == 0:
      print(ans)
      sys.exit()
  
  print(ans)

main()

Solution (Python):
n,k = map(int,input().split())
list = []
for i in range(n):
  list.append(int(input()))
list.sort()
min = list[n-1]
for j in range(n-k+1):
  if list[j+k-1]-list[j] < min:
    min = list[j+k-1]-list[j]
print(min)

Solution (Python):
n,k = map(int,input().split())
list = []
for i in range(n):
  list.append(int(input()))
list.sort()
min = list[n-1]
for j in range(n-k+1):
  if list[j+k-1]-list[j] < min:
    min = list[j+k-1]-list[j]
print(min)

Solution (Python):
N,K = map(int,input().split(" "))
h = sorted([int(input()) for x in range(N)])

l = 0
r = K - 1
diff = []
while r < N:
    diff.append(h[r]-h[l])
    l = l + 1
    r = r + 1

print(min(diff))

Solution (Python):
N, K = map(int, input().split())
H = [int(input()) for _ in range(N)]
H = sorted(H)
ans = 10**10
for i in range(N - K + 1):
    ans = min(ans, H[i+K-1] - H[i])
print(ans)

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

H = [int(input()) for _ in range(N)]
H.sort()

print(min((H[i+K-1] - H[i]) for i in range(N - K + 1)))

Solution (Python):
N,K=map(int,input().split())
H=[int(input()) for _ in range(N)]
H.sort()
tmp=H[K-1]-H[0]
for i in range(0,N-K+1):
    if tmp>H[K+i-1]-H[i]:
        tmp=H[K+i-1]-H[i]
print(tmp)

Solution (Python):
n, k = map(int, input().split())
h = [int(input()) for _ in range(n)]
h.sort()
cand = [b - a for a, b in zip(h, h[k - 1:])]
print(min(cand))

Solution (Python):



N,K = map(int,input().split())

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

vec.sort()

minV = 100000

l = []
for i in range(0,N-K+1):
    l.append(vec[i+K-1] - vec[i])
    

print(min(l))

Solution (Python):




n, k = map(int, input().split())
h = list()

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

h.sort()

minh = h[n-1]
for i in range(n - k + 1):
    rng = h[i+k-1] - h[i]
    if rng < minh:
        minh = rng

print(minh)

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

height=[]
for i in range(n-k+1):
    height.append(H[i+k-1]-H[i])
print(min(height))

Solution (Python):
n, k = map(int,input().split())
h = [0] * n

for i in range(0,n):
    h[i] = int(input())

h.sort()

l = []

for i in range(0,n-k+1):
    l.append(h[i+k-1]-h[i])

print(min(l))

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

Solution (Python):
N,K = map(int,input().split())
tree_list,new_tree_list = [],[]
for i in range(N):
    tree_list.append(int(input()))

tree_list.sort(reverse=True)

for i in range(len(tree_list)-K+1):
    new_tree_list.append(tree_list[0+i]-tree_list[K+i-1])
new_tree_list.sort()
print(new_tree_list[0])

Solution (Python):
import numpy as np

N,K = list(map(int, input().split()))
h = []
for _ in range(N):
    h.append(int(input()))
h = sorted(h)
res = h[N-1] - h[0]
for i in np.arange(0,N-K+1,1):
    tmp = h[i+K-1] - h[i]
    if res > tmp:
        res = tmp
    
print(res)

Solution (Python):
N,K = map(int,input().split(' '))

H = [int(input()) for i in range(N)]
H.sort()
  
print(min(H[i+K-1] - H[i] for i in range(N-K+1)))

Solution (Python):
n,k,*h=map(int,open(0).read().split())
x=sorted(h)
print(min(x[i+k-1]-x[i]for i in range(n-k+1)))

Solution (Python):
N,K=map(int,input().split())
L=[]
for i in range(N) :
  L.append(int(input()))
L.sort()
M=[]
for x,y in zip(L,L[K-1:]) :
  M.append(y-x)
print(min(M))

Solution (Python):
N,K=map(int,input().split())
L=[]
for i in range(N) :
  L.append(int(input()))
L.sort()
S=10**9
for i in range(N-K+1) :
  if (L[i+K-1]-L[i]) <= S :
    S=(L[i+K-1]-L[i])
print(S)

Solution (Python):
N,K = map(int, input().split())
hs = []
for _ in range(N):
    hs.append(int(input()))

hs_sorted = sorted(hs)

min_diff = float("inf")

for i in range(N-K+1):
    diff = hs_sorted[i+K-1] - hs_sorted[i]
    
    if diff < min_diff:
        min_diff = diff

print(min_diff)

Solution (Python):


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

h_list.sort()
best = 1000000000
for i in range(N - K + 1):
    
    diff = h_list[i+K-1] - h_list[i]
    if diff < best:
        best = diff

print (best)

Solution (Python):


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

h_list.sort()
'''
best = 1000000000
for i in range(N - K + 1):
    diff = h_list[i:i+K][-1] - h_list[i:i+K][0]
    if diff < best:
        best = diff
'''
print (min(h_list[i+K-1] - h_list[i] for i in range(N-K+1)))

Solution (Python):
N, K = map(int, input().split())
h = [int(input()) for _ in range(N)]
h.sort()
m = float('inf')
for i in range(N-K+1):
  if h[i+K-1] - h[i] < m:
    m = h[i+K-1] - h[i]
print(m)

Solution (Python):
import math
n,k = map(int,input().split())
hList = [int(input()) for a in range(n)]
hList.sort()
mn = 10**10
for a in range(n - k + 1):
    mn=min([mn,hList[a+k-1]-hList[a]])
print(mn)

Solution (Python):
import math
n,k = map(int,input().split())
hList = [int(input()) for a in range(n)]
hList.sort()
mn = 10**10
for a in range(n - k + 1):
    mn=min([mn,hList[a+k-1]-hList[a]])
print(mn)

Solution (Python):
N, K = map(int,input().split())
H = []
for i in range(N):
    h = int(input())
    H.append(h)

H.sort()
l = 0
r = K-1
ans = 10**10
while r <= N-1:
    sub = H[r] - H[l]
    ans = min(ans, sub)
    r += 1
    l += 1

print(ans)

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


inf = 10**9
min_height = inf
for i in range(N-K+1):
    
    min_height = min(min_height, h[i+(K-1)] - h[i])

print(min_height)

Solution (Python):
N, K  = map(int, input().rstrip().split())
h = [int(input()) for _ in range(N)]
h.sort()
minimum = 10**9 
for i in range(N-K+1):
    res = h[i+K-1] - h[i]
    if res < minimum:
        minimum = res
print(minimum)

Solution (Python):
a, b = [int(i) for i in input().split()]
c = []
for i in range(a):
    c.append(int(input()))

c.sort()

temp = 999999999999999999999999999999999999999999999999999999999999999999999999999999999
for a in range(len(c) - b + 1):
    s = c[a + b - 1]
    t = c[a]
    if s - t < temp:
        temp = s - t

print(temp)

Solution (Python):
n,k=map(int,input().split())
a=[]
for i in range(n):a.append(int(input()))
a.sort()
ans=float("inf")
for i in range(n-k+1):ans=min(ans,a[i+k-1]-a[i])
print(ans)

Solution (Python):
N, K = map(int, input().split())
h = [int(input()) for i in range(N)]
h.sort()
mn = 1e10
for i in range(N-K+1):
  mn = min(mn, h[i+K-1] - h[i])
print(mn)

Solution (Python):
import sys
input = sys.stdin.readline

n, k = map(int, input().split())
h = [int(input()) for _ in range(n)]
h = sorted(h)
ans = []
for i in range(n-k+1):
    ans.append(h[i+k-1] - h[i])
print(min(ans))