Problem:
 Score : 300 points    Problem Statement In some other world, today is Christmas Eve.  There are N trees planted in Mr. Takaha's garden. The height of the i -th tree (1 \leq i \leq N) is h_i meters.  He decides to choose K trees from these trees and decorate them with electric lights. To make the scenery more beautiful, the heights of the decorated trees should be as close to each other as possible.  More specifically, let the height of the tallest decorated tree be h_{max} meters, and the height of the shortest decorated tree be h_{min} meters. The smaller the value h_{max} - h_{min} is, the better. What is the minimum possible value of h_{max} - h_{min} ?      Constraints  2 \leq K < N \leq 10^5  1 \leq h_i \leq 10^9  h_i is an integer.         Input Input is given from Standard Input in the following format:  N  K  h_1  h_2  :  h_N       Output Print the minimum possible value of h_{max} - h_{min} .        Sample Input 1 5 3
10
15
11
14
12      Sample Output 1 2  If we decorate the first, third and fifth trees, h_{max} = 12, h_{min} = 10 so h_{max} - h_{min} = 2 . This is optimal.       Sample Input 2 5 3
5
7
5
7
7      Sample Output 2 0  If we decorate the second, fourth and fifth trees, h_{max} = 7, h_{min} = 7 so h_{max} - h_{min} = 0 . This is optimal.  There are not too many trees in these sample inputs, but note that there can be at most one hundred thousand trees (we just can't put a sample with a hundred thousand lines here).

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

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

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 = [int(i) for i in input().split()]
h_list = []
for i in range(n):
    h_list.append(int(input()))
h_list = sorted(h_list)
cur = max(h_list)
for i in range(n-k+1):
    cur = min(cur, h_list[i+k-1]-h_list[i])
print(cur)

Solution (Python):
def main():
    """
    2 <= K < N <= 10^5
    1 <= hi <= 10^9
    """
    N, K = map(int, input().split())
    h = [int(input()) for _ in range(N)]

    ans = f(N, K, h)
    print(ans)


def f(N, K, h):
    h = sorted(h, reverse=True)

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

    return ans


if __name__ == '__main__':
    main()

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

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

h.sort()

ans = 999999999999999
for i in range(N-K+1):
    hmin = h[i]
    hmax = h[i+K-1]
    if (hmax - hmin) < ans:
        ans = hmax - hmin

print(ans)

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

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

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

Solution (Python):
import sys
import collections
import itertools
import math
import re
from collections import deque

MOD = 10 ** 9 + 7





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

H = [None] * N

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

H.sort()

mi = 10 ** 12
for i in range(0, N - K + 1):
    h1 = H[i + K - 1]
    h2 = H[i]

    mi = min(mi, h1 - h2)

print(mi)

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(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**9 + 100
for i in range(N-K+1):
    d = abs(h[i]-h[i+K-1])
    if d < ans:
        ans = d

print(ans)

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

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

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

Solution (Python):
import numpy as np

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

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

h_list = sorted(h_list)

out = np.min([h_list[i+K-1] - h_list[i] for i in range(N-K+1)])
print(out)

Solution (Python):


def main():
    """Function."""
    n, k = map(int, input().split())
    hight = [0 for i in range(n)]
    for i in range(n):
        hight[i] = int(input())
    hight.sort()

    check = [0 for i in range(n - k + 1)]
    for i in range(n - k + 1):

        check[i] = hight[i+k-1] - hight[i]

    print(min(check))

if __name__ == "__main__":
    main()

Solution (Python):
import sys

input = sys.stdin.readline
n, k = map(int, input().split())
h_list = []
for _ in range(n):
    h = int(input())
    h_list.append(h)
h_list.sort()
ans = 10**9
for i in range(n-k+1):
    ans = min(ans, h_list[i+k-1]-h_list[i])
print(ans)

Solution (Python):
n,k = input().split()
n = int(n)
k = int(k)
h = []
for i in range(n):
    h.append(int(input()))
h.sort()
min = 10 ** 9
for i in range(n-k+1):
    temp = h[i + k - 1] - h[i]
    if temp < min:
        min = temp
print(min)

Solution (Python):
n, k = [int(i) for i in input().split()]
h = []
for i in range(n):
    h.append(int(input()))
h.sort(reverse = True)
d = 10000000000
for i in range(n-k+1):
    if d > h[i] - h[i+k-1]:
        d = h[i] - h[i+k-1]
print(d)

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

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

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

Solution (Python):
a,b = map(int,input().split())

c = []

for i in range(a):
	c.append(int(input()))

c.sort()
p = c[0]
for i in range(a):
	c[i] -= p

k = c[b-1] - c[0]

for i in range(a-b+1):
	if k >= c[i+b-1] - c[i]:
		k = c[i+b-1] - c[i]

print(k)

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

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

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

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

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


print(ans)

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

diff=999999999999
laterpoint=K-1
for i in range(N-K+1):
  diff=min(diff,p[laterpoint+i]-p[i])
print(diff)

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

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

ans = 1e9
for i in range(N-K+1):
    ans = min(ans, abs(hs[i]-hs[i+K-1]))
print(ans)

Solution (Python):
N,K=map(int, input().split())
ls=[]
for i in range(N):
    ls.append(int(input()))
ls2=sorted(ls)
min=float("inf")
for i in range(N-K+1):
    if min > ls2[i+K-1] - ls2[i]:
        min=ls2[i+K-1] - ls2[i]
print(min)

Solution (Python):
N,K=map(int,raw_input().split())
hlist=[]

for i in range(N):
	hlist.append(int(raw_input()))

hlist.sort()

i=0
j=K-1

minn=100000000000
for i in range(N-K+1):
	diff=hlist[i+K-1]-hlist[i]
	if diff<minn:
		minn=diff

print(minn)

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

h.sort()

ans = float("inf")

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

print(ans)

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

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

Solution (Python):
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)

Solution (Python):
N, K = map(int, input().split())
h = [int(input()) for _ in range(N)]
h.sort()
ans = 10**100
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)]
sorted_h = sorted(h)
ans = float('inf')
for i in range(N-K+1):
    temp = sorted_h[i+K-1]-sorted_h[i]
    ans = min(ans, temp)
print(ans)

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

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

inputs.sort()
diff = 10**19


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

print(diff)

Solution (Python):
import sys
from collections import deque
import copy
import math
def get_read_func(fileobject):
    if fileobject == None :
        return raw_input
    else:
        return fileobject.readline


def main():
    if len(sys.argv) > 1:
        f = open(sys.argv[1])
    else:
        f = None
    read_func = get_read_func(f);
    input_raw = read_func().strip().split()
    [N, K] = [int(input_raw[0]), int(input_raw[1])]
    h_list = []
    for i in range(N):
        input_raw = read_func().strip().split()
        [h] = [int(input_raw[0])]
        h_list.append(h)

    h_list.sort(reverse = True)
    min_sub_h = sys.maxint
    for i in range(N - K + 1):
        min_sub_h = min(min_sub_h, h_list[i] - h_list[i + K -1])
    print min_sub_h


if __name__ == '__main__':
    main()

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

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

h.sort()

total = None
for i in range(n - k + 1):
    diff = h[i+k-1] - h[i]
    if total is None or total > diff:
        total = diff

print(total)

Solution (Python):
import numpy as np

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

h = np.array([int(input()) for _ in range(n)])
h.sort()

d = h[k-1:] - h[:-k+1]
print(np.min(d))

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

smallest = float("inf")
for i in range(K-1,len(lst)):
    smallest = min(smallest,lst[i] - lst[i-K+1])
print(smallest)

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

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

print(min_differ)

Solution (Python):

from pprint import pprint
N, K = map(int, input().split())
h = [int(input()) for _ in range(N)]

h.sort()

diff = [10**10 for _ in range(N-K+1)]

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

print(min(diff))

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

a.sort()
ans = 9999999999999999999
for i in range(len(a)-(k-1)):
    x = a[i+k-1] - a[i]
    ans = min(ans,x)
print(ans)

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

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

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

lst = sorted(hs, reverse=True)

mini = lst[0] - lst[-1]
for i in range(N - K + 1):
    tmp = lst[i] - lst[i + K - 1]
    if tmp < mini:
        mini = tmp

print(mini)

Solution (Python):
n, k = map(int, input().split())
h = []
for i in range(n):
    h.append(int(input()))
h.sort()
ans = 10**9
for i in range(n - k + 1):

    ans = min(abs(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()
m = h[k-1] - h[0]
for i in range(1,n-k+1):
  x = h[k-1+i] - h[i]
  if x < m:
    m = x
print(m)

Solution (Python):

from sys import stdin

def solve():
    n, k = map(int, stdin.readline().split())
    items = []
    for i in range(n):
        items.append(int(stdin.readline().strip()))

    items = sorted(items)
    ans = 10**9
    for i in range(n-k+1):
        diff = items[i+k-1] - items[i]
        if diff<ans:
            ans = diff

    print(ans)

solve()

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

answer = int(10e9)
for i in range(n - k + 1):
    d = h[i + k - 1] - h[i]
    if d < answer:
        answer = d

print(answer)

Solution (Python):
from sys import stdin

def main():
    N, K = map(int, input().split())
    H = list(map(int, stdin.read().splitlines()))

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

input = lambda: stdin.readline()
main()

Solution (Python):
N,K = map(int,input().split())
int_list=[int(input()) for i in range(N)]
min = 10**9
int_list.sort()
for i in range (K - 1,len(int_list)):
    if int_list[i] - int_list[i - K + 1] < min:
        min = int_list[i] - int_list[i - K + 1]

print(min)

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

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

ans=10**9

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

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

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

h_list.sort()

answer = 1e+9

for i in range(N-K+1):
	h_min = h_list[i]
	h_max = h_list[i+K-1]

	if answer > (h_max - h_min):
		answer = h_max - h_min

print(answer)

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

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

    print(diff)


abc115_c()

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

Solution (Python):
def solve(N, K, Hs):
    Hs.sort()
    return min([b - a for (a, b) in zip(Hs[:], Hs[K - 1:])])


if __name__ == "__main__":
    N, K = tuple(map(int, input().split(" ")))
    Hs = [int(input()) for _ in range(N)]
    print(solve(N, K, Hs))

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

H = sorted(H)

tmp = 0
ans = H[-1]
for i in range(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 = []
for _ in range(N):
    h.append(int(input()))
h.sort()
mindiff = 10000000000
for i in range(len(h) - K + 1):
    mindiff = min(mindiff, h[i + K - 1] - h[i])
print(mindiff)

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

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):
    min_h = H[i]
    max_h = H[i+K-1]
    dif = max_h - min_h
    if dif < ans:
        ans = dif
print(ans)

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


h.sort()
result = 10 ** 10
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())
tree = [0] * n

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

tree.sort()
ans = 10 ** 9

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

print(ans)

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

Solution (Python):
a=list(map(int,input().split()))
N=a[0]
K=a[1]
h=[]
for i in range(N):
    h.append(int(input()))
h.sort()
min=abs(h[K-1]-h[0])
for i in range(N-K+1):
    if abs(h[i]-h[i+K-1]) < min:
        min=abs(h[i]-h[i+K-1])
print(str(min))

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

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

h = [int(m) for m in d]
h.sort(reverse=True)
li = []
for i in range(n):
    if i + k <= n:
        li.append(h[i] - h[i + k - 1])
    else:
        break
print(min(li))

Solution (Python):
N, K = map(int, input().split())
A = [int(input()) for _ in range(N)]
A.sort()
ans = A[-1]
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())
trees = []
for i in range(N):
  trees.append(int(input()))
  
trees = sorted(trees)
ans = 10000000000000

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

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

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

high_diff = []

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

print(min(high_diff))

Solution (Python):
nk = list(map(int, input().split()))
n = nk[0]
k = nk[1]
h = list()
for i in range(n):
    h.append(int(input()))
h.sort()
min_h = 10 ** 10
for i in range(n - k + 1):
    dis_h = h[i + k - 1] - h[i]
    if dis_h < min_h:
        min_h = dis_h
print(min_h)

Solution (Python):



 
import sys
import math



 

fileoperation=0
if(fileoperation):
    orig_stdout = sys.stdout
    orig_stdin = sys.stdin
    inputfile = open('W:/Competitive Programming/input.txt', 'r')
    outputfile = open('W:/Competitive Programming/output.txt', 'w')
    sys.stdin = inputfile
    sys.stdout = outputfile


mod=1000000007
 

def nospace(l):
    ans=''.join(str(i) for i in l)
    return ans
 
 
 

t=1
for tt in range(t):
    
    
    n,k= map(int, sys.stdin.readline().split(' '))
    h=[]

    for jj in range(n):
        p=int(input())
        h.append(p)

    ans=mod
    h.sort()

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

    
    

if(fileoperation):
    sys.stdout = orig_stdout
    sys.stdin = orig_stdin
    inputfile.close()
    outputfile.close()

Solution (Python):
ans=10**9
A=[]
n,k=[int(i) for i in input().split()]
for i in range(n):
    j=int(input())
    A.append(j)
A.sort(reverse=True)
for i in range(n-k+1):
    ans=min(ans,A[i]-A[i+k-1])
print(ans)

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

L.sort()
res = 10 ** 10
for i in range(N-K+1):
    res = min(L[i+K-1] - L[i], res)
print(res)

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

    l = []

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

    l.sort()

    m = 10**10
    for i in range(n-k+1):
        if l[i+k-1] - l[i] < m:
            m = l[i+k-1] - l[i] 
    print(m)

if __name__ == "__main__":
    main()

Solution (Python):
N,K = map(int,input().split())
L = []
for i in range(N) :
    l = int(input())
    L.append(l)
L = sorted(L)
ans = 10**9+1
for i in range(len(L)-K+1) :
    mi = L[i]
    ma = L[i+K-1]
    di = ma - mi
    if di < ans :
        ans = di
print(ans)

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

m=H[-1]
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=[]
for i in range(n):
    h.append(int(input()))
h.sort(reverse=True)
ans=h[0]-h[n-1]
for i in range(n-k+1):
    ans =min(ans,h[i]-h[i+k-1])
print(ans)

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

p.sort()
ans = 10 ** 20

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

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

h_sort = sorted(h, reverse=True)

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

print(max_res)

Solution (Python):




def solve():
    n, k = map(int, input().split())
    hl = [int(input()) for _ in range(n)]
    hl.sort()
    diff = hl[k - 1] - hl[0]
    for i in range(0, n - k + 1):
        td = hl[k - 1 + i] - hl[i]
        diff = min(td, diff)
    print(diff)


if __name__ == "__main__":
    solve()

Solution (Python):



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

h = [int(input()) for n in range(N)]

h.sort()

h_diff = [h[i+K-1] - h[i] for i in range(N-K+1)]

print(min(h_diff))

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

ans=1000000000
for j in range(N-K+1):
    if abs(A[j]-A[j+K-1])<ans:
        ans=abs(A[j]-A[j+K-1])
print(ans)

Solution (Python):
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)))

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

Solution (Python):

N, K = map(int, 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 = list(map(int, [input() for i in range(n)]))
h.sort()
res = 10**10
for i in range(k-1, n):
    res = min(res, h[i]-h[i-k+1])
print(res)

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

Solution (Python):
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))

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

h.sort()
ans = 100000000000000000000

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

print(ans)

Solution (Python):
def main():
    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):
        if m > h[i+k-1] - h[i]:
            m = h[i+k-1] - h[i]
    print(m)

if __name__ == "__main__":
    main()

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

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

Solution (Python):
def main():
    N, K = map(int, input().split())
    H = [int(input()) for _ in range(N)]
    H.sort()
    retval = float("inf")
    for i in range(N - K + 1):
        retval = min(H[i+K-1] - H[i], retval)
    print(retval)


if __name__ == "__main__":
    main()

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

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

print(ans)

Solution (Python):
n,k=[int(x) for x in input().split()]
h=[]
for _ in [0]*n:
    h.append(int(input()))
h=sorted(h)
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 = []
for i in range(n):
    x = int(input())
    l.append(x)
l.sort()
ans = 10**18
for i in range(k-1,n):
    ans = min(ans,l[i]-l[i-k+1])
print(ans)

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

hs.sort()

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

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())
h = []
for i in range(n):
    temp = int(input())
    h.append(temp)
h.sort()
i = 0
min = h[k-1]-h[0]
while i+k <= n:
    if min > (h[k+i-1]-h[i]):
        min = h[k+i-1]-h[i]
    i += 1
print(min)

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

num_list.sort()
beaut_list = [b-a for a, b in zip(num_list[:-K], num_list[K:])]

print(str(min(beaut_list)))

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

Solution (Python):
N, K = map(int, input().split())
H = [int(input()) for _ in range(N)]
H.sort()
ans = float('inf')
for i in range(N-K+1):
  d = H[i+K-1] - H[i]
  ans = ans if ans < d else d
print(ans)

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

Solution (Python):



import array
from bisect import *
from collections import *
import fractions
import heapq 
from itertools import *
import math
import random
import re
import string
import sys

N, K = map(int, input().split())
Hs = []
for n in range(N):
    Hs.append(int(input()))
Hs.sort()

diff_min = 1e10
for i in range(N - (K - 1)):
    j = i + (K - 1)
    x = Hs[i]
    y = Hs[j]
    mn = min(x, y)
    mx = max(x, y)
    diff = mx - mn
    if diff < diff_min:
        diff_min = diff
print(diff_min)

Solution (Python):
def C():
    N, K = map(int, input().split())
    h = [int(input()) for i in range(N)]
    h.sort()
    hmin = h[0]
    hmax = h[K - 1]
    ans = 100000000000000
    for i in range(K - 1, N):
        hmin = h[i - K + 1]
        hmax = h[i]
        if ans > hmax - hmin:
            ans = hmax - hmin
    print(ans)
C()

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

h.sort()

ans = 10000000000
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 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())
hl = []
for _ in range(n):
    hl.append(int(input()))
hl.sort()

res = 10**9
for i in range(n-k+1):
    hmin,hmax = hl[i],hl[i+k-1]
    res = min(res, hmax-hmin)

print(res)

Solution (Python):
n,k = list(map(int, input().split()))
h = []
for i in range(n):
  h.append(int(input()))
h = sorted(h)
m = []
for i in range(n):
  if i+k-1 < n:
    m.append(abs(h[i+k-1]-h[i]))
  else:
    break
print(min(m))

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

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

ans=10**20
for i in range(N-K+1):
    if ans > arr[i+K-1]-arr[i]:
        ans=arr[i+K-1]-arr[i]

print(ans)

Solution (Python):

def inpl(): return list(map(int, input().split()))
N, K = inpl()
H = sorted([int(input()) for _ in range(N)])

ans = 10**9
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 = []
for i in range(N):
    H.append (int(input()))

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

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

d = float('inf')
i = 0
while i + k - 1 < n:
    d = min(h[i + k - 1] - h[i], d)
    i += 1

print(d)

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

H.sort()
ans = sum(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=10**10
for i in range(K-1,N):
    if ANS>H[i]-H[i-K+1]:
        ANS=H[i]-H[i-K+1]
print(ANS)

Solution (Python):
def getN():
    return int(input())

def getMN():
    a = input().split()
    b = [int(i) for i in a]
    return b[0],b[1]

def getlist():
    a = input().split()
    b = [int(i) for i in a]
    return b

N, K  = getMN()

height = []
for i in range(N):
    height.append(getN())

height.sort()
ans = 10000000000
for i in range(N-K+1):

    if ans > abs(height[i] - height[i+K-1]):
        ans = abs(height[i] - height[i+K-1])

print(ans)

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

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

h.sort()
ans = float('inf')

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

print(ans)

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


N, K = [int(elem) for elem in fin().split()]
h_list = [int(fin()) for _ in range(N)]
h_list.sort()
min_dist = float('inf')
for i in range(N - K + 1):
    dist = h_list[i + K - 1] - h_list[i]
    min_dist = min(min_dist, dist)
print(min_dist)

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

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

h.sort()
sub = []

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

print(sub[0])