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 = list(map(int, input().split()))

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

h = sorted(h)

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

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

ans = 100000000000000000000000000000
for i in range(N-K+1):
	if(ans>H[i+K-1]-H[i]):
		ans = int(H[i+K-1]-H[i])

print(ans)

Solution (Python):
n_k = input()
n_k_list =n_k.split(' ')

N = int(n_k_list[0])
K = int(n_k_list[1])

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

sorted_h_list = sorted(h_list)

min_dif = 1000000000
for i in range(N-K+1):
    start = i
    end = K - 1 + i

    end_v = sorted_h_list[end]
    start_v = sorted_h_list[start]
    dif = end_v - start_v

    if min_dif > dif:
        min_dif = dif

print(int(min_dif))

Solution (Python):
import sys, copy, math, collections, itertools

n, k = (int(i) for i in input().split())
hs = [int(input()) for i in range(n)]
hs.sort()
i=0
m=-1
l=len(hs)
while True:
    if l < (i+k):
        break
    
    cnd=hs[i+k-1] - hs[i]
    if m==-1:
        m=cnd
    else:
        if cnd < m:
            m=cnd
    i+=1
print(int(m))

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

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

a = [int(input()) for _ in range(n)]
a.sort()
b = 0
ans = []

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

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 = [int(a) for a in input().split()]

h = []

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

sortedh = sorted(h)

min_ = 1e9

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

print(min_)

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

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

print(min)

Solution (Python):
import numpy as np
(N,K) = map(int,input().split())
h = np.array([int(input()) for _ in range(N)])
h.sort()
d = np.array([h[i+K-1]-h[i] for i in range(N-K+1)])
print(d.min())

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

for i in range(n):
    h = int(input())
    hs.append(h)

hs.sort()

for i in range(n - k + 1):
    diff.append(hs[i + k - 1] - hs[i])
    
print(min(diff))

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

Solution (Python):
n,k=[int(i) for i in input().split()]
h=[int(input()) for i in range(n)]
h.sort()
ans=10**9
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(reverse=True)
mini = 10 ** 9

for i in range(N-K+1):
  a = h[i] - h[i+K-1]
  if a < mini:
    mini = a
    
print(mini)

Solution (Python):


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

an.sort()

lmin = an[-1] - an[0]

for i in range(N-K+1):
    bn = an[i+K-1] - an[i]
    if lmin >= bn:
        lmin = bn
    if lmin == 0:
        break

print(lmin)

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

hs.sort()


min_diff = hs[k - 1] - hs[0]
for i in range(n - k + 1):
    diff = hs[i + k - 1] - hs[i]
    if diff == 0:
        min_diff = 0
        break

    if diff < min_diff:
        min_diff = diff

print(min_diff)

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







h.sort()




picked = h[:k]



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



dif = ans

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


print(ans)

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



sorted_h = sorted(h_list)

best_score = 1000000000000
for idx, h in enumerate(sorted_h[:-K+1]):
    highest_idx = idx + K - 1
    score = sorted_h[highest_idx] - h
    
    if score < best_score:
        best_score = score
print(best_score)

Solution (Python):


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

Solution (Python):

n, k = input().split()
n = int(n)
k = int(k)

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

a.sort()



height_distance = []
for i, h in enumerate(a):
	if i <= n-k:
		
		
		height_distance.append(a[i+k-1]-a[i])
		

print(min(height_distance))

Solution (Python):
from sys import stdin

N, K = [int(x) for x in stdin.readline().rstrip().split()]

ls = []

for i in range(N):
    h = int(stdin.readline().rstrip())
    ls.append(h)

ls = sorted(ls)

minval = ls[K-1] - ls[0]

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

print(minval)

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

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

Solution (Python):
def main():
    N, k = (int(i) for i in input().split())
    h = []
    for _ in range(N):
        h.append(int(input()))
    h.sort()
    hdiffmin= 10**10
    for i in range(N - k + 1):
        diff = abs(h[i] - h[i+k-1])
        if diff < hdiffmin:
            hdiffmin = diff
    print(hdiffmin)


main()

Solution (Python):
from math import fabs


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

    h.sort()
    min = fabs(h[0] - h[N-1])
    for i in range(N - K + 1):
        min_temp = fabs(h[i] - h[i + K - 1])
        if min > min_temp:
            min = min_temp
    print(int(min))


if __name__ == "__main__":
    main()

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

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

for i in range(N):
    list.append(int(input()))
list.sort()


DIF = []
for i in range(N - K + 1):
    DIF.append(abs(list[i] - list[i + K - 1]))
print(min(DIF))

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

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

trees.sort()
min_diff = 99999999999
for i in range(N-K+1) :
	max_v = trees[i+K-1]
	min_v = trees[i]
	diff = max_v - min_v
	if (min_diff > diff) :
		min_diff = diff
	if (min_diff == 0) :
		break



print(min_diff)

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

h.sort()

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())
trees = sorted([int(input()) for _ in range(n)])
ans = float('inf')
for i in range(n):
  if i+k>n:
    print(ans)
    exit()
  else:
    ans = min(trees[i+k-1]-trees[i], ans)
    if ans==0:
      print(ans)
      exit()

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

dif = 1000000000
h_sort = sorted(h)
for i in range(n-k+1):
    dif = min(dif,h_sort[i+k-1]-h_sort[i])

print(dif)

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

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

h = sorted(h)
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())
heights = []

for i in range(n):
    h = int(input())
    heights.append(h)

heights = sorted(heights)

diffs = [heights[j + k - 1] - heights[j] for j in range(n - k + 1)]

print(min(diffs))

Solution (Python):


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

if __name__ == "__main__":
    n, k = read_int_n()
    num = []
    for _ in range(n):
        num.append(int(input()))
    num.sort()
    ans = 10 ** 9
    for i in range(n-k+1):
        if ans > num[i+k-1] - num[i]:
            ans = num[i+k-1] - num[i]
    print(ans)

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

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

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

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

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

gosa.sort()
print(gosa[0])

Solution (Python):
N, K = map(int, input().split())
h = []
for i in range(N):
  h.append(int(input()))
h.sort()
mini = 1e9
for i in range(N-K+1):
  sub = h[i+K-1] - h[i]
  if(sub < mini):
    mini = sub
print(mini)

Solution (Python):
N,K=list(map(int,input().split()))
a=[]
for i in range(N):
  a.append(int(input()))
a.sort(reverse=True)
ans=a[0]-a[K-1]
for i in range(N-K+1):
  ans = min(ans, a[i]-a[i+K-1])
print(ans)

Solution (Python):

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



li = [int(input()) for i in range(N)]
li.sort()
min_val = 10e10
for i in range(N-K+1):
    val = li[K + i - 1] - li[i]
    if val < min_val:
        min_val = val
print(int(min_val))

Solution (Python):
import pdb


def main():
    tmp = input().split()
    n = int(tmp[0])
    k = int(tmp[1])

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

    tree.sort()
    
    min_num = -1
    
    for i in range(0, n-k+1):
        
        tmp = tree[i+k-1] - tree[i]
        if min_num == -1:
            min_num = tmp
        elif min_num > tmp:
            min_num = tmp

    print("%d" % min_num)

        
if __name__ == '__main__':
    main()

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

H.sort()

res = 1000000000
for i in range(N-K+1):
    
    
    tmp = H[i+K-1]-H[i]
    if tmp < res:
        res = tmp
print(res)

Solution (Python):
import sys
stdin = sys.stdin
 
sys.setrecursionlimit(10**5) 
 
def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

n,k = li()
h = [ni() for _ in range(n)]

h.sort()

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

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

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

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

Solution (Python):
n,k = (int(i) for i in input().split())
l = list([])
for i in range(n):
    h = int(input())
    l.append(h)
l2 = sorted(l, reverse=True)

d = k-1
d_min = 1000000000
for i in range(n-d):
    tmp = l2[i] - l2[i+d]
    if (d_min > tmp ):
        d_min = tmp
print(d_min)

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

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

min_diff = 10 ** 9
for i in range(n-k+1):
    h_max = h_list[i]
    h_min = h_list[i+k-1]
    h_diff = h_max - h_min
    if min_diff > h_diff:
        min_diff = h_diff
    if min_diff == 0:
        print(0)
        exit()
print(min_diff)

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

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

for i in range(N-K+1):
    P.append( L[i+K-1] - L[i])
print(min(P))

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

Solution (Python):
n, k = map(int, 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 numpy as np
def main():
 
	buf = input().split()
	n = int(buf[0])
	k = int(buf[1])
	l = np.zeros(n)
	for i in range(n):
		l[i] = int(input())
 
	l = np.sort(l)
	min = -1
	for i in range(n-k+1):
		bufmin = l[i+k-1] - l[i]
		if (min == -1):
			min = bufmin
		elif (bufmin < min):
			min = bufmin
 
	print(int(min))
 
main()

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

h.sort()

m = -1
for i in range(N-K+1):
    d = h[i+K-1] - h[i]
    if d < m or m == -1:
        m = d
        
print(m)

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

for i in range(N - K + 1):
	
	
	ans = H[i + K - 1] - H[i]
	ansl.append(ans)

print(min(ansl))

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

for i in range(n):
    h_list.append(int(input()))

h_list.sort()
result = float("inf")

for i in range(n-k+1):
    r = h_list[i+k-1] - h_list[i]
    if result >= r:
        result = r
print(result)

Solution (Python):
def inpl(): return [int(i) for i in input().split()]
N, K = inpl()
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())
h=[int(input()) for i 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=map(int,input().split())
l=[]
for _ in range(n):
    l.append(int(input()))
l.sort()
a=0
for i in range(n-k+1):
    if i==0:
        a=l[i+k-1]-l[i]
    else:
        a=min(a,abs(l[i+k-1]-l[i]))
print(a)

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

h.sort()
ans1=10**10
for i in range(N-K+1):
    ans2=0
    ans2=h[i+K-1]-h[i]
    if ans1 > ans2:
        ans1=ans2
print(ans1)

Solution (Python):
A,B = map(int,input().split())
P = [int(input()) for i in range(A)]
P.sort()

ans = 2147483647
for n in range(len(P)-B+1):
    ans = min(ans,P[n+B-1]-P[n])
    
print(str(ans))

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

h.sort()
r = [abs(h[i]-h[i+k-1]) for i in range(n-(k-1))]
print(min(r))

Solution (Python):

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



ls = sorted(ls)
ans = 1000000000
for i in range(n-k+1):
    mx = ls[i+k-1]
    mn = ls[i]
    ans = min(ans,mx - mn)
print (ans)

Solution (Python):
import numpy as np
N,K = map(int,input().split(' '))
h = []
for i in [0]*N:
	h.append(int(input()))


def sort(l):
	m = sum(l)/len(l)
	a = [x for x in l if x < m]
	b = [x for x in l if m <= x]
	if a == []:
		return b
	if b == []:
		return a
	return sort(a)+sort(b)

h = np.array(h)
sh = np.sort(h)

ans = max(h)
for i in range(0,N-K+1):
	s = (sh[i+K-1] - sh[i])
	if s < ans:
		ans = s
print(ans)

Solution (Python):
import numpy as np

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

h.sort()

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

Solution (Python):
import numpy as np
def main():
	
	buf = input().split()
	n = int(buf[0])
	k = int(buf[1])
	l = np.zeros(n)
	for i in range(n):
		l[i] = int(input())
	
	l = np.sort(l)
	min = -1
	for i in range(n-k+1):
		bufmin = l[i+k-1] - l[i]
		if (min == -1):
			min = bufmin
		elif (bufmin < min):
			min = bufmin
		
	print(int(min))
		
main()

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

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


min = 2000000000
height_list.sort()


for start in range(0, len(height_list) - k + 1):
    
    

    x = height_list[start + k - 1] - height_list[start]
    if x < min:
        min = x

print(min)

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

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

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

h.sort()

ans = 1100000000
for i in range(n - k + 1):
    hh =  h[i + k - 1] - h[i]
    if hh < ans:
        ans = hh

print(ans)

Solution (Python):
N, K = list(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):
def solve(n, k, hs):
    gs = sorted(hs)
    return min(gs[i + k - 1] - gs[i] for i in range(n - k + 1)) 


def main():
    n, k = (int(z) for z in input().split())
    hs = [int(input()) for _ in range(n)]
    res = solve(n, k, hs)
    print(res)


if __name__ == "__main__":
    main()

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

height.sort()
dh = 10 ** 10
for i in range(n - k + 1):
  if height[i + k -1] - height[i] < dh:
    dh = height[i+k-1] - height[i]

print(dh)

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

d = float('inf')
for i in range(N - K + 1):
    div = s_h[i + K - 1] - s_h[i]
    if div < d:
        d = div
print(d)

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


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

print(MIN)

Solution (Python):
N, K = map(int, input().split())
H = [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):
import sys
n, k = map(int, input().split())
lines = sys.stdin.readlines()
trees = [None] * n
for i in range(n):
    trees[i] = int(lines[i])



trees.sort()


min_h = sys.maxsize
k_ = k-1
for i in range(n-k+1):
    tmp = trees[i+k_] - trees[i]
    if tmp < min_h:
        min_h = tmp

print(min_h)

Solution (Python):
N,K = map(int,input().split())
h_list = [int(input()) for _ in range(N)]
h_list_s = sorted(h_list)
min_d = 1000000000
for k in range(0,N-K+1):
  tmp = h_list_s[k+K-1] - h_list_s[k]
  if min_d > tmp:
    min_d = tmp
    
print(min_d)

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

H.sort(reverse=True)

max_num = H[0]
min_num = H[K-1]
diff = max_num - min_num
l_h = len(H)
for i in range(l_h-K+1):
    max_num = H[i]
    min_num = H[i+K-1]
    _diff = max_num - min_num
    if _diff < diff:
        diff = _diff
print(diff)

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

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

minimum = 1000000000
for i in range(n - k + 1):
    val = highs[i + k - 1] - highs[i]
    if val < minimum:
        minimum = val

print(minimum)

Solution (Python):
tree_list = []
n = 0
k = 0
tree_max = 0
tree_min = 0

n, k = input().split(' ')
n = int(n)
k = int(k)
for i in range(n):
    tree = int(input())
    tree_list.append(tree)

tree_list.sort()
diff = tree_list[n - 1]
for i in range(n - k + 1):
    tree_min = tree_list[i]
    tree_max = tree_list[i + k - 1]
    if tree_max - tree_min < diff:
        diff = tree_max - tree_min

print(diff)

Solution (Python):
N,K = map(int,input().split(" "))
height = []
for i in range(N):
    height.append(int(input()))
height.sort()
min = 10**9
n = N-K+1
for j in range(n):
    hmin = height[j]
    hmax = height[j+K-1]
    hei = hmax-hmin
    if hei <= min:
        min = hei
print(min)

Solution (Python):
m = input()
n = int(m.split()[0])
k = int(m.split()[1])
t = []
for i in range(n):
    t.append(int(input()))
t.sort()
a = None
for i in range(len(t)-k+1):
    w = abs(-1*t[i] + t[i+k-1])
    if i == 0:
        a = w
    elif a > w:
        a = w
print(a)

Solution (Python):


import sys
from collections import Counter


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


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


N, K = inpl()

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

C = Counter(H)

H2 = sorted(H)
S = [0]
for i in range(N - 1):
    S.append(S[-1] + H2[i + 1] - H2[i])


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

print(ans)

Solution (Python):
N,K = map(int,input().split())
h_list = [int(input()) for i in range(N)]
h_list.sort()
ans = h_list[N-1] - h_list[0]

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

print(ans)

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

h.sort()
dim=[]
for i in range(n-k+1):
    dim.append(h[i+k-1]-h[i])
dim.sort()
print(dim[0])

Solution (Python):
n, k = tuple(map(int, input().split()))
arr = [0 for i in range(n)]
sums = 0
for i in range(n):
    inp = int(input())
    arr[i] = inp

arr.sort()

sa = 1000000000
for i in range(n-k+1):
    newsa = arr[i+k-1] - arr[i]
    if newsa < sa:
        sa = newsa

print(sa)

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

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

h.sort()
for i in range(n-k+1):
  m.append(h[i+k-1]-h[i])
m.sort()
print(m[0])

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


for i in range(0, N - K+1):
    h_min = h[i]
    h_max = h[i + K - 1]
    d = h_max - h_min
    
    if d < d_min:
        d_min = d
    
print(d_min)

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

N = int(N)
K = int(K)

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

h_list = sorted(h_list)

hmin = max(h_list)
for i in range(N-K+1):
    h_s = h_list[i]
    h_b = h_list[i+K-1]
    if h_b - h_s < hmin:
        hmin = h_b - h_s

print(hmin)

Solution (Python):
import sys


def main() -> None:
    n, k = [int(s) for s in input().split()]
    h = []
    for i in range(n):
        h += [int(input())]
    h.sort()

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

    print(mn)


if __name__ == '__main__':
    main()

Solution (Python):
n,k=map(int,input().split())
l=[]
for i in range(n):
  l.append(int(input()))
l.sort()
m=l[k-1]-l[0]
for i in range(1,n-k+1):
  a=l[i+k-1]-l[i]
  if a<m:
    m=a
print(m)

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

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

Hs = sorted(hs)
min = 10000000000000

for i in range(N - K + 1):
    right = i + K - 1
    diff = Hs[i + K -1] - Hs[i]
    if diff < min:
        min = diff

print(min)

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

h.sort()

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

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

Solution (Python):
from sys import stderr, setrecursionlimit
from functools import reduce
from operator import add
import statistics
setrecursionlimit(2147483647)


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


def debug(*x, sep=" ", end="\n"):
    for item in x:
        stderr.write(str(item))
        stderr.write(sep)
    stderr.write(end)


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

h.sort()
min_dif = 10**10
for i in range(n-k+1):
    t = h[i+k-1] - h[i]
    if min_dif > t:
        min_dif = t
print(min_dif)

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

Solution (Python):
def main(hs, K):
    hs.sort()
    print(min(hs[i+K-1] - hs[i] for i in range(len(hs)-K+1)))







if __name__ == "__main__":
    N, K = (int(x) for x in input().split())

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

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

h.sort()
ans = 10**12
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=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 = list(map(int, input().split()))
trees = []

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

trees.sort()
subs = []
for i in range(n - k + 1):
	subs.append(trees[i + k - 1] - trees[i])

print(min(subs))

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

sort_height = sorted(height)
dif_height = [abs(sort_height[i] - sort_height[i+K-1]) for i in range(N-K+1)]


print(min(dif_height))

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

ans=100000000000000
for i in range(n-k+1):
  tmp=hList[i+k-1]-hList[i]
  if(ans>tmp):
    ans=tmp
print(ans)

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

re = 10**9
for i in range(N - K + 1):
    min_h = h[i]
    max_h = h[i+K-1]
    diff = max_h - min_h
    if diff < re:
        re = diff
print(re)

Solution (Python):
import sys
def 解():
    N,K=[int(_) for _ in sys.stdin.readline().split()]
    aH=sorted(int(_) for _ in sys.stdin.readlines())

    iDiff = aH[-1]
    for i in range(N-K+1):
        d = aH[i+K-1] - aH[i]
        if d < iDiff:
            iDiff = d
        if iDiff == 0:
            break
    print(iDiff)
解()

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

h = []

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

h.sort()

m = 9999999999999999999

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

print(m)

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

h = [0] * N
for i in range(N):
    h[i] = int(input())

h.sort()

min = 1000000000

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

print(min)

Solution (Python):
def c_christmas_eve(N, K, H):
    H.sort()
    ans = float('inf')
    for j in range(N - K + 1):
        h_max = H[j + K - 1]
        h_min = H[j]
        ans = min(ans, h_max - h_min)
    return ans

N, K = [int(i) for i in input().split()]
H = [int(input()) for _ in range(N)]
print(c_christmas_eve(N, K, H))

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

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

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

h.sort()

res = h[K-1] - h[0]

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

print(res)

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

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

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

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

h.sort()

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

print(min)

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

h.sort()
ans = 999999999999999999
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 _ in range(N)]
for i in range(N):
    h[i] = int(input())
    
h.sort(reverse=True)

min = h[0]-h[K-1]
for i in range(N-K+1):
    if min>h[i]-h[i+K-1]:
        min = h[i]-h[i+K-1]
        
print(min)

Solution (Python):
N, K = map(int, input().split())
h_arr = []
for i in range(N):
    h_arr.append(int(input()))
h_arr = sorted(h_arr)
min_diff = 10**9
for i in range(N-K+1):
    diff = h_arr[i+K-1] - h_arr[i]
    if min_diff > diff:
        min_diff = diff
print(min_diff)

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

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

h.sort(reverse = False)

for s in range(0, len(h)-(K-1)):
    hmintmp = h[s+(K-1)] - h[s]

    if hmin == None or hmin > hmintmp:
        hmin = hmintmp


print(hmin)