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())
li = []
for _ in range(N):
    li.append(int(input()))
li.sort()
kaisu = N-K+1
li2 = []
for i in range(kaisu):
    dist = li[i+K-1] - li[i]
    li2.append(dist)
print(min(li2))

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

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

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

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

Solution (Python):

n, k = [int(el) for el in input().split(' ')]
H = sorted([int(input()) for i in range(n)])

min_d = 10**9
for i in range(n-k+1):
    min_d = min(min_d, H[i+k-1] - H[i])
print(min_d)

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

ans = 10**9+1
for i in range(n-k+1):
  d = l[i+k-1]-l[i]
  ans = min(ans, d)
print(ans)

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

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

print(min(b))

Solution (Python):
import sys

input = sys.stdin.readline

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

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

Solution (Python):
n, k = map(int, input().split())
H = [int(input()) for _ in range(n)]
H.sort()
d = []
for i in range(1,n):
  d.append(abs(H[i]-H[i-1]))
S = [0]
for i in d:
  S.append(S[-1]+i)
min_num = 10**9
for i in range(k-1,n):
  min_num = min(min_num, abs(S[i]-S[i-k+1]))
print(min_num)

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

Solution (Python):
import sys
from operator import itemgetter as ig
from collections import defaultdict as dd


args = None
INF = float("inf")
MOD = int(1e9 + 7)
def int1(n):
    return int(n) - 1
def input():
    return next(args)
def parse(*params):
    if len(params) == 1:
        return params[0](next(args))
    return tuple(p(v) for p, v in zip(params, next(args).split()))
def debug(*v):
    if __debug__:
        print(*v, file=sys.stderr)


def main():
    N, K = parse(int, int)
    H = [parse(int) for _ in range(N)]

    h = sorted(H)

    diff_h = tuple(h[l + K - 1] - h[l] for l in range(N - K + 1))

    print(min(diff_h))

if __name__ == '__main__':
    args = iter(sys.stdin.read().split("\n"))
    main()

Solution (Python):
import sys
from operator import itemgetter as ig
from collections import defaultdict as dd


args = None
INF = float("inf")
MOD = int(1e9 + 7)
def int1(n):
    return int(n) - 1
def input():
    return next(args)
def parse(*params):
    if len(params) == 1:
        return params[0](next(args))
    return tuple(p(v) for p, v in zip(params, next(args).split()))
def debug(*v):
    if __debug__:
        print(*v, file=sys.stderr)


def main():
    N, K = parse(int, int)
    H = [parse(int) for _ in range(N)]

    h = sorted(H)
    l, r = 0, K

    sum_h = [0]
    for n in range(1, N):
        sum_h += [sum_h[-1] + (h[n] - h[n - 1])]
    debug(sum_h)

    diff_h = tuple(sum_h[l + K - 1] - sum_h[l] for l in range(N - K + 1))
    debug(diff_h)
    print(min(diff_h))

if __name__ == '__main__':
    args = iter(sys.stdin.read().split("\n"))
    main()

Solution (Python):
def solve():
    n, k = map(int, input().split())
    h = sorted(list(int(input()) for _ in range(n)))
    return min(h[i + k - 1] - h[i] for i in range(n - k + 1))


if __name__ == "__main__":
    print(solve())

Solution (Python):
N, K = map(int, input().split())
hs = sorted([int(input()) for _ in range(N)], reverse=True)
             
ans = 10 ** 10

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

Solution (Python):
import math
import sys
from itertools import accumulate
from functools import reduce
import queue
from fractions import gcd

def lcm(a, b):
    return a * b // gcd(a, b)

def combination_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

def permutations_count(n, r):
    return math.factorial(n) // math.factorial(n - r)

sys.setrecursionlimit(10000)

mod = 1000000007

"""

a = 100
b = 0.987654321
print("{0:06d}-{1:6f}".format(a,b))
000100-0.987654


char_list = list("abcd") 



    = sys.stdin.readline().rstrip()
    = list(sys.stdin.readline().rstrip())


    = int(sys.stdin.readline())
    = map(int, sys.stdin.readline().split())
    = list(map(int, sys.stdin.readline().split()))
    = list(map(int, sys.stdin.readlines()))
    = [list(map(int,list(sys.stdin.readline().split()))) for i in range(h)]  
"""

if __name__ == "__main__":
    n,k= map(int, sys.stdin.readline().split())
    h = list(map(int, sys.stdin.readlines()))
    h.sort()
    
    ans=10**9
    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 = list(int(input()) for _ in range(n))

h.sort(reverse = True)
answer = h[0] - h[k-1]

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

print(answer)

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

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

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

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

h.sort()
dif = h[k-1] - h[0]

for i in range(k, n):
    temp = h[i] - h[i-k+1]
    if temp < dif:
        dif = temp

print(dif)

Solution (Python):
def resolve():
    N,k = map(int,input().split())
    K = k-1
    H = list(int(input()) for _ in range(N))
    H.sort()
    minh = 10**10
    for i in range(K,N):
        minh = min(minh,abs(H[i-K]-H[i]))
    print(minh)
resolve()

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):
    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()
a=10**9
for i in range(N-K+1):
        if h[i+K-1]-h[i]<a:
                a=h[i+K-1]-h[i]
print(a)

Solution (Python):

import sys


def solve(N: int, K: int, h: "List[int]"):
    h.sort()

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

    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    K = int(next(tokens))  
    h = [ int(next(tokens)) for _ in range(N) ]  
    solve(N, K, h)

if __name__ == '__main__':
    main()

Solution (Python):

import sys


def solve(N: int, K: int, h: "List[int]"):
    avg = sum(h)/N

    h.sort()

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

    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    K = int(next(tokens))  
    h = [ int(next(tokens)) for _ in range(N) ]  
    solve(N, K, h)

if __name__ == '__main__':
    main()

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

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

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

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

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

h.sort()

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

print(ans)

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

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

h.sort()

mi = 10 ** 9
temp = 10 ** 9

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

print(mi)

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

h.sort()

m = 10**9
for i in range(n - k + 1):
  m = min(h[i + k - 1] - h[i], m)

print(m)

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

Solution (Python):
import itertools
import collections


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

n,k=getints()
h=list(sorted([getint() for _ in range(n)]))

res = 1 << 30
dq = collections.deque()
for v in h:
    dq.append(v)
    if len(dq) == k:
        res = min(res, dq[-1] - dq[0])
        dq.popleft()

print(res)

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

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

Solution (Python):
import math
import sys
import itertools
import queue
from fractions import gcd

def lcm(a, b):
    return a * b // gcd(a, b)

def combination(n, r):
    return factorial(n) // (factorial(n - r) * factorial(r))

sys.setrecursionlimit(10000)

mod = 1000000007

"""

a = 100
b = 0.987654321
print("{0:06d}-{1:6f}".format(a,b))
000100-0.987654


char_list = list("abcd") 



    = sys.stdin.readline().rstrip()
    = list(sys.stdin.readline().rstrip())


    = int(sys.stdin.readline())
    = map(int, sys.stdin.readline().split())
    = list(map(int, sys.stdin.readline().split()))
    = [list(map(int,list(sys.stdin.readline().split()))) for i in range(h)]  
"""

if __name__ == "__main__":
    N, M = map(int, sys.stdin.readline().split())
    L = sorted([ int(sys.stdin.readline()) for _ in range(N)])
    

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

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

hList = sorted(hList)

ansList = [0]*(N-K+1)

for i in range(N-K+1):
    ansList[i] = hList[i+K-1] - hList[i]

print(min(ansList))

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

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

print(min)

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

x = sorted(h)
ans = min(b - a for a, b in zip(x, x[(K - 1):]))

print(ans)

Solution (Python):
n, k = map(int,input().split())
h = [int(input()) for _ in range(n)]
h = sorted(h)
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 = list(map(int, input().split()))
hhh = [int(input()) for _ in range(n)]
hhh.sort()
ans = float("inf")
for i in range(n-k+1):
  ans = min(ans, hhh[i+k-1] - hhh[i])
print(ans)

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

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

h = []
diff = 10000000000

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

h = sorted(h)

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

print(diff)

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

Solution (Python):
import itertools
min_all = 1000000000000000000000000
    
N,K = [int(i) for i in input().split(" ")]
H = [int(input()) for _ in range(N)]
H.sort()
    
for i in range(N+1-K):
  min_now = H[K-1+i] - H[i]
  if min_all > min_now:
    min_all = min_now
print(min_all)

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

answer = 10 ** 10

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

print(answer)

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

Solution (Python):
n1, n2 = [int(i) for i in input().split()]

n3 = []

for i1 in range(n1):
    n3.append(int(input()))

n3.sort(reverse=True)

m1 = 1000000000
t1 = 0
for i2 in range(n1 - n2 + 1):
    t1 = n3[i2] - n3[i2 + n2 - 1]
    if t1 < m1:
        m1 = t1
print(m1)

Solution (Python):


def main():

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

    hList.sort()
    it = iter(hList)
    diffList = []
    bady = K-1


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

        diff = hList[i]-hList[i+bady]
        diffList.append(abs(diff))
    

    print(min(diffList))







    
if __name__== "__main__":
    main()

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

h = sorted(h)
ans = 10 ** 9 + 1
for i in range(len(h) - (k-1)):
    ans = min(ans, h[i+k-1] - h[i])
print(ans)

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

heights.sort()

diffs = []
for i in range(N-K+1):
    diffs.append(heights[i+K-1]-heights[i])
    
print(min(diffs))

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

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

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

print(ans)

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

m_dfr = 9999999999999999

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

H = sorted(H, reverse = True)


for i in range(0, N - K + 1):
	
	if m_dfr > H[i] - H[i + K - 1]:
		m_dfr = H[i] - H[i + K - 1]

print(m_dfr)

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

ans = h[-1]

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

h.sort()

ans_list = []

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

ans = min(ans_list)
print(ans)

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

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

def main():
	n, k = map(int, input().strip().split())
	h = [0] * n
	for i in range(n):
		h[i] = int(input().strip())
	H = sorted(h)
	ans = 10**9
	for i in range(n-k+1):
		ans = min(ans, H[i+k-1]-H[i])
	print(ans)


if __name__ == '__main__':
	main()

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

H = sorted(int(input().strip()) for i in range(n))
min_diff = 10e9 + 5

cumsum = []
for h in H:
  cumsum.append(h)
  if len(cumsum) >= k:
    min_diff = min(min_diff, cumsum[-1] - cumsum[-k])

print(min_diff)

Solution (Python):
a, b = map(int, input().split())
c = [int(input()) for _ in range(a)]
c.sort()
d = 10 ** 9
for i in range(b-1, a):
    d = min(d, abs(c[i] - c[i-b+1]))
print(d)

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

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

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

Hs.sort()

ans = 10 ** 9

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

print(ans)

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

Solution (Python):
from copy import deepcopy
from sys import exit,setrecursionlimit
import math
from collections import defaultdict,Counter,deque
from fractions import Fraction as frac

setrecursionlimit(1000000)


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

def zip(a):
	mae = a[0]
	ziparray = [mae]
	for i in range(1,len(a)):
		if(mae != a[i]):
			ziparray.append(a[i])
			mae = a[i]
	return ziparray

def is_prime(n):
	if n < 2: return False

	for k in range(2, int(math.sqrt(n)) + 1):
		if n % k == 0:
			return False

	return True

def list_replace(n,f,t):
	return [t if i==f else i for i in n]

def base_10_to_n(X, n):
	X_dumy = X
	out = ''
	while X_dumy>0:
		out = str(X_dumy%n)+out
		X_dumy = int(X_dumy/n)
	if(out == ''): return '0'
	return out

def gcd(l):
	x = l.pop()
	y = l.pop()
	while(x%y!=0):
		z = x%y
		x = y
		y = z
	l.append(min(x,y))
	return gcd(l) if len(l) > 1 else l[0]

class Queue():
	def __init__(self):
		self.q = deque([])
	def push(self,i):
		self.q.append(i)
	def pop(self):
		return self.q.popleft()
	def size(self):
		return len(self.q)
	def debug(self):
		return self.q

class Stack():
	def __init__(self):
		self.q = []
	def push(self,i):
		self.q.append(i)
	def pop(self):
		return self.q.pop()
	def size(self):
		return len(self.q)
	def debug(self):
		return self.q

class graph():
	def __init__(self):
		self.graph = defaultdict(list)
	def addnode(self,l):
		f,t = l[0],l[1]
		self.graph[f].append(t)
		self.graph[t].append(f)
	def rmnode(self,l):
		f,t = l[0],l[1]
		self.graph[f].remove(t)
		self.graph[t].remove(f)
	def linked(self,f):
		return self.graph[f]

class dgraph():
	def __init__(self):
		self.graph = defaultdict(set)
	def addnode(self,l):
		f,t = l[0],l[1]
		self.graph[f].append(t)
	def rmnode(self,l):
		f,t = l[0],l[1]
		self.graph[f].remove(t)
	def linked(self,f):
		return self.graph[f]

main()

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

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

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

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

h.sort()

ans = 10**100

k -= 1

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

Solution (Python):
N,K = map(int, input().split())
List = []
for i in range(N):
    List.append(int(input()))
    
List.sort()
ans_list = []
for i in range(N-K+1):
    ans_list.append(List[i+K-1]-List[i])

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

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

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

print(ans)

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

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

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

Solution (Python):
import sys

sys.setrecursionlimit(4100000)


def inputs(num_of_input):
    ins = [input() for i in range(num_of_input)]
    return ins


def solve(N, K, inputs):
    trees = list(map(lambda x: int(x), inputs))
    trees.sort()

    min_diff = -1
    for i in range(K-1, N):
        diff = trees[i] - trees[i - K + 1]
        if min_diff == -1 or diff < min_diff:
            min_diff = diff
    return min_diff


def string_to_int(string):
    return list(map(lambda x: int(x), string.split()))


if __name__ == "__main__":
    [N, K] = string_to_int(input())
    ret = solve(N, K, inputs(N))
    print(ret)

Solution (Python):

from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
import itertools
sys.setrecursionlimit(10**5)
stdin = sys.stdin
bisect_left = bisect.bisect_left
bisect_right = bisect.bisect_right
def LI(): return list(map(int, stdin.readline().split()))
def LF(): return list(map(float, stdin.readline().split()))
def LI_(): return list(map(lambda x: int(x)-1, stdin.readline().split()))
def II(): return int(stdin.readline())
def IF(): return float(stdin.readline())
def LS(): return list(map(list, stdin.readline().split()))
def S(): return list(stdin.readline().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')


def A():
    d = II()
    print("Christmas",end="")
    for _ in range(25 - d):
        print(" Eve", end="")
    print()
    return


def B():
    n = II()
    p = IR(n)
    p.sort()
    print(sum(p) - p[-1] // 2)
    return


def C():
    n, k = LI()
    h = IR(n)
    h.sort()
    ans = inf
    for i in range(n - k + 1):
        ans = min(ans, h[i + k - 1] - h[i])
    print(ans)
    return


def D():
    return


if __name__ == '__main__':
    C()

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

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

print(res)

Solution (Python):

import sys


def solve(N: int, K: int, h: "List[int]"):
    h = sorted(h)
    mn = 1e9
    for i in range(N-K+1):
        if mn > h[i+K-1] - h[i]:
            mn = h[i+K-1] - h[i]
    print(mn)
    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    K = int(next(tokens))  
    h = [ int(next(tokens)) for _ in range(N) ]  
    solve(N, K, h)

if __name__ == '__main__':
    main()

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

mind = 10 ** 9 + 1

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 _ in range(N)]

h.sort()

ans = 1.0e+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())
tree_dict = {}
tree_list = []
for i in range(N):
    a = int(input())
    tree_list.append(a)

tree_list = sorted(tree_list)
min_count = 10 ** 9
max_num = 0
min_num = 0


for i in range(len(tree_list) - K + 1):
    max_num = tree_list[i + K - 1]
    min_num = tree_list[i]
    if max_num - min_num < min_count:
        min_count = (max_num - min_num)

print(min_count)

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

trees.sort()

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

print(m)

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

Solution (Python):
import itertools

def q49(n, k, h):
    h.sort()
    a = [h[i+k-1]-h[i] for i in range(n-k+1)]
    return min(a)
            
N, K = [int(i) for i in input().split()]
h = []
for _ in range(N):
    h.append(int(input()))
print(q49(N, K, h))

Solution (Python):
def solve():
    N,K = [int(s) for s in input().split() if s]
    h = [int(input()) for _ in range(N)]
    h = sorted(h)
    result = 10**9+1
    
    for i in range(N-K+1):
        diff = h[i+K-1]-h[i]
        
        if result > diff:
            result = diff
    print(result)
    
solve()

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

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

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

ans = 3e9
for k in range(N - K + 1):
    left = H[k]
    right = H[k + K - 1]
    ans = min(ans, right - left)
print(ans)

Solution (Python):
import math
from functools import reduce
from collections import deque
import sys
sys.setrecursionlimit(10**7)

def s(generator, splitter, mapper):
    return [ mapper(s) for s in generator().split(splitter) ]


def get_nums_l():
    return [ int(s) for s in input().split(" ")]


def get_nums_n(n):
    return [ int(input()) for _ in range(n)]

n,k = get_nums_l()
hs = sorted(map(int, sys.stdin.readlines()))

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

print(ans)

Solution (Python):


import sys

tmp = input().split()
hoge = list(map(lambda a: int(a), tmp))
n = hoge[0]
k = hoge[1]

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

h.sort()




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


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

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

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

height = []
for i in range(N):
  height.append(int(input()))
                
height.sort()
dif = [0]*(N-1)
for i in range(N-1):
  dif[i] = height[i+1]-height[i]

diflist = [dif[0]]*(N-1)
for i in range(1,N-1):
  diflist[i] = diflist[i-1] + dif[i]

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

Solution (Python):


input=open(0).readline

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

h.sort()
ans=float('inf')
for i in range(n-(k-1)):
    res=h[i+k-1]-h[i]
    ans=min(ans,res)

print(ans)

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

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

h.sort()

tmp = 10 ** 10

for i in range(n):
    if i + k - 1 >= len(h):
        break
    tmp = min(tmp, h[i + k - 1] - h[i])

print(tmp)

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

print (ans)

Solution (Python):
n, k = map(int, input().split())
ls = sorted([int(input()) for _ in range(n)])
ans = float('inf')

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

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

df=[]

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

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

h_sorted = sorted(h,reverse=True)
min_diff = 10**10

for i in range(len(h)-k+1):
  diff = h_sorted[i] - h_sorted[i+k-1]
  if min_diff > diff:
    min_diff = diff  

print(min_diff)

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

H.sort()
ans = H[N-1] - H[0]
for i in range(N-K+1):
    min_h = H[K+i-1] - H[i]
    if min_h < ans:
        ans = min_h

print(ans)

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

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

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


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

a.sort()

min=a[N-1]-a[0]
for l in range(N-K+1):
    if min>a[l+K-1]-a[l]:
        min=a[l+K-1]-a[l]

print(min)

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

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

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

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

li = [int(input()) for _ in range(n)]
li.sort()
ans = li[-1]
for i in range(n-k+1):

    if ans > li[i+k-1] - li[i]:
        ans = li[i+k-1] - li[i]

print(ans)

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

H = sorted(H)
min_diff = float('inf')
for i in range(0, N-K+1):
    diff = H[i+K-1] - H[i]
    if diff < min_diff:
        min_diff = diff
print(min_diff)

Solution (Python):
a, b = map(int, input().split())
c = [int(input()) for _ in range(a)]
c.sort()
d = 10 ** 9
for i in range(b-1, a):
    d = min(d, abs(c[i] - c[i-b+1]))
print(d)

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

h.sort()

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