CodeEff/ECCO huggingface dataset:
# Original solution
import sys
sys.setrecursionlimit(10000000)
n, k = list(map(int, input().split()))
h = [int(eval(input())) for _ in range(n)]
def score(a):
    return max(a) - min(a)

def dfs(i, a):
    if len(a) == k:
        return score(a)

    s = float('inf')
    for x, y in enumerate(h[i:]):
        a.append(y)
        s = min(s, dfs(i + x + 1, a))
        a.pop()
    return s
print((dfs(0, [])))

# Optimized solution
import itertools
n, k = list(map(int, input().split()))
h = [int(eval(input())) for _ in range(n)]
score = float('inf')
for x in itertools.combinations(h, k):
    score = min(score, max(x) - min(x))
print(score)
# Original solution
n,k=list(map(int,input().split()))
h=sorted([int(eval(input())) for _ in range(n)])
ans=10**9
for i in range(n-k+1):
    cnt=max(h[i:i+k])-min(h[i:i+k])
    ans=min(cnt,ans)
print(ans)

# Optimized solution
n,k=list(map(int,input().split()))
h=sorted([int(eval(input())) for _ in range(n)])
ans=10**9
for i in range(n-k+1):
    ans=min(h[k+i-1]-h[i],ans)
print(ans)
# Original solution
n,k=list(map(int,input().split()))
h=[int(eval(input())) for _ in range(n)]
h.sort()
l=[]
for i in range(n-k+1):
    l.append(max(h[i:i+k])-min(h[i:i+k]))
print((min(l)))

# Optimized solution
n,k=list(map(int,input().split()))
h=[int(eval(input())) for _ in range(n)]
h.sort()
l=[]
for i in range(n-k+1):
    l.append(h[i+k-1] - h[i])
print((min(l)))
# Original solution
n,k = list(map(int,input().split()))
h = [int(eval(input())) for _ in range(n)]
h.sort()
m = 10**10
for i in range(n-k+1):
    a = max(h[i:i+k])
    b = min(h[i:i+k])
    if a - b < m:
        m = a - b

print(m)

# Optimized solution
n,k = list(map(int,input().split()))
h = [int(eval(input())) for _ in range(n)]
h.sort()
m = 10**10
for i in range(n-k+1):
    b = h[i]
    a = h[i+k-1]
    if a - b < m:
        m = a - b

print(m)
# Original solution
import sys
def main():
  if 'get_ipython' not in globals():  
    # For Subsession
    N,K = list(map(int, input().split()))
    h = []
    for _ in range(N):
      h.append(int(eval(input())))
  else:
    N = 5
    K = 3
    h = [10,15,11,14,12]
    # N = 5
    # K = 3
    # h = [5,7,5,7,7]

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

main()

# Optimized solution
import sys
def main():
  if 'get_ipython' not in globals():  
    # For Subsession
    N,K = list(map(int, input().split()))
    h = []
    for _ in range(N):
      h.append(int(eval(input())))
  else:
    N = 5
    K = 3
    h = [10,15,11,14,12]
    # N = 5
    # K = 3
    # h = [5,7,5,7,7]

  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()
# Original solution
n,k = list(map(int,input().split()))
h = [int(eval(input())) for _ in range(n)]
h = sorted(h)
ans = float('inf')
for i in range(n-k+1):
    hs = h[i:i+k]
    ans = min(ans,(hs[-1] - hs[0]))
print(ans)

# Optimized solution
n,k = list(map(int,input().split()))
h = [int(eval(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)
# Original solution
N,K = list(map(int,input().split()))
H = [int(eval(input())) for _ in range(N)]

H.sort()

#res1 = max(H[:K])-min(H[:K])
#res2 = max(H[N-K:])-min(H[N-K:])

res = 1000000000
for i in range(N-K+1):
    tmp = max(H[i:i+K])-min(H[i:i+K])
    if tmp < res:
        res = tmp
print(res)

# Optimized solution
N,K = list(map(int,input().split()))
H = [int(eval(input())) for _ in range(N)]

H.sort()

res = 1000000000
for i in range(N-K+1):
    #print(H)
    #print(H[i+K-1],H[i])
    tmp = H[i+K-1]-H[i]
    if tmp < res:
        res = tmp
print(res)
# Original solution
import heapq

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

for _ in range(n):
    # h.append(int(input()))
    heapq.heappush(h, int(eval(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)

# Optimized solution
import heapq

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

for _ in range(n):
    # h.append(int(input()))
    heapq.heappush(h, int(eval(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)
# Original solution
N, K = list(map(int, input().split()))
h = [0]*N
for i in range(N):
  h[i] = int(eval(input()))
h.sort(reverse=True)
d = []
for i in range(N-K+1):
  d.append(h[i] - h[i+K-1])
print((min(d)))

# Optimized solution
N, K = list(map(int, input().split()))
h = [int(eval(input())) for i in range(N)]
h.sort(reverse=True)
d = [h[i] - h[i+K-1] for i in range(N-K+1)]
print((min(d)))
# Original solution
import sys

def main():
    N, K = list(map(int,input().split()))
    h = [0] * N
    for i in range(N):
        h[i] = int(eval(input()))

    h = sorted(h)

    abs_p = []
    for j in range(0,N-1):
        abs_p.append(abs(h[j]-h[j+1]))

    abon = []
    for k in range(0,N-K+1):
        abon.append(sum(abs_p[k:k+K-1]))

    print((min(abon)))

main()

# Optimized solution
import sys

def main():
    N, K = list(map(int,input().split()))
    h = [0] * N
    for i in range(N):
        h[i] = int(eval(input()))

    h = sorted(h)

    ave = []
    for j in range(0,N-K+1):
        ave.append(abs(h[j]-h[j+K-1]))

    print((min(ave)))

main()
# Original solution
n, k = list(map(int, input().split()))
h = sorted(list(int(eval(input())) for _ in range(n)))

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

print((min(res)))



# Optimized solution
import sys

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    n, k = list(map(int, input().split()))
    H = sorted(list(int(eval(input())) for _ in range(n)))

    res = f_inf
    for i in range(n - k + 1):
        diff = H[i + k - 1] - H[i]
        res = min(res, diff)

    print(res)


if __name__ == '__main__':
    resolve()
# Original solution
N,K = list(map(int,input().split()))
h = []
for i in range(N):
    h.append(int(eval(input())))

h.sort()
tree = []

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

print((min(tree)))

# Optimized solution
N,K = list(map(int,input().split()))
h = [int(eval(input())) for _ in range(N)]
h.sort()
ans = 1e10
for i in range(N-K+1):
    ans = min(ans,h[i+K-1]-h[i])

print(ans)
# Original solution
n, k = list(map(int, input().split()))
hlist = []
for i in range(n):
    hlist.append(int(eval(input())))
hlist.sort()
h = []
for j in range(n-k+1):
    h.append(max(hlist[j:j+k])-min(hlist[j:j+k]))
print((min(h)))


# Optimized solution
n, k = list(map(int, input().split()))
hlist = []
for i in range(n):
    hlist.append(int(eval(input())))
hlist.sort()
h = []
for j in range(n-k+1):
    h.append(hlist[j+k-1]-(hlist[j]))
print((min(h)))
# Original solution
N, K = list(map(int, input().split()))
hs = sorted([int(eval(input())) for _ in range(N)])

min_diff = 10 ** 10
for i in range(N-K+1):
    tmp = hs[i:i+K]
    min_diff = min(min_diff, tmp[-1] - tmp[0])

print(min_diff)


# Optimized solution
N, K = list(map(int, input().split()))
hs = sorted([int(eval(input())) for _ in range(N)])

min_diff = 10 ** 10
for i in range(N-K+1):
    min_diff = min(min_diff, hs[i+K-1] - hs[i])

print(min_diff)
# Original solution
n,k=list(map(int,input().split()))
h=[int(eval(input())) for i in range(n)]
h.sort()
ans=float("inf")
for i in range(len(h)-k+1):
	ma = max(h[i:i+k])
	mi = min(h[i:i+k])
	ans = min(ans,ma-mi)

print(ans)

# Optimized solution
n,k=list(map(int,input().split()))
h=[int(eval(input())) for i in range(n)]
h.sort()
ans=float("inf")
for i in range(len(h)-k+1):
	ma = h[i+k-1]
	mi = h[i]
	ans = min(ans,ma-mi)

print(ans)
# Original solution
N, M = list(map(int, input().split()))
K = [int(eval(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)

# Optimized solution
N, M = list(map(int, input().split()))
K = [int(eval(input())) for i in range(N)]
K = sorted(K)
print((min([K[i+M-1]-K[i] for i in range(N-M+1)])))
# Original solution
from itertools import combinations as comb

n, k = list(map(int, input().split()))
p_list = []
for i in range(n):
    p = int(eval(input()))
    p_list.append(p)

comb_list = list(comb(p_list, k))
min = 10000000000000000
for c in comb_list:
    c = sorted(list(c))
    if min > c[-1]-c[0]:
        min = c[-1]-c[0]

print(min)

# Optimized solution
n, k = list(map(int, input().split()))
h_list = []
for i in range(n):
    h_list.append(int(eval(input())))
h_list.sort()
min = h_list[-1]
for j in range(n-k+1):
    if min > h_list[j+k-1] - h_list[j]:
        min = h_list[j+k-1] - h_list[j]

print(min)
# Original solution
N,K=list(map(int,input().split()))
L=[]
ans=float("inf")
for i in range (N):
	L.append(int(eval(input())))
S=sorted(L)
for i in range(N-K+1):
	M=S[i:i+K]
	ans=min(ans,S[i+K-1]-S[i])
print(ans)


# Optimized solution
N,K=list(map(int,input().split()))
L=[]
ans=float("inf")
for i in range (N):
	L.append(int(eval(input())))
S=sorted(L)
for i in range(N-K+1):
	ans=min(ans,S[i+K-1]-S[i])
print(ans)
# Original solution
N,K=list(map(int,input().split()))
L=[]
ans=float("inf")
for i in range (N):
	L.append(int(eval(input())))
S=sorted(L)
for i in range(N-K+1):
	ans=min(ans,S[i+K-1]-S[i])
print(ans)


# Optimized solution
N,K=list(map(int,input().split()))
L=[]
for i in range(N):
	x=int(eval(input()))
	L.append(x)
L=sorted(L)
ans=float("inf")
for i in range(N-K+1):
	ans=min(ans,(L[i+K-1]-L[i]))
print(ans)
# Original solution
n, k = list(map(int, input().split()))
h = [int(eval(input())) for _ in range(n)]

h.sort()
ans = 10**9
for i in range(0, n-k+1):
   hmax = max(h[i:i+k])
   hmin = min(h[i:i+k])
   if (hmax - hmin < ans):
      ans = hmax - hmin

print(ans)


# Optimized solution
n, k = list(map(int, input().split()))
height = [int(eval(input())) for _ in range(n)]

height.sort()

diff = [height[i+k-1] - h for i, h in enumerate(height) if i+k-1 < len(height)]
print((min(diff)))
# Original solution
N,K = list(map(int,input().split(" ")))
height = []
for i in range(N):
    height.append(int(eval(input())))
height.sort()
min = 10**9
n = N-K+1
for j in range(n):
    light = height[j:j+K]
    hmin = light[0]
    hmax = light[K-1]
    hei = hmax-hmin
    if hei <= min:
        min = hei
print(min)

# Optimized solution
N,K = list(map(int,input().split(" ")))
height = []
for i in range(N):
    height.append(int(eval(input())))
height.sort()
min = 10**9
n = N-K+1
for j in range(n):
    hmin = height[j]
    hmax = height[j+K-1]
    hei = hmax-hmin
    if hei <= min:
        min = hei
print(min)
# Original solution
def main():
    N, K = list(map(int, input().split()))
    S = [int(eval(input())) for _ in range(N)]

    S.sort(reverse=True)

    diff_arr = [S[i] - S[i+K-1] for i in range(len(S)-K+1)]
    print((min(diff_arr)))

if __name__ == "__main__":
    main()

# Optimized solution
def main():
    N, K = list(map(int, input().split()))
    S = [int(eval(input())) for _ in range(N)]

    S.sort(reverse=True)

    diff_arr = [S[i] - S[i+K-1] for i in range(len(S)-K+1)]
    # [print(S[i], S[i+K-1], i, i+K-1) for i in range(len(S)-K)]
    
    # print(diff_arr)
    # mx_idx = diff_arr.index(min(diff_arr))
    # mn_idx = mx_idx + K - 1

    print((min(diff_arr)))

if __name__ == "__main__":
    main()
# Original solution
N,K = list(map(int,input().split()))
h = list(int(eval(input())) for i in range(N))
h.sort(reverse=True)
cnt = 0
ans = []
dif = float('inf') 
for i in range(N):
    if K-1<=i:
        if dif>ans[0]-h[i]:
            dif = ans[0]-h[i]
        del ans[0]
    ans.append(h[i])
print(dif) 

# Optimized solution
N,K = list(map(int,input().split()))
h = list(int(eval(input())) for i in range(N))
h.sort(reverse=True)
dif = float('inf') 
for i in range(K-1,N):
    dif = min(dif,h[i-K+1]-h[i])
print(dif)
# Original solution
n,k  = list(map(int,input().split()))
l = sorted([int(eval(input())) for i in range(n)])
#print(l)
ansl = []
for m in range(n-k+1):
  ansl.append(max(l[m:m+k])-min(l[m:m+k]))
print((min(ansl)))

# Optimized solution
n,k  = list(map(int,input().split()))
l = sorted([int(eval(input())) for i in range(n)])
print((min(l[i+k-1] - l[i] for i in range(n-k+1))))
# Original solution
n,k = list(map(int,input().split()))
l = []
for _ in range(n):
    a = int(eval(input()))
    l.append(a)

ans = 10**9
l.sort()
for i in range(n-k+1):
    tmp = max(l[i:k+i]) - min(l[i:k+i])
    if tmp < ans:
        ans = tmp

print(ans)

# Optimized solution
n,k = list(map(int,input().split()))
l = []
for _ in range(n):
    a = int(eval(input()))
    l.append(a)

ans = 10**9
l.sort()

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

print(ans)
# Original solution
n, k = list(map(int, input().split()))
h = [int(eval(input())) for _ in range(n)]
h = sorted(h)
print((min(h[i + k - 1] - h[i] for i in range(n - k + 1))))


# Optimized solution
N, K = list(map(int, input().split()))
h = [int(eval(input())) for _ in range(N)]

h = list(sorted(h))

ans = min(hr - hl for hr, hl in zip(h[K - 1:], h))
print(ans)
# Original solution
n, k = list(map(int, input().split()))
h = [int(eval(input())) for _ in range(n)]
h.sort()
print((min(hmax - hmin for hmax, hmin in zip(h[k - 1:], h))))


# Optimized solution
def main():
    import sys
    input = sys.stdin.readline

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

    h.sort()

    ans = min(ma - mi for mi, ma in zip(h, h[K - 1:]))
    print(ans)


if __name__ == '__main__':
    main()

# import sys
# input = sys.stdin.readline
# 
# sys.setrecursionlimit(10 ** 7)
# 
# (int(x)-1 for x in input().split())
# rstrip()
#
# def binary_search(*, ok, ng, func):
#     while abs(ok - ng) > 1:
#         mid = (ok + ng) // 2
#         if func(mid):
#             ok = mid
#         else:
#             ng = mid
#     return ok
# Original solution
N, K = list(map(int, input().split()))
h =[int(eval(input())) for _ in range(N)]
a = sorted(h)
ans = min(a[i + K - 1] - a[i] for i in range(N - K + 1))
print(ans)

# Optimized solution
N, K = list(map(int, input().split()))
h = [int(eval(input())) for _ in range(N)]

x = sorted(h)
ans = min(b - a for a, b in zip(x, x[(K - 1):]))

print(ans)
# Original solution
n,k=list(map(int,input().split()))
h=[]
for i in range(n):
    h.append(int(eval(input())))
h.sort()

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


# Optimized solution
n,k=list(map(int,input().split()))
h=[]
for i in range(n):
    h.append(int(eval(input())))
h.sort()

ans=h[-1]-h[0]
for i in range(n-k+1):
    ans=min(ans,h[i+k-1]-h[i])
print(ans)
# Original solution
# -*- coding: utf-8 -*-
n,k = list(map(int, input().split(' ')))
prices = []
for i in range(n):
    prices.append(int(eval(input())))

sortedPrices = sorted(prices)


trees = []
for i in range(k):
    trees.append(sortedPrices.pop(0))

hdiff = 1000000000

while(True):
    hdiff = min( (trees[-1] - trees[0]), hdiff )
    trees.pop(0)
    if len(sortedPrices) == 0:
        break
    trees.append(sortedPrices.pop(0))

print(hdiff)

# Optimized solution
# -*- coding: utf-8 -*-
N, K = list(map(int, input().split(' ')));
h = [int(eval(input())) for i in range(N)]
h.sort()
print(( min( h[i+K-1] - h[i] for i in range(N-K+1) ) ))
# Original solution
# coding: utf-8
N, K = list(map(int, input().split()))
H = []
for i in range(N):
    h = int(eval(input()))
    H.append(h)
H.sort()
L = []
for i in range(N-K+1):
    tmp = H[i:i+K]
    L.append(abs(tmp[0] - tmp[-1]))
print((min(L)))

# Optimized solution
# coding: utf-8
N, K = list(map(int, input().split()))
H = []
for i in range(N):
    h = int(eval(input()))
    H.append(h)
H.sort()
L = []
ans = float("inf")
for i in range(N-K+1):
    ans = min(ans, abs(H[i] - H[i+K-1]))
print(ans)
# Original solution
n, k = list(map(int, input().split()))
h = []
for i in range(n):
  h_ = int(eval(input()))
  h.append(h_)
h.sort()
#print(h)
ans = float('inf')
for i in range(n-k+1):
  ans = min(ans, h[i+k-1]-h[i])
print(ans)

# Optimized solution
n,k=list(map(int,input().split()))
h=[int(eval(input())) for i in range(n)]
h.sort(reverse=True)
ans=float('inf')
for i in range(n-k+1):
  ans=min(h[i]-h[i+k-1],ans)
print(ans)
# Original solution
n, k = list(map(int, input().split()))
h = [int(eval(input())) for _ in range(n)]
sh = sorted(h)
u = []
for i in range(len(sh)-k+1):
    a = sh[i:i+k]
    u.append(max(a) - min(a))
print((min(u)))

# Optimized solution
n, k = list(map(int, input().split()))
h = [int(eval(input())) for _ in range(n)]
sh = sorted(h)
print((min([sh[i+k-1]-sh[i] for i in range(n-k+1)])))
# Original solution
import itertools

def main():
    N, K = list(map(int, input().split()))
    h = []
    for i in range(N):
        h.append(int(eval(input())))

    arr = list(itertools.permutations(h, K))
    res1 = []
    res2 = []
    ans = []
    for j in range(len(arr)):
        res1.append(sorted(arr[j]))

    for k in range(len(res1)):
        res2.append(sorted(res1[k]))

    for l in range(len(res2)):
        ans.append(res2[l][K-1] - res2[l][0])
    print((min(ans)))

if __name__ == "__main__":
    main()


# Optimized solution
N, K = list(map(int, input().split()))
h = [int(eval(input())) for i in range(N)]
h.sort()
print((min(h[i+K-1] - h[i] for i in range(N-K+1))))
# Original solution
n,k = list(map(int,input().split()))
tree = [int(eval(input())) for _ in range(n)]
tree.sort()
ans = int(1e15)
for i in range(n-k+1):
    ans = min(max(tree[i:i+k]) - min(tree[i:i+k]), ans)
print(ans)


# Optimized solution
n,k = list(map(int,input().split()))
tree = [int(eval(input())) for _ in range(n)]
tree.sort()
ans = int(1e15)
for i in range(n-k+1):
    ans = min(tree[i+k-1] - tree[i], ans)
print(ans)
# Original solution
N, K = list(map(int, input().split()))
H = []
for i in range(N):
        h = int(eval(input()))
        H.append(h)

H.sort()
ans = []

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

# Optimized solution
n,k=list(map(int,input().split()))
H=[]
for i in range(n):
    H.append(int(eval(input())))
 
H.sort()
 
#print(H)
 
ans=H[n-1]-H[0]
 
for i in range(n-k+1):
    #print(H[i+k-1]-H[i])
    ans=min(ans,H[i+k-1]-H[i])
 
print(ans)
# Original solution
NK = list(map(int, input().split()))
a = [0]*NK[0]
for i in range(len(a)):
    a[i] = int(eval(input()))
a.sort()
N = NK[0]
K = NK[1]

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

print((min(offsets)))


# Optimized solution
NK = list(map(int, input().split()))
a = [0]*NK[0]
for i in range(len(a)):
    a[i] = int(eval(input()))
a.sort()
N = NK[0]
K = NK[1]

minimum = a[N-1]
for i in range(N-K+1):
    offset = a[i+K-1] - a[i]
    if minimum > offset:
        minimum = offset

print(minimum)
# Original solution
import sys
from collections import deque
input = sys.stdin.readline
N, K = [int(x) for x in input().split()]
h = []
for _ in range(N):
    h.append(int(eval(input())))
h.sort()
i = 0
ans = []
while N - i >= K:
    temp = h[i:i+K]
    ans.append(temp[-1] - temp[0])
    i += 1
print((min(ans)))


# Optimized solution
N, K = list(map(int, input().split()))
h = [int(eval(input())) for i in range(N)]
h.sort()
print((min(h[i+K-1] - h[i] for i in range(N-K+1))))
# Original solution
import sys
from operator import itemgetter as ig
from collections import defaultdict as dd

# お約束
args = None
INF = float("inf")
MOD = int(1e9 + 7)
def int1(n):
    return int(n) - 1
def input():
    return next(args)
def parse(*params):
    if len(params) == 1:
        return params[0](next(args))
    return tuple(p(v) for p, v in zip(params, next(args).split()))
def debug(*v):
    if __debug__:
        print(*v, file=sys.stderr)

# エントリーポイント
def main():
    N, K = parse(int, int)
    H = [parse(int) for _ in range(N)]

    h = sorted(H)
    l, r = 0, K

    sum_h = [0]
    for n in range(1, N):
        sum_h += [sum_h[-1] + (h[n] - h[n - 1])]
    debug(sum_h)

    diff_h = tuple(sum_h[l + K - 1] - sum_h[l] for l in range(N - K + 1))
    debug(diff_h)
    print(min(diff_h))

if __name__ == '__main__':
    args = iter(sys.stdin.read().split("\n"))
    main()


# Optimized solution
import sys
from operator import itemgetter as ig
from collections import defaultdict as dd

# お約束
args = None
INF = float("inf")
MOD = int(1e9 + 7)
def int1(n):
    return int(n) - 1
def input():
    return next(args)
def parse(*params):
    if len(params) == 1:
        return params[0](next(args))
    return tuple(p(v) for p, v in zip(params, next(args).split()))
def debug(*v):
    if __debug__:
        print(*v, file=sys.stderr)

# エントリーポイント
def main():
    N, K = parse(int, int)
    H = [parse(int) for _ in range(N)]

    h = sorted(H)

    diff_h = tuple(h[l + K - 1] - h[l] for l in range(N - K + 1))

    print(min(diff_h))

if __name__ == '__main__':
    args = iter(sys.stdin.read().split("\n"))
    main()
# Original solution
n,k = input().split()
n=int(n)
k=int(k)
h = [int(eval(input())) for i in range(n)]
h.sort()
#print(h)
ans=10000000000

for i in range(n-k+1):
  list1 = [h[j] for j in range(i,i+k)]
#  print(list1)
  if max(list1)-min(list1) < ans:
      ans = max(list1)-min(list1)
print(ans)

# Optimized solution
n,k = input().split()
n=int(n)
k=int(k)
h = [int(eval(input())) for i in range(n)]
h.sort()
#print(h)
ans=10000000000

for i in range(n-k+1):
#  list1 = [h[j] for j in range(i,i+k)]
#  print(list1)
  if h[i+k-1]-h[i] < ans:
      ans = h[i+k-1]-h[i]
print(ans)
# Original solution
N,K = list(map(int, input().split()))
A = [list(map(int,input().split())) for i in range(N)]

def bubble_sort(arr):
    change = True
    while change:
        change = False
        for i in range(len(arr) - 1):
            if arr[i] > arr[i + 1]:
                arr[i], arr[i + 1] = arr[i + 1], arr[i]
                change = True
    return arr

B=[]
for i in range(N):
    B.append(A[i][0])
    
C=bubble_sort(B)

ans = C[K-1] - C[0]
for i in range(N-K+1):
    if ans > C[i+K-1] -C[i]:
        ans = C[i+K-1] -C[i]
        
print(ans)

# Optimized solution

N, K = list(map(int, input().split()))
h = [int(eval(input())) for i in range(N)]
h.sort()
print((min(h[i+K-1] - h[i] for i in range(N-K+1))))
# Original solution
from itertools import combinations

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

ans=10**9
for i in combinations(H, K):
    ans=min(ans,max(i)-min(i))
print(ans)

# Optimized solution
N,K=list(map(int,input().split()))
H=[int(eval(input())) for _ in range(N)]

H=sorted(H)
ans=10**9
for i in range(N-K+1):
    ans=min(ans,H[i+K-1]-H[i])
print(ans)
# Original solution
def christmas_eve(n,k,h):
    _h = sorted(h)
    opt = 0
    ans = 10**9 + 1

    for i in range(n-(k-1)):
        l = _h[i:i+k]
        opt = max(l) - min(l)
        if ans > opt:
            ans = opt

    return ans
if __name__ == '__main__':
    n,k = list(map(int,input().split()))
    h = [int(input()) for _ in range(n)]


    print(christmas_eve(n,k,h))

# Optimized solution
def christmas_eve(n,k,h):
    _h = sorted(h)
    opt = 0
    ans = 10**9 + 1
    _k = k - 1
    loop = n - _k
    for i in range(loop):
        opt = _h[i+_k] - _h[i]
        if ans >= opt:
            ans = opt

    return ans
if __name__ == '__main__':
    n,k = list(map(int,input().split()))
    h = [int(input()) for _ in range(n)]

    print(christmas_eve(n,k,h))