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):
import heapq
 
N, k = [int(s) for s in input().split(' ')]
 
h = []
while len(h) != N:
    heapq.heappush(h, int(input()))
  
min_height = 1000000000
h = heapq.nsmallest(N, h)
for start in range(0, N - k + 1):
    end = start + k - 1
    diff = h[end] - h[start]
    if diff < min_height:
        min_height = diff
print(min_height)

Solution (Python):


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

trees=[]

for i in range(n):
    tmp=int(input())
    trees.append(tmp)

trees.sort()

min=trees[k-1]-trees[0]

for i in range(n-k+1):
    if min>(trees[i+k-1]-trees[i]):
        min=trees[i+k-1]-trees[i]

print(min)

Solution (Python):






def main():
    N, K = [int(i) for i in raw_input().strip().split(' ')]
    H = []
    for _ in range(N):
        h = int(raw_input().strip())
        H.append(h)
    H.sort()

    min_h = 10**10
    for i in xrange(N-K+1):
        h = H[i+K-1] - H[i]
        if h < min_h:
            min_h = h
    print min_h


if __name__ == '__main__':
    main()

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

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

Solution (Python):
def main():
    N, K = list(map(int, input().split()))
    hs = [int(input()) for i in range(N)]
    hs.sort()
    i = 0
    r = 10 ** 9
    while True:
        if i + K > N:
            break
        r = min(r, hs[i + K - 1] - hs[i])
        i += 1
    print(r)


if __name__ == '__main__':
    main()

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

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

h.sort()

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

print(value)

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())
A = [int(input()) for i in range(n)]
A.sort(reverse=True)
B = []

for i in range(n-k+1):
    diff = A[i] - A[i+k-1]
    B.append(diff)
    
print(min(B))

Solution (Python):


def main():
    n, k = map(int, input().split())
    hn = [int(input()) for i in range(n)]
    hn = sorted(hn)
    mi = 9999999999
    for i in range(n - k + 1):
        di = hn[i + k - 1] - hn[i]
        mi = min(di, mi)
    print(mi)

main()

Solution (Python):


n, k = [int(x) for x in input().split()]
h = [int(input()) for i in range(n)]
h.sort()
d = []

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

d.sort()
print(d[0])

Solution (Python):
n,k,*h=map(int,open(0).read().split());h.sort();print(min(y-x for x,y in zip(h,h[k-1:])))

Solution (Python):
n,k,*h=map(int,open(0).read().split());h.sort();print(min(y-x for x,y in zip(h,h[k-1:])))

Solution (Python):
n,k,*h=map(int,open(0).read().split());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());h.sort();print(min(h[i]-h[i-k+1]for i in range(k-1,n)))

Solution (Python):
import numpy as np

treenum, num = map(int,input().split())
trees = [int(input()) for _ in range(treenum)]

sa = np.sort(trees)
kekka = np.min(sa[num-1:]-sa[:-1*num+1])

print(kekka)

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

h = sorted(h)

ans = h[k-1] - h[0]
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())
trees = [int(input()) for _ in range(n)]
trees.sort()

answer = min(trees[x + k - 1] - trees[x] for x in range(0, n - k + 1))
print(answer)

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

ps = []
for n in range(N):
        ps.append(int(input()))
ps = sorted(ps)

mins = []
for start in range(N-K+1):
        mins.append( ps[start+K-1] - ps[start] ) 

print(min(mins))

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

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

g = [ h[i+K-1]-h[i] for i in range(len(h[:-K+1])) ]
print(min(g))

Solution (Python):
a, b = map(int, input().split())
A = []
for i in range(a):
    A.append(int(input()))
j = 0
A.sort()

B = []
for i in range(a-b+1):
    B.append(A[i+b-1] - A[i])
print(min(B))

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

m = float('inf')

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

print(m)

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

h = []

for i in range(N):
    tmp = int(input())
    h.append(tmp)
    
h.sort()

ans = 2*10**9

for i in range(N-K+1):
    now = abs(h[i+K-1] - h[i])
    if now<ans:
        ans = now
        
print(ans)

Solution (Python):
""" Atcoder """

import sys
input = sys.stdin.readline

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

H.sort()

min_h = 1000000000

for i in range(N - K + 1):
    if H[i+K-1] - H[i] < min_h:
        min_h = H[i+K-1] - H[i]
print(min_h)

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

h.sort()
diff = []
for i in range(n-k+1):
    diff.append(h[i+k-1] - h[i])
print(min(diff))

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

lis.sort() 

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

print(ans)

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

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

print(ans)

Solution (Python):
def find_min_diff(hs, n, k):
    l, r = 0, k-1
    diff = hs[r] - hs[l]
    for _ in range(1, n - r):
        l, r = l+1, r+1
        diff = min(diff, hs[r] - hs[l])
    return diff


def main():
    n, k = [int(v) for v in input().split()]
    hs = sorted(int(input()) for _ in range(n))
    min_diff = find_min_diff(hs, n, k)
    print(min_diff)


if __name__ == '__main__':
    main()

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

Solution (Python):
n, k = map(int, input().split())
H = [int(input()) for i in range(n)]
H.sort(reverse=True)
ans = 10 ** 9

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

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

Solution (Python):
s = list(map(int,input().split()))
h = [int(input()) for i in range(s[0])]
h.sort()
min = 0
temp = 0
for i in range(s[0]-s[1]+1):
    temp = abs(h[i]-h[i+s[1]-1])

    if i == 0:
        min = temp
    elif temp < min:
        min = temp
print(min)

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

Solution (Python):
import numpy as np
N,K = map(int,input().split())

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


res = min(list((np.array(h[K-1:]) - np.array(h[:-K+1]))))
print(res)

Solution (Python):

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

H.sort()
dif = 10**9
for i in range(N-K+1):
    if H[i+K-1] - H[i]<dif: dif = H[i+K-1] - H[i]
        
print(dif)

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

for _ in range(N):
    h_list.append(int(input()))

h_list.sort()
answer = h_list[N-1] - h_list[0]

for i in range(N-K+1):
    a = h_list[i+K-1] - h_list[i]
    answer = min(a,answer)

print(answer)

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

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

cand = [0]*(n-k+1)
cand[0] = h[k-1] - h[0]


for j in range(1,n-k+1):
    cand[j] = h[k+j-1] - h[j]

print(min(cand))

Solution (Python):

"""
Created on Wed Jan  2 09:59:01 2019

@author: Yamazaki Kenichi
"""

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(N-K+1):
   temp = h[K-1+i] - h[0+i]
   ans = min(ans,temp)
print(ans)

Solution (Python):
n, k = list(map(int, input().split()))
heights = sorted(int(input()) for _ in range(n))
print(min(h - l for l, h in zip(heights, heights[k-1:])))

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

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

H.sort(reverse=True)

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

print(ans)

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

diff = [tree[i+K-1] - tree[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.sort()
m = max(h)-min(h)
for i in range(N-K+1):
    m = min(m,h[i+K-1]-h[i])
print(m)

Solution (Python):

"""
Created on Tue Jan  1 10:30:05 2019
ABC115C
@author: maezawa
"""

n, k = list(map(int, input().split()))
h = [0]*n
for i in range(n):
    h[i] = int(input())
    
h.sort()
ans = h[k-1] - h[0]
for i in range(n-k+1) :
    dh = h[i+k-1] - h[i]
    if ans > dh:
        ans = dh
print(ans)

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

import numpy as np
hlist = np.sort(hlist)

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

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

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

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

ans = 2 * 10**9
for i in range(len(h)-K+1):
        ans = min(ans, h[i+K-1]-h[i])

print(ans)

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

Solution (Python):

n , k = list(map(int, input().split()))
p = []
for i in range(n):
        p.append(int(input()))
p.sort()
m = p[n-1] - p[0]
for i in range(n-k+1):
    t = p[i+k-1] - p[i]
    m = min(t,m)
print(m)

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

H.sort()
res = 10000000000
for i in range(0, N-K+1):
    res = min(res, H[i+K-1] - H[i])
print(res)

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

s = sorted(h)
sa = max(s)-min(s)

for i in range(n-k+1):
    a = s[i+k-1] - s[i]
    if a < sa:
        sa = a

print(sa)

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

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

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

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

H.sort(reverse=True)

ans = H[0] - H[K-1]
for n in range(0, N - K + 1):
    dif = H[n] - H[n + K -1]
    if (dif < ans):
        ans = dif

print(ans)

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

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())
hList = []
for i in range(N):
  hList.append(int(input()))
hList_sorted = sorted(hList)
kList = []
for i in range(N - K + 1):
  dis = abs(hList_sorted[i] - hList_sorted[i + K - 1])
  kList.append(dis)
print(min(kList))

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

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

h_array.sort()


result = float("inf")

for i in range(K - 1, N):
    result = min(result, h_array[i] - h_array[i - K + 1])

print(result)

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

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

h_array.sort()


result = float("inf")

for i in range(K - 1, N):
    result = min(result, h_array[i] - h_array[i - K + 1])

print(result)

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 = input().split(' ')
N = int(N)
K = int(K)

childs = [ int(input()) for _ in range(N)]
min_sub = 10**9
childs.sort()

for n in range(K-1,N):
    min_sub = min(min_sub, childs[n]-childs[n-(K-1)])

print(min_sub)

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

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

H=[]

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

H.sort()

ans = 1e10

for i in range(N-K+1):
    dif = H[i+K-1]-H[i]
    if dif<ans:
        ans=dif

print(ans)

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

Solution (Python):
from collections import Counter
n,k=map(int,input().split())
l=[]
for i in range(n):
    l.append(int(input()))
l.sort()
d=Counter(l)
for i in d:
    if(d[i]>=k):
        print(0)
        exit()
ans=10**9
for i in range(n-k+1):
    if(l[k+i-1]-l[i]<ans):
        ans=l[k+i-1]-l[i]
print(ans)

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    n,k = LI()
    a = sorted([I() for _ in range(n)])
    r = inf
    for i in range(n-k+1):
        t = a[i+k-1] - a[i]
        if r > t:
            r = t

    return r


print(main())

Solution (Python):
N,K=map(int,raw_input().split())
P=[]
m=0;

for i in range(N):
  P.append(int(raw_input()))
  
P.sort()
K-=1

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

Solution (Python):
N,K=map(int,raw_input().split())
P=[]
m=0;

for i in range(N):
  P.append(int(raw_input()))
  
P.sort()


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

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

Solution (Python):
def main():
    n, k = map(int, input().split())
    hs = sorted(int(input()) for _ in range(n))
    res = hs[k - 1] - hs[0]
    for i in range(1, n - k + 1):
        res = min(res, hs[i + k - 1] - hs[i])
    print(res)


if __name__ == '__main__':
    main()

Solution (Python):
N,K,*H = map(int,open(0).read().split())
H.sort()
a=10**9
for i in range(N-K+1):
    a=min(a, H[i+K-1]-H[i])
print(a)

Solution (Python):
N, K, *H = map(int, open(0).read().split())
H.sort()
a = float('inf')
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 = 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):
a,b= map(int,input().split(" "))
ar = []
for i in range(a):
    l = int(input())
    ar.append(l)
ar.sort()
c = []
for i in range((a-b)+1):
    c.append(ar[(i+b)-1] - ar[i])
print(min(c))

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 = map(int, input().split())
h = [int(input()) for _ in range(n)]
sh = sorted(h)
print(min([sh[i+k-1]-sh[i] for i in range(n-k+1)]))

Solution (Python):






"""問題文

とある世界では、今日はクリスマスイブです。

高羽氏の庭には N
本の木が植えられています。i 本目の木 (1≤i≤N) の高さは hi

メートルです。

彼は、これらの木のうち K

本を選んで電飾を施すことにしました。より美しい光景をつくるために、できるだけ近い高さの木を飾り付けたいです。

より具体的には、飾り付ける木のうち最も高いものの高さを hmax
メートル、最も低いものの高さを hmin メートルとすると、hmax−hmin が小さいほど好ましいです。hmax−hmin

は最小でいくつにすることができるでしょうか？
制約

    2≤K<N≤105

1≤hi≤109
hi

    は整数である。

入力

入力は以下の形式で標準入力から与えられる。

N

 K


h1


h2


:


hN


出力

hmax−hmin

としてありうる最小の値を出力せよ。
入力例 1
Copy

5 3
10
15
11
14
12

出力例 1
Copy

2
"""





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





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





p.sort()





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

Solution (Python):


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

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

h.sort()

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

print(d_min)

Solution (Python):
n,k=(int(i) for i in input().split())
h=[]
for i in range(n):
    h.append(int(input()))
h.sort()
dif = 10000000000
for i in range(n-k+1):
    if dif>h[i+k-1]-h[i]:
        dif = h[i+k-1]-h[i]

print(dif)

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

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

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

m = 10e9
for i in range(N-K+1):
    dh = h[K-1+i]-h[0+i]
    m = min(m, dh)

print(m)

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

Solution (Python):
n, k = map(int, input().split())
ki = []
for i in range(n):
    ki.append(int(input()))
ki.sort()
mx = 10**9
for i in range(n - k + 1):
    if mx > ki[i + k - 1] - ki[i]:
        mx = ki[i + k - 1] - ki[i]
print(mx)

Solution (Python):
import numpy as np
N,K=input().split()
N = int(N)
K = int(K)
e = [0]*N

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








e = list(sorted(e))

f = [0]*(N - K + 1)
for i in range(N-K+1):
    f[i] = e[K + i - 1] - e[i]

print(np.min(np.array(f)))

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

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

Solution (Python):
import numpy as np
n,k = map(int,input().split())
item = [int(input()) for i in range(n)]
item = np.array(item)
item = np.sort(item)

ans = []

for i in range(len(item)-k+1):
  temp = item[i+k-1]-item[i]
  ans.append(temp)
  
ans = np.array(ans)
print(ans.min())

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

h.sort()
l = []
for i in range(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)]

list2=list()
h.sort()

for i in range(N-K+1):
    list2.append(h[i+K-1]-h[i])
    continue
print(min(list2))

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

ans = float("inf")
for i in range(K-1, N):
    a=h[i]-h[i-K+1]
    if a<ans:
        ans=a
print(ans)

Solution (Python):
N, K = map(int, input().split())
h_list = [None] * N
for n in range(N):
    h_list[n] = int(input())

h_list = list(sorted(h_list))

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

Solution (Python):
N, K = map(int, input().split())
h_list = [None] * N
for n in range(N):
    h_list[n] = int(input())

h_list = list(sorted(h_list))

min_diff = 10 ** 10
for i in range(N):
    if K - 1 + i >= N:
        break
    diff = h_list[K - 1 + i] - h_list[i]
    if diff < min_diff:
        min_diff = diff
print(min_diff)

Solution (Python):



def main():
    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)


if __name__ == '__main__':
    main()

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(i) for i in input().split()]
a=[0]*n
for i in range(0,n,1):
    a[i]=int(input())
a.sort(reverse=True)
sa=[1000000000]*100001
j=0
while(n!=(j+k-1)):
    sa[j]=a[j]-a[j+k-1]
    j=j+1

print(min(sa))

Solution (Python):

from sys import stdin

s_in = lambda: stdin.readline()[:-1]  
d_in = lambda: int(stdin.readline())  
ds_in = lambda: list(map(int, stdin.readline().split()))  


N, K = ds_in()
height_list = []
for _ in range(N):
    height_list.append(d_in())

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

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

for _ in range(N):
    h_list.append(int(input()))

h_list.sort()

min_diff = 9999999999
for i in range(N - K + 1):
    diff = abs(h_list[i] - h_list[i + K - 1])
    min_diff = diff if diff < min_diff else min_diff

print(min_diff)

Solution (Python):
n, k = map(int, input().split())
A = []
D = []

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

A.sort(reverse=True)
    
for i in range(n - k + 1):
    diff = A[i] - A[i + k -1]
    D.append(diff)
    
print(min(D))

Solution (Python):
n, k = map(int, input().split())
num_list = []
for i in range(n):
  num_list.append(int(input()))
num_list.sort()
diff = 10**9 -1
for i in range(n-k+1):
  temp = abs(num_list[i]-num_list[i+k-1])
  if diff > temp:
    diff = temp
print(diff)

Solution (Python):
S = input()
S = S.split()
N = int(S[0])
K = int(S[1])
h = [int(input()) for i in range(N)]
h.sort()
ans = 10**9 + 1
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 = [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, raw_input().split())
hs = []
for i in range(N):
  hs.append(int(raw_input()))

hs.sort()

minv = abs(hs[0]-hs[K-1])

for i in range(len(hs)-K+1):
  tmp = hs[i+K-1] - hs[i]
  if minv > tmp:
    minv = tmp
print minv

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

h_list.sort()

min_index = 0
max_index = min_index + K - 1

hmax_hmin = 99999999999999

while len(h_list) >= max_index + 1:
  if hmax_hmin > h_list[max_index] - h_list[min_index]:
  	hmax_hmin = h_list[max_index] - h_list[min_index]
  
  min_index += 1
  max_index += 1

print(hmax_hmin)

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

min=h[N-1]
for i in range(N):
  if(i+K>N):
    break
  tmp = h[i+K-1]-h[i]
  if(min>tmp):
    min = tmp
print(min)

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

ans = 1000000000

for i in range(0, n-k+1):
    temp = h[i+k-1] - h[i]
    ans = temp if ans > temp else ans

print(ans)

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

sys.setrecursionlimit(10000)


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

H.sort()
res = 100000000000000
for i in range(N-K+1):
    res = min(res, H[i+K-1] - H[i])

print(res)

Solution (Python):
a = input().split()
a0 = int(a[0])
k = int(a[1])
b = []
for i in range(a0):
    c = int(input())
    b.append(c)
b.sort()
d = []
for i in range(len(b)-k+1):
    d.append(b[i+k-1]-b[i])

print(min(d))

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

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

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):
NK = input().split(' ')
N, K = int(NK[0]), int(NK[1])
trees = []
for i in range(N):
  trees.append(int(input()))
trees.sort()
min = float("inf")
for i in range(N-K+1):
  h = trees[i+K-1]-trees[i]
  if h < min:
    min = h
print(min)

Solution (Python):
n,k=map(int,input().split())
answer=0
tree=[]
def tree_keisan(x,y): 
    ans=tree[x+y-1]-tree[x]
    return ans
for i in range(n):
    tree.append(int(input()))
tree.sort()
answer=tree_keisan(0,k)
for i in range(n-k+1):
    sa=tree_keisan(i,k)
    if answer>sa:
        answer=sa
print(answer)

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

t = []
for i in range(0, n):
  t.append(int(input()))

t.sort()
min = max(t) - min(t)

for i in range(0, n - k + 1):
  d = t[i + k - 1] - t[i]
  if min > d:
    min = d

print(min)

Solution (Python):
firstLine = input().split(" ")
valn = int(firstLine[0])
valk = int(firstLine[1])
valh = []

for vali in range(valn):
    valh.append(int(input()))

valh.sort()

smallest = 1000000001
for vali in range(valn - valk + 1):
    diff = valh[vali + valk - 1] - valh[vali]
    if diff < smallest:
        smallest = diff

print(smallest)

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

heights.sort()


answer = max(heights) - min(heights)

for i in range(0, n-k+1):
    candidate = heights[i+k-1] - heights[i]
    if candidate < answer:
        answer = candidate

print(answer)

Solution (Python):
H, W = map(int, input().split())
S = [int(input()) for _ in range(H)]
S.sort()
res = None
for i in range(len(S) - W + 1):
    sdiff = S[i + W - 1] - S[i]
    if res is None:
        res = sdiff
    if res > sdiff:
        res = sdiff

print(res)

Solution (Python):
n,k = [int(x) for x in input().split()]
l = sorted([int(input()) for x in range(n)])
m = [(l[x+k-1]-l[x]) for x in range(n-k+1)]
print(min(m))

Solution (Python):
M = list(map(int,input().split()))
N = M[0]
K = M[1]
h = list()
for x in range(0,N):
    h.append(int(input()))
sorted_h = sorted(h)
sub_h_org = sorted_h[N-1] - sorted_h[0]
for y in range(0,N-K+1):
    sub_h = sorted_h[y+K-1] - sorted_h[y]
    if sub_h < sub_h_org:
        sub_h_org = sub_h
print(sub_h_org)

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

h.sort()
for i in range(K-1, N):
    dp[i] = h[i] - h[i-(K-1)]

print(min(dp))

Solution (Python):
N, K = (int(x) for x in 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 = [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())
t = [int(input()) for i in range(N)]
t.sort()
l = []

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

print(min(l))

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

h.sort()

DEF_MIN = max(h)-min(h)

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

print(DEF_MIN)

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

h.sort()

DEF_MIN = max(h)-min(h)

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

print(DEF_MIN)

Solution (Python):


n,k=map(int,raw_input().split())
h=[]
for i in xrange(n):
 h.append(int(raw_input()))
h.sort()
res=float('inf')
for i in xrange(n-k+1):
 res=min(res,h[i+k-1]-h[i])
print res

Solution (Python):
nnk = list(map(lambda x: int(x), input().split(" ")))
n = nnk[0]
k = nnk[1]

hs = [int(input()) for l in range(n)]
hs.sort()

num = n - k + 1

hmmm = []
for l in range(num):
    hmmm.append(hs[k+l-1] - hs[l])

hmmm.sort()
print(hmmm[0])

Solution (Python):
nnk = input().split(" ")
n = int(nnk[0])
k = int(nnk[1])

hs = [int(input()) for l in range(n)]
hs.sort()

hmmm = []
for m in range(n-k+1):
    hmmm.append(hs[k+m-1] - hs[m])

hmmm.sort()
print(hmmm[0])

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):
	ans = min(ans, h[i + K - 1] - h[i])
print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
N, K = map(int, input().split())
heights = sorted([int(input()) for _ in range(N)])
ans = float('inf')
for i in range(N - K + 1):
    if abs(heights[i] - heights[i + K - 1]) < ans:
        ans = abs(heights[i] - heights[i + K - 1])
print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
N, K = map(int, input().rstrip().split())
heights = sorted([int(input().rstrip()) for _ in range(N)])
ans = float('inf')
for i in range(N - K + 1):
    if abs(heights[i] - heights[i + K - 1]) < ans:
        ans = abs(heights[i] - heights[i + K - 1])
print(ans)

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

Solution (Python):
N, K = map(int, input().split())
heights = sorted([int(input()) for _ in range(N)])
ans = max(heights)
for i in range(N - K + 1):
    if abs(heights[i] - heights[i + K - 1]) < ans:
        ans = abs(heights[i] - heights[i + K - 1])
print(ans)

Solution (Python):
N,s = input().split()
S = int(s)
a = []
for i in range(int(N)):
    a.append(int(input()))

a.sort()

mindiff = 1000000000

for i in range(int(N)-(S - 1)):
    v = a[i+S-1]-a[i]

    if mindiff > v: mindiff = v

print(mindiff)