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):
nk = input().split()
n = int(nk[0])
k = int(nk[1])
hlist = []
for i in range(n):
    hlist.append(int(input()))

hlist.sort()

submin = 999999999999999999999999999999999
for i in range(0, n - k + 1):
    submin = min(submin, hlist[i+k-1] - hlist[i])

print(submin)

Solution (Python):
ans = 10**9

N,K = map(int, input().split())
H = []
for i in range(N):
    x = int(input())
    H.append(x)
H = sorted(H)
for i in range(N-(K-1)):
    y = H[i+(K-1)]-H[i]
    ans = min(y,ans)
print(ans)

Solution (Python):
N, K = map(int, input().split())
h = []
for i in range(N):
    h.append(int(input()))
    
h.sort()
hight = []
for i, j in enumerate(h):
    if i >= len(h) - K + 1:
        break
    hight.append(h[i + K - 1] - h[i])
hight.sort()
print(hight[0])

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

Solution (Python):
import sys
input = sys.stdin.readline
N, K = map(int, input().split())
h = sorted([int(input()) for _ in [0]*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()))
h = sorted([int(input()) for i in range(n)])
print(min(h[i] - h[i - k + 1] for i in range(k - 1, n)))

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

Solution (Python):
N, K = map(int, input().split())
h = list(int(input()) for i in range(N))
h.sort()
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 = []
for i in range(n):
    h.append(int(input()))
h = sorted(h)
ans = 1e9+18
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=1000000000
for i in range(k-1,n):
  ans=min(ans,h[i]-h[i-k+1])
print(ans)

Solution (Python):
import heapq

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

arr=[0]*N
for z in range(N):
  heapq.heapreplace(arr,int(input()))

arr2=[0]*K
for z in range(K):
  arr2[z]=heapq.heappop(arr)

r=arr2[-1]-arr2[0]

z=0
try:
  while True:
    arr2[z]=heapq.heappop(arr)
    if z==K-1:
      zz=0
    else:
      zz=z+1

    rb=arr2[z]-arr2[zz]
    if rb<r:
      r=rb

    z+=1
    if z==K:
      z=0

except Exception:
  pass

print(r)

Solution (Python):
import heapq

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

arr=[0]*N
for z in range(N):
  heapq.heapreplace(arr,int(input()))

arr2=[0]*K
for z in range(K):
  arr2[z]=heapq.heappop(arr)

r=arr2[-1]-arr2[0]

z=0
try:
  while True:
    arr2[z]=heapq.heappop(arr)
    if z==K-1:
      zz=0
    else:
      zz=z+1

    rb=arr2[z]-arr2[zz]
    if rb<r:
      r=rb

    z+=1
    if z==K:
      z=0

except Exception:
  pass

print(r)

Solution (Python):
if __name__ == '__main__':
    n, k = map(int, input().split())
    trees = [0] * n
    for i in range(n):
        trees[i] = int(input())
    trees.sort()

    ans = 10 ** 9
    for i in range(n - k + 1):
        diff = trees[i + k - 1] - trees[i]
        if diff < ans:
            ans = diff
    print(ans)

Solution (Python):

def main():
    a, b = map(int, input().split())
    data = [int(input()) for i in range(a)]
    data.sort()
    ans = data[-1] - data[0]

    for i in range(a - b + 1):
        now_data = data[i + b - 1] - data[i]
        if now_data < ans:
            ans = now_data

    print(ans)



if __name__ == '__main__':
    main()

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

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

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

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

heights.sort()

ans = float('inf')
for i in range(len(heights)):
    if i + k - 1 >= len(heights):
        break
    ans = min(ans, heights[i + k - 1] -  heights[i])

print(ans)

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

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

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

Solution (Python):

import sys
INF = float("inf")


def solve(N: int, K: int, h: "List[int]"):
    h.sort()
    print(min(h[i+K-1]-h[i] for i in range(N-K+1)))

    return


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_list = [int(input()) for _ in range(N)]

h_list.sort(reverse=True)

sa = [0 for _ in range(N-K+1)]
for i in range(N-K+1):
    sa[i] = h_list[i] - h_list[i+K-1]

print(min(sa))

Solution (Python):

import sys


def solve(N: int, K: int, h: "List[int]"):
    minDiff = 10 ** 18
    for i in range(N - K + 1):
        minDiff = min(minDiff,h[i+K - 1] - h[i])
    print(minDiff)
    return



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))  
    q = []
    for i in range(N):
        q.append(int(next(tokens)))
    q.sort()
    solve(N, K, q)

if __name__ == '__main__':
    main()

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

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

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

Solution (Python):
N, K = map(int, raw_input().split())
tree = []
for i in range(0, N):
	tree.append(int(raw_input()))
tree.sort()
min = tree[N-1]
for i in range(0, N-K+1):
	if tree[i+K-1] - tree[i] < min: 
		min = tree[i+K-1] - tree[i]

print min

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

h = []

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

h.sort(reverse=True)

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

print(ans)

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

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

print(min)

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

h.sort()

min_h = 1000000000

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

print(min_h)

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

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

h = sorted(h)
hK = h[0:K]
ans = hK[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 = list(map(int, input().split()))
H = sorted([int(input()) for _ in range(N)])

min_h = 1e15
for i in range(N-K+1):
    tmp = H[i+K-1] - H[i]
    if tmp < min_h:
        min_h = tmp
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):
from sys import stdin
n, k = [int(_) for _ in stdin.readline().rstrip().split()]
h = [int(stdin.readline().rstrip()) 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()
ans = 10 ** 9
for i in range(0, N - K + 1):
  ans = min(ans, h[i + K - 1] - h[i])
print(ans)

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

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

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

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

ans = h[K - 1] - h[0]
for i in range(1, 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())
lis = [int(input()) for x in range(N)]
lis.sort()
print(min(lis[i+K-1] - lis[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)])
ans = 10e9 + 1

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

print(ans)

Solution (Python):
N, K = map(int, input().split())
h = [int(input()) for i in range(N)]
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 = map(int, input().split())
a = []
for i in range(n):
	a.append(int(input()))
a = sorted(a)
counter = a[k - 1] - a[0]
for i in range(n - k + 1):
	b = a[i + k - 1] - a[i]
	if counter > b:
		counter = b
print(counter)

Solution (Python):
def main():
	n, k = map(int, input().split())
	h = [0]*n
	diff = [0]*n
	for i in range(n):
		h[i] = int(input())
	h.sort()
	min_diff = 10**9
	for i in range(n-k+1):
		this_diff = h[i+k-1] - h[i]
		if this_diff < min_diff:
			min_diff = this_diff
	print(min_diff)

if __name__ == '__main__':
    main()

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

h.sort()
min = 10**9
for i in range(n-k+1):
    if h[i+k-1] - h[i] < min:
        min = h[i+k-1] - h[i]
print(min)

Solution (Python):
n,k=map(int,input().split())
trees= [int(input()) for i in range(n)]
trees.sort()
print(min(trees[i+k-1]-trees[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()
dif = [h[i+K-1] - h[i] for i in range(N-K+1)]
res = min(dif)
print(res)

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

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

Solution (Python):
trees_num, illuminations_num = map(int, input().split())
trees = sorted(int(input()) for _ in range(trees_num))

diffs = []

for i in range(trees_num - illuminations_num + 1):
    diffs.append(trees[i+illuminations_num-1] - trees[i])

print(min(diffs))

Solution (Python):
trees_num, illuminations_num = map(int, input().split())
trees = sorted(int(input()) for _ in range(trees_num))

diffs = []

for i in range(trees_num - illuminations_num + 1):
    diffs.append(trees[i+illuminations_num-1] - trees[i])

print(min(diffs))

Solution (Python):
n, k = list(map(int, input().split()))
h_li = []
for i in range(n):
    h = int(input())
    h_li.append(h)
h_li.sort()
d = 10 ** 10
m = k - 1
while m < n:
    if d > (h_li[m] - h_li[m - k + 1]):
        d = h_li[m] - h_li[m - k + 1]
    m += 1
print(d)

Solution (Python):
n, k = map(int, input().split())
h = [int(input()) for _ in range(n)]
h.sort()
mini = 10 ** 10
for i in range(0, n - k + 1):
    temp = h[i + k - 1] - h[i]
    if temp < mini:
        mini = temp
print(mini)

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

h = sorted(h)

ans = 10000000000
for i in range(N - K + 1):
    if ans > h[i + K - 1] - h[i]:
        ans = h[i + K - 1] - h[i]

print(ans)

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

item=[]
for i in range(n):
    b=int(input())
    item.append(b)
    
item.sort()
temp=[]

for i in range(n-p+1):
    a=item[i+p-1]-item[i]
    temp.append(a)
    
temp.sort()
print(min(temp))

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

s = [0] * N
for i in range(1, N):
    s[i] = s[i-1] + (hn[i] - hn[i-1])
m = min(s[i+K-1] - s[i] for i in range(0, N-K+1))

print(m)

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

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

diff = hs[-1]
for i in range(N - K + 1):
    diff = min(hs[i+K-1] - hs[i], diff)
print(diff)

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

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

Solution (Python):

import sys


def solve(N: int, K: int, h: "List[int]"):
    h.sort()
    ret = float('inf')
    for i in range(K - 1, N):
        ret = min(ret, h[i] - h[i - K + 1])
    print(ret)
    return


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 = [int(input()) for i in range(n)]
h.sort()
sabun = [h[i+k-1] - h[i] for i in range(n-k+1)]
ans = min(sabun)
print(ans)

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

Solution (Python):
import collections
def c():
    n, k = map(int, input().split())
    h = []
    for i in range(n):
        h.append(int(input()))
    h.sort()
    d = collections.deque(maxlen = k)
    mins = []
    while len(d) < k:
        d.append(h.pop())
    while h:
        mins.append(d[0] - d[k-1])
        d.append(h.pop())
    mins.append(d[0] - d[k-1])
    print(min(mins))
c()

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

ans = 1100000000
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=h[N-1]-h[0]

x=[h[i+K-1]-h[i] for i in range(N-K+1)]
print(min(x))

Solution (Python):
N, K = map(int, input().split())
lengths = list(int(input()) for i in range(N))
sorted_lengths = sorted(lengths)
min_score = 2**100

for i in range(N-K+1):
  score = sorted_lengths[i+K-1] - sorted_lengths[i]
  min_score = min(min_score, score)

print(min_score)

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

h = sorted(h)
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 = []
for i in range(N):
	h.append(int(input()))
h.sort()
ans = max(h) - min(h)
for i in range(N - K + 1):
	k = h[i + K - 1] - h[i]
	if k < ans:
		ans = k
print(ans)

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

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

print(ans)

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

Solution (Python):

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

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

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

Solution (Python):
n, k = map(int, input().split())
h = [int(input()) for i in range(n)]
h = sorted(h)
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()]
H = sorted([int(input()) for i in range(N)])
ans = H[K - 1] - H[0]
for i in range(N - K + 1):
    t = H[K + i - 1] - H[i]
    if ans > t:
        ans = t
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):
    x = h[i + k - 1] - h[i]
    if x < ans:
        ans = x
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(len(H)-k+1):
  ans=min(ans,abs(H[i]-H[i+k-1]))
print(ans)

Solution (Python):
n,k = map(int,input().split())
s = [int(input()) for i in range(n)]
s.sort()
c = s[k-1] - s[0]
for i in range(1,n-k+1):
    t = s[i+k-1] - s[i] 
    if t < c:
        c = t

print(c)

Solution (Python):
n,k = map(int,input().split())
s = [int(input()) for i in range(n)]
s.sort()
c = s[k-1] - s[0]
for i in range(1,n-k+1):
    if s[i+k-1] - s[i] < c:
        c = s[i+k-1] - s[i]

print(c)

Solution (Python):
n,k = map(int,input().split())
s = [int(input()) for i in range(n)]
s.sort()
c = s[k-1] - s[0]
for i in range(1,n-k+1):
    if s[i+k-1] - s[i] < c:
        c = s[i+k-1] - s[i]

print(c)

Solution (Python):
N, K = map(int, input().split())  
li = list()
for n in range(N):
    li.append(int(input()))

li.sort()

ans = float("inf")
for i in range(N - K + 1):
    if ans > li[i + K - 1] - li[i]:
        ans = li[i + K - 1] - li[i]
print(ans)

Solution (Python):


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

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


dif_h = []

K = K -1

for i in range(K, N):
    dif_h.append(h[i]-h[i-K])

ascending_dif_h = dif_h.sort()


print(dif_h[0])

Solution (Python):


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

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


dif_h = []

K = K -1

for i in range(K, N):
    dif_h.append(h[i]-h[i-K])

ascending_dif_h = dif_h.sort()


print(dif_h[0])

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

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

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

d= h[k-1]-h[0]

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

Solution (Python):
from bisect import bisect_left, bisect_right
from collections import Counter, defaultdict, deque, OrderedDict
from copy import deepcopy
from functools import lru_cache, reduce
from math import ceil, floor
from sys import setrecursionlimit

import heapq
import itertools
import operator


inf = float('inf')



N, K = 0, 0
H = []


def set_inputs():
    global N, K, H
    N, K = get_li()
    H = get_data(N, [int])
    return


def main():
    setrecursionlimit(100000)
    set_inputs()
    sorted_h = sorted(H)
    diff = inf
    for _min, _max in zip(sorted_h, sorted_h[K-1:]):
        diff = min(diff, _max - _min)
    print(diff)
    return


def get_int():
    return int(input())


def get_float():
    return float(input())


def get_str():
    return input().strip()


def get_li():
    return [int(i) for i in input().split()]


def get_lf():
    return [float(f) for f in input().split()]


def get_lc():
    return list(input().strip())


def get_data(n, types):
    if len(types) == 1:
        return [types[0](input()) for _ in range(n)]
    return zip(*(
        [t(x) for t, x in zip(types, input().split())]
        for _ in range(n)
    ))


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()
ans = []
for x, y in zip(h[:N-K+1], h[K-1:]):
    ans.append(y-x)
print(min(ans))

Solution (Python):
N, K = map(int, input().split(" "))
treehight = []
difference = []
for i in range(N):
    treehight.append(int(input()))
treehight.sort()
for j in range(N - K + 1):
    difference.append(treehight[j+K-1]-treehight[j])

print(min(difference))

Solution (Python):
def gcd_core(a,b):
    if b == 0:
        return a
    else:
        return gcd_core(b,a%b)

def gcd(arr):
    g = gcd_core(arr[0],arr[1])
    for i in range(2,len(arr)):
        g = gcd_core(g,arr[i])
    return g

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

arr.sort()
ans = 1000000000000000000
for i in range(n-k+1):
    if ans > arr[i+k-1]-arr[i]:
        ans = arr[i+k-1]-arr[i]

print(ans)

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

c_sum = [0]
for i in range(len(h)-1):
    c_sum.append(c_sum[-1] + h[i+1]-h[i])

ans = float('inf')
for i in range(len(h)-K+1):
    ans = min(ans, c_sum[i+K-1] - c_sum[i])

print(ans)

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

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

print( min( list_h[i + K - 1] - list_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()

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

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

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

li_h = list()
for i in range(n):
    li_h.append(int(input()))

s_li_h = sorted(li_h)

wk = k - 1
li_diff = list()
for i in range(wk, n):
    li_diff.append(s_li_h[i] - s_li_h[i - wk])

print(min(li_diff))

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

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

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

Solution (Python):
from sys import exit
N,K = [int(n) for n in input().split()]
K-=1
h = [int(input()) for _ in range(N)]
h = sorted(h)
minm = 10**9


for i in range(0,N-K):
    
    if h[i+K]-h[i] < minm:
        minm = h[i+K]-h[i]
print(minm)

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

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

loopn = n-k+1

ans = 10**10

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

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

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

print(ans)

Solution (Python):
import sys
stdin = sys.stdin
read_int = lambda : list(map(int,stdin.readline().split()))
read_str = lambda : stdin.readline().rstrip()

N,K = read_int()
h = [read_int()[0] for _ in range(N)]
h.sort()

def solve():
    ans = float('INF')
    for i in range(N - K + 1):
        ans = min(ans, h[i + K - 1] - h[i])
    return ans

if __name__ == "__main__":
    print(solve())

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

h = sorted(h)
t = [0] * N
for i in range(N - 1):
    t[i + 1] = t[i] + (h[i + 1] - h[i])
ans = float('inf')
for j in range(N - K + 1):
    ans = min(ans, t[j + K - 1] - t[j])
print(ans)

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

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

ans = 1e9+7
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 = 10 ** 10
for i in range(n-k+1):
    ans = min(ans,h[i+k-1] - h[i])
print(ans)

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

Solution (Python):
N, K = map(int,input().split())
a = 1000000000
h = list(map(int, [input() for i in range(N)]))
h.sort()
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 = [int(input()) for i in range(N)]
H.sort()
min_diff = 10 ** 9 
for i in range(N-K+1):
    low = H[i]
    high = H[i+K-1]
    min_diff = min(min_diff, high - low)
print(min_diff)

Solution (Python):
N,K=[int(i) for i in 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())
hs = [int(input()) for _ in range(N)]
hs_sort = sorted(hs)
diff_min = 1e10
for i in range(N-K+1):
    diff = hs_sort[i+K-1] - hs_sort[i]
    if diff <= diff_min:
        diff_min = diff
    else:
        continue
print(diff_min)

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

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

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

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

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

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

Solution (Python):
import numpy as np
N, K = map(int,input().split())
H = np.array([int(input()) for _ in range(0, N)])
H = np.sort(H)
H_diff = np.diff(H)
best = int(np.min(np.convolve((H_diff), np.ones(K-1), mode='valid')))
print(best)

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

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

list.sort()

init = max(list) - min(list)

diff = [b-a for a,b in zip(list,list[K-1::])]

print(min(diff))

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

Solution (Python):
N,K = map(int,input().split())
h = sorted([int(input()) for _ 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_list = []
for i in range(n):
    h_list.append(int(input()))
h_list = sorted(h_list)
diff_h = [h_list[i+k-1] - h_list[i] for i in range(len(h_list) - k + 1)]
print(min(diff_h))

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

print(min)

Solution (Python):

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

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

Solution (Python):

from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS():return list(map(list, sys.stdin.readline().split()))
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = I()
    return l
def LIR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LI()
    return l
def SR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = S()
    return l
def LSR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = SR()
    return l
mod = 1000000007


def A():
    return


def B():
    return


def C():
    n,k = LI()
    h = IR(n)
    h.sort()
    ans = float("inf")
    for i in range(n-k+1):
        ans = min(ans,h[i+k-1]-h[i])
    print(ans)


def D():
    return


def E():
    return


def F():
    return


def G():
    return


def H():
    return


if __name__ == "__main__":
    C()

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

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

tree.sort()

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