AtCoder Beginner Contest 115 - Christmas Eve

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-----
5 3
10
15
11
14
12

-----Sample Output-----
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.

Source: atcoder | Difficulty: EASY


Solutions:
(n, k) = map(int, input().split())
lst = [int(input()) for _ in range(n)]
lst.sort()
print(min((lst[i + k - 1] - lst[i] for i in range(n - k + 1))))


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


(n, k) = map(int, input().split())
l = sorted([int(input()) for _ in [0] * n])
print(min((j - i for (i, j) in zip(l, l[k - 1:]))))


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


import math
from math import gcd, pi, sqrt
INF = float('inf')
import sys
sys.setrecursionlimit(10 ** 6)
import itertools
from collections import Counter, deque

def i_input():
	return int(input())

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

def i_list():
	return list(i_map())

def i_row(N):
	return [i_input() for _ in range(N)]

def i_row_list(N):
	return [i_list() for _ in range(N)]

def s_input():
	return input()

def s_map():
	return input().split()

def s_list():
	return list(s_map())

def s_row(N):
	return [s_input for _ in range(N)]

def s_row_str(N):
	return [s_list() for _ in range(N)]

def s_row_list(N):
	return [list(s_input()) for _ in range(N)]

def main():
	(n, k) = i_map()
	h = [i_input() for i in range(n)]
	h.sort()
	print(min((h[i + k - 1] - h[i] for i in range(n - k + 1))))

def __starting_point():
	main()
__starting_point()


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


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


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


(N, K) = 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(0, N - (K - 1))]))


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


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


import sys
sys.setrecursionlimit(10000000)
mod = 10 ** 9 + 7
INF = float('inf')
from sys import stdin
readline = stdin.readline

def readInts():
	return list(map(int, readline().split()))

def readTuples():
	return tuple(map(int, readline().split()))

def I():
	return int(readline())
(n, k) = readInts()
A = [I() for _ in range(n)]
A = sorted(A)
a = INF
for i in range(n - k + 1):
	a = min(a, abs(A[i + k - 1] - A[i]))
print(a)


(N, K) = map(int, input().split())
h = [int(input()) for i in range(N)]
s_h = sorted(h)
tmp = []
ans1 = 0
ans2 = 0
ans3 = 0
for i in range(N - K + 1):
	ans1 = s_h[i + K - 1]
	ans2 = s_h[i]
	ans3 = ans1 - ans2
	tmp.append(ans3)
print(min(tmp))


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


(n, k) = map(int, input().split())
h = [int(input()) for _ in [0] * n]
h.sort()
print(min((j - i for (i, j) in zip(h, h[k - 1:]))))


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


(n, k) = list(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))))


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


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


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


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


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


def main():
	(N, K) = list(map(int, input().split()))
	H = sorted([int(input()) for _ in range(N)])
	ans = min([H[i + K - 1] - H[i] for i in range(N - K + 1)])
	print(ans)

def __starting_point():
	main()
__starting_point()


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


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


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


(N, K) = list(map(int, input().split()))
h = []
ans = 1000000000
for i in range(N):
	h.append(int(input()))
h = sorted(h)
for j in range(N - K + 1):
	if ans > h[j + K - 1] - h[j]:
		ans = h[j + K - 1] - h[j]
print(ans)


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


(_, k, *h) = map(int, open(0).read().split())
h.sort()
ans = 10 ** 9
for (hmin, hmax) in zip(h, h[k - 1:]):
	ans = min(ans, hmax - hmin)
print(ans)


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


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


(n, k) = map(int, input().split())
h = []
for i in range(n):
	h.append(int(input()))
h.sort()
ans = 1000000000
for j in range(n - k + 1):
	s = h[j + k - 1] - h[j]
	if s < ans:
		ans = s
print(ans)


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


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


(n, k) = map(int, input().split())
h = [0] * n
for i in range(n):
	h[i] = int(input())
h = sorted(h)
answer = 100000000000000
for j in range(n + 1 - k):
	answer = min(answer, h[j - 1 + k] - h[j])
print(answer)


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


(N, M) = list(map(int, input().split()))
K = [int(input()) for i in range(N)]
K = sorted(K)
print(min([K[i + M - 1] - K[i] for i in range(N - M + 1)]))


(n, k) = map(int, input().split())
l = sorted((int(input()) for _ in [0] * n))
print(min((l[i + k - 1] - l[i] for i in range(n - k + 1))))


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


(n, k) = map(int, input().split())
h = sorted([int(input()) for _ in range(n)])
ans = float('inf')
for i in range(n - k + 1):
	sa = h[i + k - 1] - h[i]
	if ans > sa:
		ans = sa
print(ans)


(N, K) = map(int, input().split())
A = [int(input()) for _ in range(N)]
B = sorted(A)
ans = []
flag = 0
for i in range(N - K + 1):
	ans.append(B[i + K - 1] - B[i])
	flag = 1
print(min(ans) if flag == 1 else '0')


(n, k) = map(int, input().split())
lis = []
ans = 1000000000000000000
for _ in range(n):
	lis.append(int(input()))
lis.sort()
for i in range(n):
	if i + k - 1 >= len(lis):
		break
	else:
		mi = abs(lis[i] - lis[i + k - 1])
		ans = min(ans, mi)
print(ans)


(N, K) = map(int, input().split())
L = []
mini = float('inf')
for i in range(N):
	h = int(input())
	L.append(h)
L.sort(reverse=True)
for i in range(len(L) - K + 1):
	if L[i] - L[i + K - 1] < mini:
		if L[i] - L[i + K - 1] == 0:
			print(0)
			return
		else:
			mini = L[i] - L[i + K - 1]
print(mini)


(n, k) = list(map(int, input().split()))
arr = []
for _ in range(n):
	arr.append(int(input()))
a = sorted(arr)
m = 10 ** 9
for i in range(n - k + 1):
	ma = a[i + k - 1] - a[i]
	m = min(m, ma)
print(m)


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


import numpy as np
(N, K) = list(map(int, input().split()))
H = [int(input()) for _ in range(N)]
sH = np.array(sorted(H), dtype=np.int64)
shift = sH[K - 1:]
diff = shift - sH[:len(shift)]
ans = min(diff)
print(ans)


(N, K) = map(int, input().split())
H = sorted([int(input()) for n in range(N)])
A = []
for n in range(N - K + 1):
	A += [H[n + K - 1] - H[n]]
print(min(A))


(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])
print(min(l))


(N, K) = map(int, input().split())
ls = sorted([int(input()) for _ in range(N)])
print(min([ls[x + K - 1] - ls[x] for x in range(N - K + 1)]))


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


(n, k) = list(map(int, input().split()))
h = [int(input()) for _ in range(n)]
h.sort()
d = []
for i in range(n - k + 1):
	d.append(abs(h[i] - h[i + k - 1]))
print(min(d))


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


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


(N, K) = map(int, input().split())
H = [int(input()) for _ in range(N)]
H.sort(reverse=True)
min_h = float('inf')
for i in range(N - K + 1):
	temp = H[i] - H[i + K - 1]
	min_h = min(min_h, temp)
print(min_h)


import sys
input = sys.stdin.readline
import numpy as np

def main():
	numbers = []
	(n, m) = map(int, input().split())
	[numbers.append(int(input())) for _ in range(n)]
	n_numbers = np.array(numbers, dtype=int)
	n_numbers.sort()
	print(min((n_numbers[i + m - 1] - n_numbers[i] for i in range(n - m + 1))))

def __starting_point():
	main()
__starting_point()


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


(n, k) = list(map(int, input().split()))
h = sorted([int(input()) for _ in range(n)])
print(min([b - s for (s, b) in zip(h, h[k - 1:])]))


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


(N, K, *H) = map(int, open(0).read().split())
H.sort()
a = float('inf')
for i in range(N - K + 1):
	a = min(a, H[i + K - 1] - H[i])
print(a)


(n, k, *h) = map(int, open(0).read().split())
h.sort()
print(min((y - x for (x, y) in zip(h, h[k - 1:]))))


(n, k) = list(map(int, input().split()))
h = []
for i in range(n):
	x = int(input())
	h.append(x)
h = sorted(h)
ans = 10 ** 9
tmp = 0
for i in range(k - 1, n):
	tmp = h[i] - h[i - (k - 1)]
	if tmp < ans:
		ans = tmp
print(ans)


def main():
	(N, K) = list(map(int, input().split()))
	h = []
	for _ in range(N):
		h.append(int(input()))
	h.sort()
	ans = pow(10, 10)
	for k in range(N - K + 1):
		ans = min(ans, abs(h[k] - h[K - 1 + k]))
	print(ans)

def __starting_point():
	main()
__starting_point()


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


import sys
import math
from collections import defaultdict, deque, Counter
from copy import deepcopy
from bisect import bisect, bisect_right, bisect_left
from heapq import heapify, heappop, heappush
input = sys.stdin.readline

def RD():
	return input().rstrip()

def F():
	return float(input().rstrip())

def I():
	return int(input().rstrip())

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

def MF():
	return map(float, input().split())

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

def TI():
	return tuple(map(int, input().split()))

def LF():
	return list(map(float, input().split()))

def Init(H, W, num):
	return [[num for i in range(W)] for j in range(H)]

def main():
	(N, K) = MI()
	L = [I() for i in range(N)]
	L.sort()
	res = float('inf')
	for i in range(N - K + 1):
		res = min(res, L[i + K - 1] - L[i])
	print(res)

def __starting_point():
	main()
__starting_point()


(n, k) = map(int, input().split())
hs = sorted([int(input()) for _ in range(n)])
m = float('inf')
for (l, h) in zip(hs, hs[k - 1:]):
	m = min(m, h - l)
print(m)


import sys
input = sys.stdin.readline
(n, k) = map(int, input().split())
h_list = []
for _ in range(n):
	h_list.append(int(input()))
sort_h_list = sorted(h_list)
L_index = 0
R_index = 0
L_value = sort_h_list[0]
R_value = sort_h_list[0]
count = 0
ans = 10 ** 9
while R_index < n:
	if count < k:
		count += 1
		R_value = sort_h_list[R_index]
		if count == k:
			ans = min(ans, R_value - L_value)
		R_index += 1
	else:
		count -= 1
		L_index += 1
		L_value = sort_h_list[L_index]
print(ans)


(n, k) = (int(i) for i in input().split())
hl = [int(input()) for _ in range(n)]
hl.sort()
print(min([hl[i + k - 1] - hl[i] for i in range(n - k + 1)]))


(N, K, *H) = map(int, open(0).read().split())
H.sort()
print(min((y - x for (x, y) in zip(H, H[K - 1:]))))


import math
import collections
from itertools import product
ii = lambda : int(input())
mi = lambda : map(int, input().split())
li = lambda : list(map(int, input().split()))
(n, k) = mi()
h = [ii() for i in range(n)]
h.sort()
ans = 10 ** 12
for i in range(n):
	last = i + k - 1
	if last >= n:
		break
	ans = min(ans, h[last] - h[i])
print(ans)


(n, k) = map(int, input().split())
h = sorted([int(input()) for _ in range(n)], reverse=True)
m = float('inf')
for i in range(n - k + 1):
	m = min(m, h[i] - h[i + k - 1])
print(m)


(n, k) = map(int, input().split())
h = sorted([int(input()) for _ in range(n)])
nums = [0] * (n - 1)
for i in range(n - 1):
	nums[i] = h[i + 1] - h[i]
ans = sum(nums[:k - 1])
t = sum(nums[:k - 1])
for i in range(k - 1, n - 1):
	t += nums[i] - nums[i - k + 1]
	if t < ans:
		ans = t
print(ans)


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


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


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


(n, k) = map(int, input().split())
trees = [int(input()) for i in range(n)]
trees.sort()
print(min((trees[i + k - 1] - trees[i] for i in range(n - k + 1))))


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


(N, K) = map(int, input().split())
H = sorted([int(input()) for _ in range(N)])
ans = H[K - 1] - H[0]
for k in range(N - K + 1):
	ans = min(ans, H[k + K - 1] - H[k])
print(ans)


(N, K) = list(map(int, input().split()))
h = [0] * N
for H in range(N):
	h[H - 1] = int(input())
A = sorted(h)
L = K - 1
M = A[L] - A[0]
O = N - K + 1
for j in range(O):
	if M > A[L + j] - A[j]:
		M = A[L + j] - A[j]
print(M)


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


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


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


(N, K) = map(int, input().split())
A = [int(input()) for _ in range(N)]
B = sorted(A)
ans = []
for i in range(N - K + 1):
	ans.append(B[i + K - 1] - B[i])
print(min(ans))


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


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


(n, k) = map(int, input().split())
hl = []
for i in range(n):
	hl.append(int(input()))
hl.sort()
ans = 10000000001
for j in range(n - k + 1):
	s = hl[j + k - 1] - hl[j]
	if s < ans:
		ans = s
	else:
		pass
print(ans)


def solve(N, K, h):
	h.sort()
	ans = 10 ** 9
	for i in range(K - 1, N):
		ans = min(ans, h[i] - h[i - (K - 1)])
	print(ans)

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


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


(n, k) = map(int, input().split())
k -= 1
a = sorted([int(input()) for i in range(n)])
b = sorted([a[i + k] - a[i] for i in range(n - k)])
print(b[0])


(N, K) = map(int, input().split())
high = [int(input()) for _ in range(N)]
s_high = sorted(high)
ans = 10 ** 9
for i in range(N - K + 1):
	j = s_high[i + K - 1] - s_high[i]
	ans = min(ans, j)
print(ans)


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


(N, K) = map(int, input().split())
L = []
for i in range(N):
	L.append(int(input().rstrip()))
L.sort()
M = []
for i in range(N - K + 1):
	M.append(L[K + i - 1] - L[i])
M.sort()
print(M[0])


(N, K) = map(int, input().split())
h = []
for i in range(N):
	h.append(int(input()))
h.sort()
ans = 10 ** 18
for i in range(N - K + 1):
	if h[i + K - 1] - h[i] < ans:
		ans = h[i + K - 1] - h[i]
print(ans)


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


(n, k) = map(int, input().split())
arr = [int(input()) for _ in range(n)]
arr.sort()
print(min((abs(y - x) for (x, y) in zip(arr, arr[k - 1:]))))


(n, k) = map(int, input().split())
H = []
for i in range(n):
	H.append(int(input()))
H.sort()
ans = 10 ** 9 + 2
for i in range(n - k + 1):
	ans = min(ans, H[i + k - 1] - H[i])
print(ans)


(N, K) = map(int, input().split())
h = [0] * N
for i in range(N):
	h[i] = int(input())
h = sorted(h)
ans = 10 ** 10
for i in range(N - (K - 1)):
	tmp = h[i + K - 1] - h[i]
	ans = min(tmp, ans)
print(ans)


(n, k) = list(map(int, input().split()))
hs = [int(input()) for i in range(n)]
hs = sorted(hs)
diff = 10 ** 12
for i in range(n - k + 1):
	diff = min(diff, hs[i + k - 1] - hs[i])
print(diff)


(n, k) = [int(x) for x in input().split()]
l = sorted([int(input()) for x in range(n)])
m = [l[x + k - 1] - l[x] for x in range(n - k + 1)]
print(min(m))


(N, K) = map(int, input().split())
H = [int(input()) for n in range(N)]
H.sort()
R = [H[i + K - 1] - H[i] for i in range(N + 1 - K)]
print(min(R))


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


(N, K) = map(int, input().split())
T = sorted([int(input()) for _ in range(N)])
result = 10 ** 9
for i in range(N - K + 1):
	result = min(result, T[i + K - 1] - T[i])
print(result)


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


(n, k) = map(int, input().split())
l = sorted([int(input()) for i in range(n)])
print(min([abs(l[i] - l[i + k - 1]) for i in range(n - k + 1)]))


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


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


(N, K) = map(int, input().split())
A = sorted([int(input()) for i in range(N)])
B = [A[i - 1] - A[i - K] for i in range(K, N + 1)]
print(min(B))


(N, K) = map(int, input().split())
h = [int(input()) for _ in range(N)]
h = sorted(h, reverse=True)
diff = 10 ** 9
for i in range(N - K + 1):
	a = h[i] - h[i + K - 1]
	if diff > a:
		diff = a
	if diff == 0:
		break
print(diff)


(n, k) = map(int, input().split())
listh = []
for i in range(n):
	listh.append(int(input()))
listh.sort()
ans = 10 ** 9
for i in range(n - k + 1):
	high = listh[i + k - 1] - listh[i]
	if high < ans:
		ans = high
print(ans)


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


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


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


(n, k) = map(int, input().split())
l = [int(input()) for i in range(n)]
l.sort()
mi = float('INF')
for i in range(n - k + 1):
	sub = l[i + k - 1] - l[i]
	mi = min(mi, sub)
print(mi)


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


(N, K) = list(map(int, input().split()))
a = [int(input()) for i in range(N)]
a.sort()
ans = 100000000000000000000000
for j in range(N - K + 1):
	if a[j + K - 1] - a[j] < ans:
		ans = a[j + K - 1] - a[j]
print(ans)


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


(n, k) = map(int, input().split())
h = [int(input()) for i in range(n)]
h.sort(reverse=True)
ans = float('inf')
for i in range(n - k + 1):
	ans = min(h[i] - h[i + k - 1], ans)
print(ans)


(n, k, *H) = map(int, open(0).read().split())
H.sort()
ans = 10 ** 10
for (h0, h1) in zip(H, H[k - 1:]):
	ans = min(ans, h1 - h0)
print(ans)


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


(N, K, *trees) = map(int, open(0).read().split())
trees = sorted(trees)
print(min((trees[i + K - 1] - trees[i] for i in range(N - K + 1))))


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


(n, k) = map(int, input().split())
trees = []
for _i in range(n):
	tree = int(input())
	trees.append(tree)
trees.sort()
ans = float('inf')
for i in range(n - k + 1):
	diff = trees[i + k - 1] - trees[i]
	if ans > diff:
		ans = diff
print(ans)


(n, m) = list(map(int, input().split(' ')))
a = []
for i in range(n):
	x = int(input())
	a.append(x)
a.sort()
mn = 1000000000
for i in range(m - 1, n):
	mn = min(a[i] - a[i - m + 1], mn)
print(mn)


from sys import stdin
from collections import defaultdict
input = stdin.readline
T = 1
for t in range(1, T + 1):
	(n, k) = list(map(int, input().split()))
	_input = []
	for i in range(n):
		_input.append(int(input()))
	_input.sort()
	min_ans = 10000000000
	for i in range(k - 1, len(_input)):
		min_ans = min(min_ans, _input[i] - _input[i - k + 1])
	print(min_ans)


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


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


(n, k) = list(map(int, input().split()))
hl = sorted(list((int(input()) for _ in range(n))), reverse=True)
ans = 1001001001
for i in range(n - k + 1):
	ans = min(ans, hl[i] - hl[i + k - 1])
print(ans)


(n, k) = map(int, input().split())
H = sorted([int(input()) for i in range(n)])
D = sorted([H[i + k - 1] - H[i] for i in range(n - k + 1)])
print(min(D))


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


(N, K) = list(map(int, input().split()))
h = [int(input()) for _ in range(N)]
ans = 10 ** 10
h.sort()
for i in range(0, N - K + 1):
	tmp = h[i + K - 1] - h[i]
	ans = min(ans, tmp)
print(ans)


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

def check(x):
	for i in range(n):
		if i + k - 1 >= n:
			continue
		if h[i + k - 1] - h[i] <= x:
			return True
	return False
(hmax, hmin) = (max(h), min(h))
(fv, tv) = (-1, hmax - hmin)
while tv - fv > 1:
	mid = (tv + fv) // 2
	if check(mid):
		tv = mid
	else:
		fv = mid
print(tv)


(n, k) = map(int, input().split())
h = []
ma = 10 ** 9
for i in range(n):
	a = int(input())
	h.append(a)
h.sort()
for i in range(n - k + 1):
	sa = h[k - 1 + i] - h[i]
	if sa < ma:
		ma = sa
print(ma)


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


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


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


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


(N, K) = map(int, input().split())
H = sorted([int(input()) for n in range(N)])
ans = H[-1] - H[0]
for n in range(N - K + 1):
	ans = min(ans, H[n + K - 1] - H[n])
print(ans)


def main():
	(n, k) = map(int, input().split(' '))
	data = sorted((int(input()) for _ in range(n)))
	print(min([data[i + k - 1] - data[i] for i in range(n - k + 1)]))
main()


(N, K) = map(int, input().split())
lsh = [int(input()) for _ in range(N)]
lsh.sort()
ans = 10 ** 9
for i in range(N - K + 1):
	ans = min(ans, lsh[i + K - 1] - lsh[i])
print(ans)


(n, k) = map(int, input().split())
h = []
for i in range(n):
	h_ = int(input())
	h.append(h_)
h.sort()
ans = float('inf')
for i in range(n - k + 1):
	ans = min(ans, h[i + k - 1] - h[i])
print(ans)


import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007

def main():
	(N, K, *H) = list(map(int, read().split()))
	H.sort()
	ans = INF
	for i in range(N - K + 1):
		if ans > H[i + K - 1] - H[i]:
			ans = H[i + K - 1] - H[i]
	print(ans)
	return

def __starting_point():
	main()
__starting_point()


(n, k) = map(int, input().split())
ab = [int(input()) for _ in range(n)]
sab = sorted(ab)
res = 10 ** 10
for i in range(n - k + 1):
	res = min(res, sab[i + k - 1] - sab[i])
print(res)


(n, m) = map(int, input().split(' '))
lst = [int(input()) for i in range(n)]
lst.sort()
print(min((lst[i + m - 1] - lst[i] for i in range(n - m + 1))))


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


(n, k) = map(int, input().split())
a = [int(input()) for i in range(n)]
c = sorted(a)
m = 10 ** 10
for x in range(n - k + 1):
	m = min(m, c[x + k - 1] - c[x])
print(m)


(n, k) = map(int, input().split())
l = []
for i in range(n):
	l.append(int(input()))
b = sorted(l)
ans = b[k - 1] - b[0]
for i in range(n - k + 1):
	ans = min(ans, b[i + k - 1] - b[i])
print(ans)


(n, k) = list(map(int, input().split()))
hs = [int(input()) for _ in range(n)]
hs.sort()
an = hs[k - 1] - hs[0]
for i in range(1, n - k + 1):
	ans = hs[i + k - 1] - hs[i]
	an = min(an, ans)
print(an)


(N, K) = map(int, input().split())
H = []
min_dif = 1000000000000000000000000
for n in range(N):
	h = int(input())
	H.append(h)
H.sort()
for i in range(N - K + 1):
	dif = H[i + K - 1] - H[i]
	if dif < min_dif:
		min_dif = dif
print(min_dif)


import sys
input = sys.stdin.readline

def ngram(numbers, n):
	return list([numbers[i:i + n] for i in range(len(numbers) - 2)])

def main():
	numbers = []
	(n, m) = map(int, input().split())
	[numbers.append(int(input())) for _ in range(n)]
	numbers.sort()
	print(min((numbers[i + m - 1] - numbers[i] for i in range(n - m + 1))))

def __starting_point():
	main()
__starting_point()


import sys
(N, K, *h) = map(int, sys.stdin.read().split())
h.sort()
m = min((h[i + K - 1] - h[i] for i in range(N - K + 1)))
print(m)


(N, K) = map(int, input().split())
H = sorted((int(input()) for _ in range(N)))
print(min((h1 - h0 for (h0, h1) in zip(H, H[K - 1:]))))


(n, k) = [int(x) for x in 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))))


(N, K) = list(map(int, input().split()))
h = [int(input()) for i in range(N)]
h.sort(reverse=True)
minimum = 10 ** 9
for i in range(N - K + 1):
	minimum = min(minimum, h[i] - h[i + K - 1])
print(minimum)