N,K,*h=list(map(int,open(0).read().split()))

h.sort()

if K == N:

    print((h[-1] - h[0]))

else:

    dif_h=[]

    for i in range(N-1):

        dif_h.append(h[i+1] - h[i])

    _min = base = sum(dif_h[:K-1])

    for i in range(K-1,N-1):

        base += dif_h[i]

        base -= dif_h[i-K+1]

        _min = min(base,_min)

    print(_min)    

        

        

    
N,K,*h=list(map(int,open(0).read().split()))

h.sort()

_min = 10**9

for i in range(N+1-K):

    _min = min(_min, h[i+K-1] - h[i])

print(_min)
N, K, *h = list(map(int, open(0).read().split()))
h.sort()
if K == N:
    print((h[-1] - h[0]))
else:
    dif_h = []
    for i in range(N - 1):
        dif_h.append(h[i + 1] - h[i])
    _min = base = sum(dif_h[: K - 1])
    for i in range(K - 1, N - 1):
        base += dif_h[i]
        base -= dif_h[i - K + 1]
        _min = min(base, _min)
    print(_min)

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

-if K == N: -    print((h[-1] - h[0])) -else: -    dif_h = [] -    for i in range(N - 1): -        dif_h.append(h[i + 1] - h[i]) -    _min = base = sum(dif_h[: K - 1]) -    for i in range(K - 1, N - 1): -        base += dif_h[i] -        base -= dif_h[i - K + 1] -        _min = min(base, _min) -    print(_min) +_min = 10**9 +for i in range(N + 1 - K): +    _min = min(_min, h[i + K - 1] - h[i]) +print(_min)