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())
arr = sorted([int(input()) for _ in range(N)])
ans = float('inf')

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

Solution (Python):
N, K, *h = map(int, open(0).read().split())

h_sort = sorted(h)

ans = 10**9

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

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

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


if __name__ == "__main__":
    main()

Solution (Python):
def resolve():
    N, K = list(map(int, input().split()))
    h = [int(input()) for _ in range(N)]
    ans = 10 ** 10
    h.sort()

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


resolve()

Solution (Python):
n,k=map(int,input().split())
h=[int(input()) for i in range(n)]
h.sort()
ans=h[k-1]-h[0]
res=float("INF")
for i in range(n-k+1):
    res=h[i+k-1]-h[i]
    if ans>res:
        ans=res
print(ans)

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

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

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

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

def merge(array):                                   
    mid = len(array)                                
    if mid > 1:                                     
        left = merge(array[:(mid//2)])               
        right = merge(array[(mid//2):])              
        array = []                                  
        while len(left) != 0 and len(right) != 0:   
            if left[0] < right[0]:                  
                array.append(left.pop(0))           
            else:                                   
                array.append(right.pop(0))          
        if len(left) != 0:                          
            array.extend(left)                      
        elif len(right) != 0:                       
            array.extend(right)                     
    return array



for i in range(n):
	a=int(input())
	S.append(a)
L=merge(S)

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

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

Solution (Python):

N, K = map(int, input().split())
h = []
ans = 10**9

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

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,input().split())
a=[int(input()) for i in range(n)]
a.sort()
b=[]
for p in range(n-k+1):
  c=a[p+k-1]-a[p]
  b.append(c)
  
print(min(b))

Solution (Python):
import sys
import math
import itertools as it
def I():return int(sys.stdin.readline().replace("\n",""))
def I2():return map(int,sys.stdin.readline().replace("\n","").split())
def S():return str(sys.stdin.readline().replace("\n",""))
def L():return list(sys.stdin.readline().replace("\n",""))
def Intl():return [int(k) for k in sys.stdin.readline().replace("\n","").split()]
def Lx(k):return list(map(lambda x:int(x)*-k,sys.stdin.readline().replace("\n","").split()))

if __name__ == "__main__":
    n ,k = I2()
    h = [I() for _ in range(n)]
    h.sort()
    ans = float("inf")
    for i in range(n-k+1):
        a,b = h[i],h[i+k-1]
        
        ans = min(ans,b-a)
    print(ans)

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

Solution (Python):
import sys

def main():
    input = sys.stdin.readline
    N, K = map(int, input().rstrip().split())
    trees = []

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

    trees.sort()
    min_diff = float("inf")

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



if __name__ == '__main__':
    main()

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

def quicksort(a, left: int, right: int):
    
    pl=left
    pr=right
    pivot= a[(left +right)//2]

    

    while pl<=pr:
        while a[pl] <pivot: pl+=1
        while a[pr] >pivot: pr-=1
        if pl <= pr:
            a[pl], a[pr]= a[pr], a[pl]
            pl+=1
            pr-=1

    if left < pr: quicksort(a, left, pr)
    if pl < right: quicksort(a, pl, right)

def quick_sort(a):
    quicksort(a, 0, len(a)-1)
mn=10**9
quick_sort(h)
for i in range(n-k+1):
    dif=h[i+k-1]-h[i]
    if dif<mn:
        mn=dif

print(mn)

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[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()
QQ=1000000000
for i in range(N-K+1):
    QQ=min(QQ,abs(h[i]-h[i+K-1]))
print(QQ)

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

Solution (Python):
def mi():
    return map(int, input().split())

def ii():
    return int(input())

def main():
    N, K = mi()
    h = [ii() for _ in range(N)]
    h.sort()
    print(min(h[i+K-1]-h[i] for i in range(N-K+1)))

if __name__ == '__main__':
    main()

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

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



h.sort()

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

print(ans)

Solution (Python):

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

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

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 i in range(N)]

h.sort()

ans = h[-1] - h[0]

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

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

Solution (Python):
import sys
input = sys.stdin.readline
def main():
    N, K = map(int, input().split())
    h = [int(input()) for i in range(N)]
    H = sorted(h)

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

if __name__ == '__main__':
    main()

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

diff = float('inf')
hh = sorted(hh)
for i in range(n - k + 1):
    diff = min(diff, hh[i + k - 1] - hh[i])
print(diff)

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

S.sort()
minh=10**10
for i in range(N-K+1):
    if minh>S[i+K-1]-S[i]:
        minh=S[i+K-1]-S[i]

print(minh)

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

Solution (Python):
import numpy as np
n, k = map(int, input().split())
h = np.array(sorted([int(input()) for _ in range(n)]))
h_roll = np.roll(h, -k + 1)
x = h_roll - h
print(int(x[:-k + 1].min()))

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

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

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, K = map(int, input().split())
h = [int(input()) for _ in range(N)]
ans = float('inf')
h.sort()
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,K=map(int,input().split())
l=list()
for i in range(N):
  l.append(int(input()))
l.sort()
ans=int(l[K-1])-int(l[0])
for i in range(1,N-K+1):
  key=int(l[i+K-1])-int(l[i])
  if ans>key:
    ans=key
print(ans)

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

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

lst=[0]*(n-1)

temp=0
for i in range(n-1):
    temp=l[i+1]-l[i]
    lst[i]=temp

ll=[0]*(n)
temp=0
for ii in range(n-1):
    temp+=lst[ii]
    ll[ii+1]=temp



ans=10**9    
for j in range(n-k+1):
    ans=min(ans,ll[j+k-1]-ll[j])
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(N-K+1):
    ans = min(ans, H[i+K-1]-H[i])

print(ans)

Solution (Python):

N, K = map(int,input().split())
M = [int(input()) for i in range(N)]
M.sort()
lis = [0]*(N-K+1)
for i in range(N-K+1):
    lis[i] = abs(M[i]-M[i+K-1])
print(min(lis))

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

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

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

print(mn)

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

Solution (Python):



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

h.sort(reverse=True)

ans = 10**10
for i in range(0, n-k+1):
    max_ = h[i]
    min_ = h[i+k-1]
    ans = min(ans, max_-min_)
print(ans)

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

tmp = 10000000000

for i in range(n-k+1):
    ma = lis[i+k-1]
    mi = lis[i]
    
    tmp = min(tmp, ma-mi)
    
print(tmp)

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

ans = 1000000000

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

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)]
ans = 10**9+7
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, input().split())
H = sorted([int(input()) for _ in range(n)])
ans = 10e10
for i in range(n - k + 1):
    ans = min(ans, H[i + k - 1] - H[i])

print(ans)

Solution (Python):
def inp():return map(int,input().split())
def ip():return int(input())

n,k=inp()
h=[]
for _ in range(n):
    h.append(ip())
h.sort()

ans=10**18
i=0
while i+k-1<n:
    ans=min(ans,h[i+k-1]-h[i])
    i+=1
print(ans)

Solution (Python):
n,k=map(int,input().split())
h=[int(input()) for _ in range(n)]
h.sort()
INF=2*10**9
ans=INF
for i in range(n-k+1):
	diff=h[i+k-1]-h[i]
	if ans>diff:
		ans=diff
		if ans==0:
			print(0)
			exit()
print(ans)

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

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

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

h = sorted(h)
ans = 1001001001
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=99999999999
for i in range(n-k+1):
  ans=min(ans,abs(h[i]-h[k+i-1]))
print(ans)

Solution (Python):
n, k = map(int, input().split())
apple = []
orange = []
for _ in range(n):
    apple.append(int(input()))
a = sorted(apple)
for i in range(n - k):
    b = - a[i] + a[i + k -1]
    c = a[-i-1] - a[-(i + k)]
    if b != 0 or c != 0:
        orange.append(min(b, c))
    else:
        orange.append(0)
        break
print(min(orange))

Solution (Python):
n,k = map(int,input().split())
x = [int(input()) for i in range(n)]
x.sort()
ans = float('inf')
for i in range(n-k+1):
    ans = min(ans,x[i+k-1]-x[i])
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])
    if ans==0:
        print(ans)
        exit()
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())
hs=sorted([int(input()) for _ in range(n)])
m=float('inf')
for l,h in zip(hs,hs[k-1:]):
    m=min(m,h-l)
print(m)

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

h = list(sorted([int(input()) for i in range(N)]))
h_dif = [h[i+K-1] - h[i] for i in range(N-K+1)]
print(min(h_dif))

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

print(min([b-s for s,b in zip(h, h[k-1:])]))

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

n, k, *h = map(int, read().split())

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

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

h = [int(input()) for x 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 _ in range(n):
    h.append(int(input()))

h.sort()
ans = float('inf')
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=sorted([int(input()) for i in range(n)])
d=10**9
for i in range(n-k+1):
    d=min(h[i+k-1]-h[i],d)
print(d)

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

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

Solution (Python):
N, K = map(int, input().split())
hs = []
for _ in range(N):
  hs.append(int(input()))
hs.sort()
  
start, end = 0, K-1
ans = float('inf')
while end < N:
  ans = min(ans, hs[end]-hs[start])
  start += 1
  end += 1
  
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):
def main():
    n, k = map(int, input().split()) 
    h = [int(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)

if __name__ == '__main__':
    main()

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

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

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

h.sort()
ans = float('inf')

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

print(ans)

Solution (Python):
n, k = map(int, input().split())
h = sorted([int(input()) for x in range(n)])
d = 10**9

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

    if d == 0:
        break

print(d)

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

Solution (Python):
N, K, *h = map(int, open(0).read().split())
h = sorted(list(h))
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 = sorted(list(h))
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 i in range(n)])
minv = float('inf')
for x, y in zip(H, H[k-1:]):
	minv = min(minv, y - x)
print(minv)

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

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

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**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 i in range(n)]
h = sorted(H)
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()

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

print(mini)

Solution (Python):
n, k = map(int, input().split())
tree = [int(input()) for _ in range(n)]
tree.sort()
ans = 10 ** 9 + 7

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

print(ans)

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

for i in range(N):
  a = int(input())
  arr.append(a)
  
arr.sort()
ans = 10**10

for i in range(N-K+1):
  a = arr[i]
  b = arr[i+K-1]
  c = b-a
  ans = min(ans,c)
  
print(ans)

Solution (Python):

import sys

import fractions
import copy
import bisect
import math
import numpy as np
import itertools
from itertools import combinations_with_replacement


from operator import itemgetter
from sys import stdin

from operator import mul
from functools import reduce
from collections import Counter





j=0
k=0
n=3
r=1
a=[0]






"1行1つの整数を入力を取得し、整数と取得する"




"12 21 332 とか入力する時に使う"
"1行に複数の整数の入力を取得し、整数として扱う"

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

for i in number_list:
    print(number_list[i-1])

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

'''
x=[]
y=[]
for i in range(N):
    x1, y1=[int(i) for i in input().split()]
    x.append(x1)
    y.append(y1)
print(x)
print(y)
"複数行に2数値を入力する形式 x座標とy座標を入力するイメージ"
'''

'''
mixlist=[]
for i in range(N):
    a,b=input().split()
    mixlist.append((int(a),b))
print(mixlist)
"複数行にintとstrを複合して入力するやつ，今回はリスト一つで処理している"
'''

'''



"正方行列にstr型の値を入力"
'''




'''
入力
1 2
4 5
7 8
出力結果
[[1, 2], [4, 5], [7, 8]]
'''
"列数に関して自由度の高いint型の値を入力するタイプの行列"


'''
table = [[int(i) for i in input().split()] for m in range(m)]

print(type(N))
print(N)
print(type(M))
print(M)

print(type(table))
print(table)
'''











'''
入力
1234
出力
1
[[1, 2, 3, 4]]
'''




'''
word_list= input().split(" ")
print(word_list[0])
"連続文字列の入力"
"qw er ty とかの入力に使う"
"入力すると空白で区切ったところでlistの番号が与えられる"
'''



'''
A, B, C=stdin.readline().rstrip().split()
print(A)
"リストではなく独立したstr型を入れるなら以下のやり方でOK"
'''




"aという変数に入っているものを大文字にして出力"

















'''
コンビネーションの組み合わせの中身を出力する形式
for i in itertools.combinations(brray, 2)
combinationsをpermutationsにすれば順列になる
今回なら(abc133B)
入力
1 2
5 5
-2 8
出力
[[1, 2], [5, 5], [-2, 8]]
もちろん一次元リストでも使えるし
何よりiもリストのように使えるので便利
'''


'''
def cmb(n,r):
    
    r= min(n-r,r)
    
    if r == 0: return 1
    over = reduce(mul, range(n, n-r, -1))
    
    
    

    under = reduce(mul, range(1, r+1))
    
    
    
    

    return over // under
    
    
    
    
a = cmb(n, r)

'''

'''
import itertools
from itertools import combinations_with_replacement

combinationについて
以下の違いを意識しよう
combinations() p, r 長さrのタプル列、ソートされた順で重複なし

combinations_with_replacement() p, r 長さrのタプル列、ソートされた順で重複あり


使用例 　　　　　　　　　　　                  出力　　
combinations('ABCD', 2)　　　　　　　　　　　　AB AC AD BC BD CD

combinations_with_replacement('ABCD', 2)　　 AA AB AC AD BB BC BD CC CD DD
'''



'''




for i in a.most_common(n):print(i)





 


'''









'''
bisect.bisect_left(a, x, lo=0, h=len(a))
引数
a: ソート済みリスト
x: 挿入したい値
lo: 探索範囲の下限
hi: 探索範囲の上限
(lo, hiはスライスと同様の指定方法)
bisect_leftはソートされたリストaに対して順序を保ったままxを挿入できる箇所を探索します。leftが示す通り、aにすでにxが存在している場合は、挿入箇所は既存のxよりも左側になります。また、lo, hiを指定することで探索範囲を絞り込むことも可能です。デフォルトはaの全体が探索対象です。
'''

'''
素数の判定
'''


def is_prime(n):
    if n == 1: return False

    for k in range(2, int(np.sqrt(n)) + 1):
        
        if n % k == 0:
            return False
            
    return True


'''
npのmaxとmaximumの違い
xs = np.array([1, -2, 3])
np.max(xs, 0)
この出力は3となります．[1, -2, 3]と0の4つの数字のうち，最も大きい値を出力します．

一方で，[max(1, 0), max(-2, 0), max(3, 0)]を出力したい時があります．
その時は，numpyのmaximum関数を用います．

xs = np.array([1, -2, 3])
np.maximum(xs, 0)  

'''

def factorial(x, mod=10**9+7):
    
    
    
    tmp = 1
    for i in range(1, x+1):
        tmp = (tmp * i) % mod
    return tmp
    '''
    Basically, when you do a**d % n, you actually have to calculate a**d, which could be quite large.
    But there are ways of computing a**d % n without having to compute a**d itself, and that is what pow does.
    '''











'''
ascii_letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
ascii_lowercase = 'abcdefghijklmnopqrstuvwxyz'
ascii_uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
digits = '0123456789'
hexdigits = '0123456789abcdefABCDEF'
letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv...\xaf\xb0...
lowercase = 'abcdefghijklmnopqrstuvwxyz'
octdigits = '01234567'
printable = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTU...
punctuation = '!"
uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
whitespace = '\t\n\x0b\x0c\r '
'''
'''
引数endに任意の文字列を指定すると、その文字列が改行の代わりに最後に挿入される。例えば連続するprint()の出力を改行なしで単純に連結したい場合は空文字列''を指定すればよい。

print('abc', end='---')
print('xyz')


print('abc', end='')
print('xyz')

source: print_basic.py
なお、デフォルトで改行されるのは引数endのデフォルト値が改行文字'\n'に設定されているから。
'''

def main():
    n=3
    steps=0
    state=0
    w=1
    j=0
    k=0
    
    
    
    n,k=map(int, input().split(" "))
    h=[]
    d=[10**9]*n
    for i in range(n):
        h.append(int(input()))
    h.sort()
    for j in range(n-k+1):
        
        d[j]=h[j+k-1]-h[j]
    print(min(d))
    
    
    
    
    
    
    


    
    
     

    l = ['aaa', 'bbb', 'ccc']
    x=l.index('aaa')
    
    
    
    

    
        
    
    

    
    
    
    
    DP = np.zeros(w+1, dtype=int)
    exdp=np.zeros((3,4)) 
    
    
    
        
        
    
    
        
        
    


    
    



    '''
    入力
    1234
    出力
    [1, 2, 3, 4]
    '''
    pin_l=["x" for i in range(10)]
    

    ls = ["a", "b", "c", "d", "e"]


    
    

    
    

    
    
    
    
    
    '''
    lsというリストの場合に、1つ飛びの値を取得したい場合には
    ls[::2]
    のようにします。こうすると、
    ["a", "c", "d"]と出力される
    '''

if __name__ == "__main__":
    main()

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

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,*H=map(int,open(0).read().split())
ans=10**9
H.sort()
i=0
while i+K-1<N:
    ans=min(ans,H[i+K-1]-H[i])
    i+=1
print(ans)

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

def merge_sort(arr):
    if len(arr) <= 1:
        return arr

    mid = len(arr) // 2
    
    left = arr[:mid]
    right = arr[mid:]

    
    left = merge_sort(left)
    right = merge_sort(right)

    
    return merge(left, right)


def merge(left, right):
    merged = []
    l_i, r_i = 0, 0

    
    while l_i < len(left) and r_i < len(right):
        
        if left[l_i] <= right[r_i]:
            merged.append(left[l_i])
            l_i += 1
        else:
            merged.append(right[r_i])
            r_i += 1

    
    if l_i < len(left):
        merged.extend(left[l_i:])
    if r_i < len(right):
        merged.extend(right[r_i:])
    return merged
h=merge_sort(h)
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 + 1
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()
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)

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

h = sorted(h)
m = 10**10
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 _ in range(n):
    tmp = int(input())
    h.append(tmp)

h.sort()

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

Solution (Python):

def inp():
    return(int(input()))



def inlist():
    return(list(map(int, input().split())))



def instr():
    s = input()
    return(list(s[:len(s)]))



def invr():
    return(map(int, input().split()))


N, K = invr()
A = []
for i in range(N):
    A.append(inp())
A = sorted(A)
res = A[N-1] - A[0]
for i in range(N-K+1):
    d = A[i+K-1] - A[i]
    res = min(d, res)
print(res)

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

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

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

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

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