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 = [int(str) for str in input().strip().split()]
hs = [int(input().strip()) for _ in range(N)]

hs.sort()

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

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

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

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

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

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

ans = 10**9
for i,j in zip(h,h[k-1:]):
  ans = min(ans,j-i)
print(ans)

Solution (Python):

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

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

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, = sorted([int(input()) for _ in range(n)])

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

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

tr.sort()
INF = float('inf')
ans = INF
for i in range(N-K+1):
    dif = tr[i+K-1] -tr[i]
    if dif < ans:
        ans = dif

print(ans)

Solution (Python):
import numpy as np
N, K, *H = map(int, open(0).read().split())
H.sort()
H = np.array(H)
print(min(H[(K-1):]-H[:-(K-1)]))

Solution (Python):
import sys
input = sys.stdin.readline

N, K = [int(x) for x in input().strip().split()]
H = sorted([int(input()) for _ in range(N)])
ans = 10 ** 9
for n in range(N - (K - 1)):
    ans = min(ans, H[n+K-1] - H[n])

print(ans)

Solution (Python):
import sys

input = sys.stdin.readline

def main():
    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)
    
if __name__ == '__main__':
    main()

Solution (Python):
N, K = map(int, input().split())
h = sorted(list(int(input()) for _ in range(N)))
fin = h[K-1] - h[0]
for i in range(1, N-K+1):
  t = h[i+K-1] - h[i]
  if fin > t :
    fin = t
print(fin)

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

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

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

b=[]
li.sort()
for j in range(n+1-k):
  b.append(li[j+k-1]-li[j])
  
print(min(b))

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

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

print(res)

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

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

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

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

sa=[]

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

print(min(sa))

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

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

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

h.sort()
S=10000000000000
for i in range(n-k+1):
  if S>h[i+k-1]-h[i]:
    S=h[i+k-1]-h[i]
print(S)

Solution (Python):

import sys, math
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
sys.setrecursionlimit(10**8)
inf = float('inf')
ans=inf
count=0

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

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

Solution (Python):
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools
sys.setrecursionlimit(10**8)
mod = 10**9+7
mod2 = 998244353
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))

n,k = inpl()
a = inpln(n)
a.sort()
res = INF
for i in range(n-k+1):
    res = min(res, a[i+k-1] - a[i])
print(res)

Solution (Python):
N, K = map(int, input().split())
H = sorted([int(input()) for i in range(N)], reverse=True)
ans = []
for i in range(N):
    try:
        ans.append(H[i] - H[i+K-1])
    except:
        pass
print(min(ans))

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

lis = []

for _ in range(n):
    lis.append(int(input()))

lis.sort()

min = 10**10
for i in range(len(lis)-k+1):
    dist = lis[i+k-1] - lis[i]
    if min > dist: min = dist
print(min)

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

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

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

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

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())
L = []
for val in range(n):
    val = int(input())
    L.append(val)

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

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

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

Solution (Python):

import sys










def main():
    n, k, *h = (int(x) for x in sys.stdin.read().split())
    h.sort()
    diffs = []
    for i in range(n - k + 1):
        diffs.append(h[i+k-1] - h[i])
    print(min(diffs))

if __name__ == "__main__":
    main()

Solution (Python):

import sys










def main():
    n, k, *h = (int(x) for x in sys.stdin.read().split())
    h.sort()
    min_diff = h[-1] - h[0] 
    for i in range(n - k + 1):
        diff = h[i+k-1] - h[i]
        min_diff = min(min_diff, diff)
    print(min_diff)

if __name__ == "__main__":
    main()

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

Solution (Python):
N,K = map(int,input().split())
H = []
for n in range(N):
    H.append(int(input()))
H.sort()
ans = 10000000000
for n in range(N-K+1):
    ans = min(ans,H[n+K-1]-H[n])
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_dif = 10**9 + 1
for shift in range(n - k + 1):
  dif = H[shift + k - 1] - H[shift]
  min_dif = min(min_dif, dif)
print(min_dif)

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

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())
l = [int(input()) for _ in range(n)]
l.sort()
ans = 10**9+7
for i in range(n-k+1):
    if ans > l[i+k-1]-l[i]:
        ans = l[i+k-1]-l[i]
print(ans)

Solution (Python):
n, k = map(int, input().split())
a = sorted([int(input()) for _ in range(n)])
left = 0
right = k - 1
ret = float('inf')
while right < n:
    ret = min(ret, a[right] - a[left])
    left += 1
    right += 1
print(ret)

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

Solution (Python):
import os, sys, re, math

N,K = list(map(int,input().split(' ')))
trees = []

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

trees.sort(reverse=True)
ret = trees[0]

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

print(ret)

Solution (Python):
n,k=map(int,input().split())
h=[]
for i in range(n):
    h.append(int(input()))
h2=sorted(h)
start=0
goal=k-1
ans=10000000000
while goal<=n-1:
    ans=min(ans,h2[goal]-h2[start])
    start+=1
    goal+=1
print(ans)

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

H = [0 for _ in range(N)]
for i in range(N):
    H[i] = int(input())

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

print(ans)

Solution (Python):
n, k = [int(i) for i in input().split()]
stock = []
for i in range(n):
	stock.append(int(input()))
stock = sorted(stock)
ret = 1000000000
for i in range(n - k + 1):
	length = stock[i+k-1] - stock[i]
	ret = min(ret, length)
print (ret)

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):
from sys import stdin
from itertools import groupby

n,k = [int(x) for x in stdin.readline().rstrip().split()]
li = [int(stdin.readline().rstrip()) for _ in range(n)]
li.sort()

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

print(mi)

Solution (Python):
import os, sys, re, math

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

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

print (result)

Solution (Python):
n, k = list(map(int, input().split(' ')))
ts = []
for i in range(n):
  ts.append(int(input()))
ts = sorted(ts)  
ans = 1e10
for i in range(n-k+1):  
  ans = min(ans, ts[i + k - 1] - ts[i])
print(ans)

Solution (Python):

import sys


def solve(N: int, K: int, h: "List[int]"):
    h_sort = sorted(h, reverse=True)
    ans = h_sort[0] - h_sort[-1]
    for i in range(0, N-K+1):
        h_top = h_sort[i]
        h_bottom = h_sort[i+K-1]
        ans = min(ans, h_top - h_bottom)
    print(int(ans))



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 = 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())
hs = []
for n in range(N):
    h = int(input())
    hs.append(h)
hs.sort()
ret = hs[-1] - hs[0] 
for i in range(0, N - K + 1):
    hmin = hs[i]
    hmax = hs[i + K - 1]
    ret = min(ret, hmax - hmin)

print(ret)

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

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

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

h.sort(reverse=True)

dh_max = h[0]

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

print(dh_max)

Solution (Python):
N, K = [int(x) for x in input().split()]
H = sorted([int(input()) for _ in range(N)])
minh = H[0]
H = [h-minh for h in H]
l = K-1
ans = H[-1]
for i in range(N-l):
    ans = min(ans, H[i+l]-H[i])
print(ans)

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



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

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

X = []
for n in range(N-K+1):
    x = H[n] - H[n+K-1]
    X.append(x)

print(min(X))

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

h.sort()

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

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

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

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

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

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

L = []

for i in range(0,N - K + 1):
  diff = H[i + K - 1] - H[i]
  L.append(diff)

    
print(min(L))

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

Solution (Python):
def c_christmas_eve():
    N, K = [int(i) for i in input().split()]
    H = sorted([int(input()) for _ in range(N)])
    
    return min([H[j + K - 1] - H[j] for j in range(N - K + 1)])

print(c_christmas_eve())

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):
nk = input().rstrip().split(" ")
n = int(nk[0])
k = int(nk[1])
h = []
for i in range(n):
    h.append(int(input()))
h.sort()
res = h[-1] - h[0]
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=[]
for _ in range(N):
  h.append(int(input()))
h.sort()
m=[]
for l in range(N-K+1):
  m.append(h[l+K-1]-h[l])
print(min(m))

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

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

H.sort()

reslist = []

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

        if j >= N:
                break

        df = H[j] - H[i]
        reslist.append(df)

res =min(reslist)
print(res)

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

result = float("Inf")

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

print(result)

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

minh = 10000000000
for i in range(n-(k-1)):
	if h[i+k-1] - h[i] < minh:
		minh = h[i+k-1] - h[i]

print (minh)

Solution (Python):
if __name__ == '__main__':
    N, K = map(int, input().split())
    height = sorted([int(input()) for n in range(N)])
    s =  [ height[idx + K - 1] - h for idx, h in enumerate(height)
            if idx + K - 1 < len(height)
        ]
    print(min(s))

Solution (Python):
from functools import reduce
n, k = map(int, input().split())

heights = []
for _ in range(n):
	heights.append(int(input()))

heights.sort()

diff = [0]*n

for i in range(k-1,len(heights)):
	diff[i] = heights[i]-heights[i-k+1]
	
print(reduce(min, diff[k-1:], 1000000002))

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

difBanig = []
for bot in range(n-k+1):
  difBanig.append(banig[bot+k-1] - banig[bot])

print(min(difBanig))

Solution (Python):
nk = input()
nk = nk.split()
n = int(nk[0])
k = int(nk[1])
trees = []
for i in range(n):
  trees.append(int(input()))
trees.sort()
minimum = 1000000000
i = 0
while i<=n-k:
  if trees[i+k-1]-trees[i]<minimum:
    minimum = trees[i+k-1]-trees[i]
  i+=1
print(minimum)

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

Solution (Python):
import numpy as np

N, K = map(int, input().split())
H = np.array(sorted([int(input()) for _ in range(N)]))

diff = H[K-1:] - H[:-K+1]
print(diff.min())

Solution (Python):
n,k = map(int,input().split())
h = sorted([int(input()) for _ in range(n)])
ans = h[k-1] - h[0]
for i in range(n-k+1):
    ans = min(ans,abs(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()
ans=max(h)-min(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=[int(input()) for i 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 = sorted([int(input()) for i in range(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=[]
for i in range(N):
    H.append(int(input()))
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 = map(int,input().split())
h=[0 for _ in range(n)]
for i in range(n):
    h[i]=int(input())
h.sort()
m=sum(h)
for i in range(n-k+1):
    m=min(h[i+k-1]-h[i],m)
print(m)

Solution (Python):
from collections import Counter
N , K = map(int,input().split())
h = list(int(input()) for _ in range(N))
h.sort()
min = 10 ** 10
for i in range(len(h) - K + 1):
    tmp_min = h[i + K - 1] - h[i]
    if min > tmp_min:
        min = tmp_min
print(min)

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

Solution (Python):
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.buffer.readline().rstrip()
def resolve():
    n,k=map(int,input().split())
    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)
resolve()

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

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

Solution (Python):
n, k, *h = map(int, open(0).read().split())
h.sort()
ans = 10e9+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 _ in range(n)]
h.sort()
between = [h[i+1]-h[i] for i in range(n-1)]
answer = 10**9
for i in range(1,n-1):
  between[i] += between[i-1]
between.insert(0,0)

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

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

Solution (Python):
n, k = map(int, input().split())
h = sorted([int(input()) for _ in range(n)], reverse=True)
ans = h[0]
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):
def solve(n, k, h):
    h.sort()
    diff = [h[i]-h[i-k+1] for i in range(k-1, n)]
    return min(diff)


if __name__ == "__main__":
    n, k = map(int, input().split())
    h=[]
    for _ in range(n):
        h.append(int(input()))
    print(solve(n, k, h))

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

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

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy,copy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

def main():
    n, k = MAP()
    h = [INT() for _ in range(n)]

    h.sort()

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

if  __name__=='__main__':
    main()

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

N, K = MAP()
h = [INT() 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 = map(int, input().split())
H = []
for i in range(N):
    h = int(input())
    H.append(h)
H = sorted(H)
mi = 10**9
for i in range(N-K+1):
    _ = H[i+(K-1)] - H[i]
    if mi > _:
        mi = _
print(mi)

Solution (Python):
N,K=map(int,input().split())
l=[]
ans=10**9

for _ in range(N):
    l.append(int(input()))
    
l.sort()
temp=l[0:K]
ans=min(ans,temp[-1]-temp[0])

for i in range(N-K):
    temp.pop(0)
    temp.append(l[K+i])
    ans=min(ans,temp[-1]-temp[0])

print(ans)

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

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

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


h.sort()
h_diff = []

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

print(min(h_diff))

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

Solution (Python):
n,k = map(int,input().split())
h = sorted([int(input()) for i in range(n)])
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 _ 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()
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 = [int(input()) for _ 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):
def slove():
    import sys
    input = sys.stdin.readline
    n, k = list(map(int, input().rstrip('\n').split()))
    h = [int(input().rstrip('\n')) for _ in range(n)]
    h.sort()
    ma = 10 ** 10
    for i in range(k-1, n):
        ma = min(ma, abs(h[i] - h[i-k+1]))
    print(ma)


if __name__ == '__main__':
    slove()

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

sorted_H = sorted(H, reverse=True)
diff = sorted_H[0] - sorted_H[-1]

for i in range(n):
    try:
        diff_in = sorted_H[i] - sorted_H[i+k-1]
    except IndexError:
        break
    if diff_in < diff:
        diff = diff_in

print(diff)

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

m=10**9
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=[int(input()) for _ in range(n)]
h.sort()

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

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())
trees = []
for _ in range(n):
    trees.append(int(input()))
trees.sort()
ans = 10**9
for i in range(n):
    if i+k -1 ==n:
        break
    ans = min(ans,trees[i+k-1] -trees[i])
print(ans)

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

Solution (Python):
import sys
from bisect import *
from collections import *
from copy import deepcopy
from datetime import *
from heapq import *
from itertools import *

from operator import *
from pprint import *

sys.setrecursionlimit(10**8)

input = sys.stdin.readline

def main():
    """ main """
    N,K = map(int, input().split())
    HS = [int(input()) for _ in range(N)]
    HS.sort()

    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):
n,k=map(int,input().split())
L=sorted([int(input()) for i in range(n)])
r=10**9
for i in range(n-k+1):
	s=L[i+k-1]-L[i]
	r=min(r,s)
print(r)

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

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

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

answer = min(h2 - h1 for h1, h2 in zip(h, h[k - 1:]))
print(answer)

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

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

print(ans)

Solution (Python):
in1 = [int(x) for x in input().strip().split()]

N = in1[0]
K = in1[1]

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

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

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

ans_list = []
for index, tree in enumerate(trees):
    if  k-1 <= index:
        ans_list.append(tree-trees[index-(k-1)])
print(min(ans_list))

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)