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())
hl = [int(input()) for _ in range(n)]
hl.sort()
print(min([hl[k - 1 + i] - hl[i] for i in range(0, n - k + 1)]))

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

a=sorted(a)

ans=max(a)

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

print(ans)

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

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

ans = float("inf")
h = sorted(h)

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())
tree_list = []
dif = 999999999
for i in range(n):
    tree_list.append(int(input()))
tree_list.sort()
for i in range(n-k+1):
    if dif > (tree_list[i+k-1] - tree_list[i]):
        dif = tree_list[i+k-1] - tree_list[i]
print(dif)

Solution (Python):
N, K=(input().strip().split(' '))

p=[int(input()) for i in range(int(N))]
p.sort()
ans=1000000000

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

print(int(ans))

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

arr = []
for i in xrange(n):
    a = int(raw_input())
    arr.append(a)

arr.sort()


min = 1000000001


for i in xrange((n-k)+1):
    diff = arr[i+k-1] - arr[i]

    if diff < min:
        min = diff
        

print min

Solution (Python):
N,K=(int(x) for x in input().split())
i=1
p=[]
while i<=N:
    p.append(int(input()))
    i=i+1

p.sort()
high=[]
i=0
while i+K<=N:
        L=int(K+i)
        a = p[int(L-1)]
        b = p[i]
        deff = int(a - b)
        high.append(int(deff))
        i = i + 1

print(int(min(high)))

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

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

ans = 0xdeadbeef

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())
trees = sorted([int(input()) for _ in range(n)])
print(min([trees[i+k-1] - trees[i] for i in range(n-k+1)]))

Solution (Python):
Num = []
Num = list(map(int,input().split(" "))) 
lists = []
for i in range(0,Num[0]):
    lists.append(int(input()))
lists.sort()
lists.reverse()
dist = lists[0] - lists[0+Num[1]-1]
for j in range(1,Num[0] - Num[1] + 1):
    tmp = lists[j] - lists[j+Num[1]-1]
    if (dist > tmp):
        dist = tmp
print(dist)

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

for i in range(n):
    tree = int(input())
    T.append(tree)

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

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

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

print(min(li))

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

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

trees.sort()

min_height = trees[-1]

for i in range(N-K+1):
	tmp = trees[i+K-1] - trees[i]
	min_height = tmp if (tmp < min_height) else min_height

print(min_height)

Solution (Python):
from sys import stdin
import numpy as np
import sys

N, K = [int(x) for x in stdin.readline().rstrip().split()]
h = np.array(list(map(int, [stdin.readline().rstrip() for _ in range(N)])))

h.sort()
hmin = 2**30

for i in range(0, N-K+1):
    hdiff = h[i+K-1] - h[i]
    if hdiff < hmin:
        hmin = hdiff

print(hmin)

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 = list(map(int, input().strip().split(" ")))
hList = []
for n in range(N):
    hList.append(int(input().strip()))

hList.sort()
answer = 1000000000
for i in range(N - (K-1)):
    answer = min(answer, hList[i+K-1] - hList[i])
print(answer)

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

h.sort()
ans = 1e9 + 1
s = 0
while s + k - 1 <= n - 1:
  mm = h[s+k-1] - h[s]
  if mm < ans:
    ans = mm
  s += 1
print(int(ans))

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

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

h = sorted(h)

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

print(sub_min)

Solution (Python):
N, K = list(map(int, input().split()))
height = []
mindiv = 1000000000
for i in range(N):
    temp = int(input())
    height.append(temp)
height.sort()
for i in range(N - (K - 1)):
    if (height[i + K - 1] - height[i]) < mindiv:
        mindiv = height[i + K - 1] - height[i]

print(mindiv)

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

hN.sort()

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

Solution (Python):
n,k = map(int,input().split())
h = []
for _ in range(0,n):
    hh = int(input())
    h.append(hh)
h.sort()
ans = float("inf")
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 = sorted([int(input()) for _ in range(N)])

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

Solution (Python):
from sys import stdin
a = stdin.readline().split(" ")
num=int(a[0])
find=int(a[1])
j=1
w2=[]
while j<=num:
    z = stdin.readline().rstrip('\n')
    w2.append(int(z))
    j+=1
w2.sort()
minnum=w2[find-1]-w2[0]
for i in range(len(w2)-find+1):
    minnum=min(minnum,w2[i+find-1]-w2[i])
print(minnum)

Solution (Python):



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

    h.sort()

    min_diff = 99999999999
    for i in range(n - k + 1):
        tmp = h[i + k - 1] - h[i]
        if tmp < min_diff:
            min_diff = tmp

    print(min_diff)


if __name__ == '__main__':
    main()

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

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

for j in range(n-k+1):
  sa=h[j+k-1] - h[j]
  if min > sa:
    min=sa

print(min)

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

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

Solution (Python):
n,k = map(int,input().split())
h = []
for i in range(n):
    h.append(int(input()))
h.sort()
min_diff = h[k] - h[0]
for i in range(n-k +1):
    h_min = h[i]
    h_max = h[i + k -1]
    if min_diff > h_max - h_min:
        min_diff = h_max - h_min
print(min_diff)

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 = list(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 = [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):


import sys


input = sys.stdin.readline
INF = float('inf')
MOD = 10 ** 9 + 7


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


N, K = inpl()
H = [int(input()) for _ in range(N)]

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

print(ans)

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

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

Solution (Python):

total_cnt, select_cnt = map(int, input().split())

tree_list = []

for i in range(total_cnt):
    tree_high = int(input())
    tree_list.append(tree_high)

tree_sorted_list = sorted(tree_list)

diff_list = []

for j in range(total_cnt-select_cnt+1):
    diff = tree_sorted_list[j+select_cnt-1] - tree_sorted_list[j]
    diff_list.append(diff)
    
print(min(diff_list))

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

    h.sort()

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

    print(min(ans))

if __name__ == "__main__":
    main()

Solution (Python):
n,k = map(int,input().split())
a=[]
c=[]
for i in range(n):
    b=int(input())
    a.append(b)
a.sort()
for u in range(n-k+1):
    d=a[u+k-1]-a[u]
    c.append(d)
print(min(c))

Solution (Python):
def main():
    n, k = map(lambda n: int(n), input().split(' '))
    prices = []
    for _ in range(n):
        p = int(input())
        prices.append(p)
    prices.sort()

    min_diff = prices[-1] - prices[0]
    for i in range(n-k+1):
        diff = prices[i+k-1] - prices[i]
        if diff < min_diff: 
            min_diff = 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()
ans = 1<<31
for i in range(N-K+1):
  ans = min(ans, h[i+K-1]-h[i])
print(ans)

Solution (Python):


from collections import deque
import itertools as it
import sys
import math

sys.setrecursionlimit(1000000)

INF = 10 ** 18
MOD = 10 ** 9 + 7

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())
listT = []
index=0
while index < n:
    h = int(input())
    listT.append(h)
    index+=1
listT.sort()
if n == k:
    print(listT[len(listT)-1]-listT[0])
else:
    mini=1000000000
    index =0
    while index < n-k+1:
        if listT[index+k-1]-listT[index] < mini:
            mini = listT[index+k-1]-listT[index]
        index+=1
    print(mini)

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

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

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

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

print(min_diff)

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

Solution (Python):
total, window = map(int, input().split())

trees_height = sorted([int(input()) for _ in range(total)])
diff = []

for i in range(total-window+1):
    diff.append(trees_height[i+window-1] - trees_height[i])

print(min(diff))

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

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

a.sort()
b = a[k-1] - a[0]
for i in range(n-k+1):
    if a[i+k-1] - a[i] < b:
        b = a[i+k-1] - a[i]
print(b)

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

tree_list = []
sort_list = []
for x in range(N):
    tree_list.append(int(input()))

sort_list = sorted(tree_list)

min_heigt = sort_list[M-1] - sort_list[0]

for x in range(N-M+1):
    nagasa =sort_list[x+M-1] - sort_list[x] 
    if min_heigt > nagasa:
        min_heigt = nagasa

print(min_heigt)

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

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

xs = sorted(h)
result = min(xs[i + K - 1] - xs[i] for i in range(N - K + 1))

print(result)

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())
c = [int(input()) for i in range(N)]
c.sort()
output = c[K-1] - c[0]
for i in range(N-K):
    if output > c[K+i] - c[i+1]:output = c[K+i] - c[i+1]
print(output)

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

xs = sorted(h)
result = xs[-1]
for i in range(N - K + 1):
    r = xs[i + K - 1] - xs[i]
    result = min(result, r)

print(result)

Solution (Python):
N, K = map(int, input().split())
hi = list(map(lambda x: int(input()), range(N)))

hi.sort()
diff = map( lambda i: hi[i+K-1]-hi[i], range(N-K+1) )
min_diff = min(diff)

print(min_diff)

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

hi = []
i = 0
while i + (K - 1) < N:
    hi.append(int(h[i + (K - 1)] - h[i]))
    i += 1

print(min(hi))

Solution (Python):
n, k = map(int,input().split())
h = [int(input()) for i in range(n)]
m = max(h)-min(h)
h.sort()

for i in range(len(h)-k+1):
	if(h[i+k-1]-h[i] < m):
		m = h[i+k-1]-h[i]

print(m)

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

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

Solution (Python):
def solve(string):
    n, k = map(int, string.split("\n")[0].split(" "))
    ins = list(map(int, string.split("\n")[1:]))
    ins.sort()
    return str(min([ins[i + k - 1] - ins[i] for i in range(len(ins) - k + 1)]))


if __name__ == '__main__':
    n, k = map(int, input().split(" "))
    ins = [input() for _ in range(n)]
    print(solve("{} {}\n{}".format(n, k, "\n".join(ins))))

Solution (Python):



import sys


sys.setrecursionlimit(10 ** 6)

read_list_int = lambda: list(map(int, sys.stdin.readline().strip().split(' ')))
read_single_int = lambda: int(sys.stdin.readline().strip())


def solution(n: int, k: int, hs: list):
    sorted_hs = sorted(hs)
    min_diff = 99999999999999999
    for start in range(0, n-k+1):
        diff = sorted_hs[start+k-1] - sorted_hs[start]
        if min_diff > diff:
            min_diff = diff
    return min_diff


def main():
    n, k = read_list_int()
    hs = []
    for _ in range(n):
        hs.append(read_single_int())
    print(solution(n, k, hs))


if __name__ == '__main__':
    main()

Solution (Python):
n, k = map(int, input().split())
h = []
ans = 1000000001
for i in range(n):
    h.append(int(input()))
h.sort()
for j in range(n-(k-1)):
    if ans == 0:
        break
    elif ans > h[j+k-1]-h[j]:
        ans = h[j+k-1]-h[j]
print(ans)

Solution (Python):
N,K = map(int,input().split())
h = list(int(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)

Solution (Python):
n, k = map(int,input().split())
lis = []
for _ in range(n):
  lis.append(int(input()))
ans = 100000000000000
lis.sort()
for i in range(n-k+1):
  s = lis[i+k-1]-lis[i]
  if s < ans:
    ans = s
print(ans)

Solution (Python):
N,K=map(int,input().split())
height = []
for i in range(N):
    height.append(int(input()))
height_s = sorted(height)
diff = []
i = 0
while True:
    if i + K == len(height_s) + 1:
        break
    diff.append(height_s[i + K -1]-height_s[i])
    i += 1
s = sorted(diff)

print(s[0])

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

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

sorted_li = sorted(h_list)
min_a = 10**10
for i in range(N-K+1):
    a = sorted_li[i+K-1]-sorted_li[i]
    min_a = [min_a,a][a < min_a]
print(min_a)

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

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

Solution (Python):
import sys

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

mn = sys.maxsize
for i in range(n - k + 1):
    cnt = h[i + k - 1] - h[i]
    mn = min(mn, cnt)

print(mn)

Solution (Python):
N, K = (int(x) for x in input().split())

trees = list()

for i in range(N):
  trees.append(int(input()))
  
trees = sorted(trees)
diff_list = []

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

Solution (Python):


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

l = [int(input()) for j in range(N)]
l = sorted(l)
dif = 1000000000
for a in range(N-K+1):
    d = l[a+K-1] - l[a]
    
    
    if dif >= d:
        dif = d

print(dif)

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

cnt = 0
L = []
for i in range(N - K + 1):
    L.append(A[i + K - 1] - A[i])
    
print(min(L))

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

b = sorted(a, reverse=True)
c = min([b[i] - b[i+K-1] for i in range(N-K+1)])
print(c)

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

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

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

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

H = sorted(H)
mi = max(H)
for i in range(N-K+1):
    mi = min(abs(H[i]-H[i+K-1]), mi)
    
print(mi)

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(ans, h[k+i-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()

sa = []
for i in range(0, N + 1 - K):
    h1 = h[i]
    h2 = h[i + K - 1]
    sa.append(h2 - h1)

print('%d' % (min(sa)))

Solution (Python):

N, K = map(int, input().split())
h = [None] * 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 = [int(i) for i in input().split()]
h = [0 for _ in range(n)]
for i in range(len(h)):
    h[i] = int(input())
h = sorted(h)

_min = h[k-1] - h[0]
for i in range(k, len(h)):
    if h[i] - h[i - k + 1] <= _min:
        _min = h[i] - h[i - k + 1]
print(_min)

Solution (Python):
N, K = map(int, input().split())
h =[int(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)

Solution (Python):
NK = input()
NK = NK.split(" ")
N = int(NK[0])
K = int(NK[1])


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):
N,K = map(int,raw_input().split())
a = []
for i in range(N):
    a.append(int(raw_input()))
a.sort()
b = []
for i in range(N-K+1):
    b.append(a[i+K-1]-a[i])
print(min(b))

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

ans = 1e10
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 i in range(N)]
trees.sort()
ans = 10 ** 10
for i in range(N - K + 1):
    ans = min(ans, abs(trees[i]-trees[i+K-1]))
print(ans)

Solution (Python):
N, K, *H = map(int, open(0).read().split())
H.sort()
ans = 10**18
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())
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):
import sys
n=input()
list = n.split(" ")
n=int(list[0])
k=int(list[1])

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

def merge_sort(alist):
  if len(alist) <= 1:
    return alist
  
  mid = int(len(alist)/2)
  left = alist[:mid]
  right = alist[mid:]
  
  left = merge_sort(left)
  right = merge_sort(right)
  
  return merge(left, right)

def merge(left,right):
  sorted_list = []
  left_index = 0
  right_index = 0
  
  while left_index < len(left) and right_index < len(right):
    if left[left_index] >= right[right_index]:
      sorted_list.append(left[left_index])
      left_index += 1
    else:
      sorted_list.append(right[right_index])
      right_index += 1
      
  if left:
    sorted_list.extend(left[left_index:])
    
  if right:
    sorted_list.extend(right[right_index:])
    
    return sorted_list

trees = merge_sort(trees)

ans=sys.maxsize
flag=0
temp = []

for i in range(len(trees)-k+1):
  diff = trees[i]-trees[i+k-1]
  if(diff==0):
    ans=0
    break
  if(ans>diff):
    ans=diff
  temp.clear()

print(ans)

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

ans = float("inf")
for i in range(n-k+1):
    tmp = a[i+k-1] - a[i]
    ans = min(ans, tmp)
print(ans)

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

ans=10000000000

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

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

Solution (Python):
N, K = map(int,input().split())
trees = []
for i in range (N):
    temp = int(input())
    trees.append(temp)
trees.sort()
ans = 10**9
for i in range (N-K+1):
    low = trees[i]
    high = trees[i+K-1]
    diff = high-low
    if diff < ans:
        ans = diff
print(ans)

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

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

highs = [int(input()) for i in range(n)]
highs = sorted(highs)

minimum = min([highs[i + k - 1] - highs[i] for i in range(n - k + 1)])

print(minimum)

Solution (Python):

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

h = []
sums = [0]
for i in range(N):
    h.append(int(input()))

h = sorted(h)
diff = []
bef = 0
for e,i in enumerate(h):
    if e == 0:
        bef = i
    else:
        diff.append(i - bef)
        bef = i
        

diffin = 0
for n in diff:
    sums.append(sums[diffin] + n  )
    diffin += 1


mins = 10e9
leng = len(sums)
for i in range(leng-K+1):
    
    mins = min(mins, sums[i+K-1] - sums[i])
print(mins)

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

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

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

c = [a[i + k - 1] - a[i] for i in range(n - k + 1)]

print(min(c))

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

ans = []
 
for i in range(n - k + 1):
    b = a[i + k - 1] - a[i]
    ans.append(b)

print(min(ans))

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

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

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

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

Solution (Python):
N,K=list(map(int,input().split()))
h=[int(input()) for i in range(N)]
 
h.sort()
 
m="a"
for i in range(N-K+1):
	if m=="a" or m>h[i+K-1]-h[i]: m=h[i+K-1]-h[i]
 
print(m)

Solution (Python):
N,K = list(map(int,input().strip().split()))
ans = int(10e10)

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

hlist.sort()

for i in range(N-K+1):
    low = hlist[i]
    high = hlist[i+K-1]
    ans = min(ans,(high-low))

print(ans)

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

h.sort()

m="a"
for i in range(N-K+1):
	if m=="a" or m>h[i+K-1]-h[i]: m=h[i+K-1]-h[i]

print(m)

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

Solution (Python):
s = input().split(" ")
N = int(s[0])
K = int(s[1])

h = []
for i in range(N) : 
    s = input()
    h.append(int(s))
    
h = sorted(h)
minDiff = h[len(h) - 1] - h[0]
for i in range(N) : 
    try : 
        diff = h[i + K - 1] - h[i]
        minDiff = min(minDiff , diff)
        
    except : 
        if i == 0 : minDiff = 0
        continue
print(minDiff)

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

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

Solution (Python):
N,K = list(map(int, input().split()))
h = [int(input()) for i in range(N)]
candidate = None
h.sort()
for i in range(N-K+1):
    tmp = h[i+K-1] - h[i]
    if candidate == None or tmp < candidate:
        candidate = tmp
print(candidate)

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

t.sort()
m = 10 ** 9
for i in range(N-K+1):
    m = min(t[i+K-1] - t[i], m)

print(int(m))

Solution (Python):
def main():
  N, K = map(int, input().split(' '))
  
  
  
  
  trees = [int(input()) for _ in range(N)]
  trees.sort()
  trees = [n - trees[0] for n in trees]
  
  
  
  
  
  
  
  
  MIN = min([trees[n+K-1] - trees[n] for n in range(N-K+1)])
  print(MIN)

main()

Solution (Python):
from itertools import combinations

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

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

hs.sort()

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

print(R)

Solution (Python):
n, k = map(int, input().split())
h = [int(input()) for _ 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 = 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 = map(int, input().split())
H = [int(input()) for i in range(N)]
H.sort()
ans = float('inf')
now = sum(H[:K])
for i in range(N-K+1):
    ans = min(ans, abs(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**13
for i in range(N-K+1):
    ans=min(H[i+K-1]-H[i],ans)
    
print(ans)

Solution (Python):
n,k = map(int, input().split())
d = sorted([int(input()) for _ in range(n)],reverse=True)
ret = 9999999999

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

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

ans=float('inf')
for i in range(n-k+1):
    a=l[i]-l[i+k-1]
    ans=min(ans,a)
print(ans)

Solution (Python):
import collections

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

h.sort(reverse=True)








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

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

Solution (Python):
from itertools import starmap
from operator import sub

n, k = map(int, input().split())
hhh = sorted(int(input()) for _ in range(n))
print(min(starmap(sub, zip(hhh[k - 1:], hhh))))