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

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

Solution (Python):
n,k = map(int,input().split())
tree = [int(input()) for _ in range(n)]
tree.sort()
ans = int(1e15)
for i in range(n-k+1):
    ans = min(tree[i+k-1] - tree[i], ans)
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):
N,K=map(int,input().split())
L=[]
for i in range(N):
	x=int(input())
	L.append(x)
L=sorted(L)
ans=float("inf")
for i in range(N-K+1):
	ans=min(ans,(L[i+K-1]-L[i]))
print(ans)

Solution (Python):
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from heapq import heapify, heappop, heappush
 
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
 
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N, K = MAP()
H = [INT() for i in range(N)]
H.sort()

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

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

Solution (Python):
def main():
    N, K = (int(i) for i in input().split())
    ans = 10**12
    H = [int(input()) for i in range(N)]
    H.sort()
    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 = [int(input()) for i in range(N)]
h.sort()
ans = max(h) - min(h)
for i in range(N-K+1):
    ans = min(ans, h[i+K-1] - h[i])
print(ans)

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

Solution (Python):
def examA():
    N = I()
    ans = ((N+1)//2)/N
    print(ans)
    return

def examB():
    N, K = LI()
    H = [I()for _ in range(N)]
    H.sort()
    ans = inf
    for i in range(N-K+1):
        cur = H[i+K-1]-H[i]
        if ans>cur:
            ans = cur
    print(ans)
    return

def examC():
    ans = 0
    print(ans)
    return

def examD():
    ans = 0
    print(ans)
    return

def examE():
    ans = 0
    print(ans)
    return

def examF():
    ans = 0
    print(ans)
    return

import sys,bisect,itertools,heapq,math,random
from copy import deepcopy
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
def I(): return int(readline())
def LI(): return list(map(int,readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf,alphabet,_ep
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18
_ep = 10**(-12)
alphabet = [chr(ord('a') + i) for i in range(26)]

sys.setrecursionlimit(10**7)

if __name__ == '__main__':
    examB()

"""
142
12 9 1445 0 1
asd dfg hj o o
aidn
"""

Solution (Python):
def main():
    n, k = map(int, input().split())
    h = [int(input()) for _ in range(n)]
    h.sort()
    answer = float("inf")
    for i in range(n - k + 1):
        answer = min(answer, h[i + k - 1] - h[i])
    print(answer)


if __name__ == '__main__':
    main()

Solution (Python):

import sys
import numpy as np

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N, K = lr()
H = np.array([ir() for _ in range(N)])
H.sort()
Hdiff = H[K-1:] - H[:-(K-1)]
answer = Hdiff.min()
print(answer)

Solution (Python):
def main():
    N, K = map(int, input().split())
    H = [int(input()) for i in range(N)]
    H.sort()
    mi = 10**9
    for i in range(N - K + 1):
        diff = H[i+K-1] - H[i]
        if diff < mi:
            mi = diff
    print(mi)
if __name__ == "__main__":
    main()

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 = sorted([int(input()) for i in range(N)])
mn = 10**9
for i in range(N-K+1):
    mn = min(mn, h[i+K-1]-h[i])
print(mn)

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

Solution (Python):


def solve():

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

    
    hlist.sort()
    l = []

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

    print(min(l))


if __name__ == "__main__":
    solve()

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N, K = MAP()
h = [INT() for _ in range(N)]
h.sort()

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

print(a)

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

h.sort()

left = h[0]
right = h[K - 1]
min_diff = right - left
for i in range(len(h)):
    if i <= K - 1: continue

    left = h[i - K + 1]
    right = h[i]
    diff = h[i] - left

    if diff < min_diff:
        min_diff = diff

print(min_diff)

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

ans = h[0]

for i, j in zip(h[:-K + 1], h[K - 1:]):
    div = i - j
    if div == 0:
        ans = 0
        break
    if div < ans:
        ans = div

print(ans)

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

min = 1000000000
for i in range(n-k+1):
  dif = array[i+k-1] - array[i]
  if min>dif:
    min = dif
    
print(min)

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

h.sort()

result = 10**9

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

Solution (Python):


nim, mike = map(int, input().split())
array = [int(input()) for _ in range(nim)]

array.sort()
m = sum(array)

for i in range(nim - mike + 1):
    m = min(array[i + mike - 1] - array[i], m)
print(m)

Solution (Python):
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(n-k+1)))

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

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

print(ans)

Solution (Python):
def cChristmas(n, k, hl):


    hl.sort()
    ans = 10**10

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

    return ans

def main():
    n, k = map(int, input().split())
    hl = [int(input()) for i in range(n)]
    print(cChristmas(n, k, hl))

if __name__ == '__main__':
    main()

Solution (Python):


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

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

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

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

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

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

print(ans)

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

Solution (Python):
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(n-k+1)]))

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

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

Solution (Python):
n,k = map(int,input().split())
li = []
for _ in range(n):
    i = int(input())
    li.append(i)
li.sort()
INF = float('inf')
diff = INF
for i,l in enumerate(li):
    if i + k - 1 > len(li)-1:
        break
    if li[i+k-1] - li[i] < diff:
        diff = li[i+k-1] - li[i]
print(diff)

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

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

Solution (Python):
import bisect,collections,copy,heapq,itertools,math,operator,string
def I(): return int(input())
def S(): return input()
def LI(): return list(map(int,input().split()))
def LS(): return list(input().split())

N,K = LI()
h = sorted([I() for _ in range(N)])
ans = float('INF')
for i in range(N-K+1):
    ans = min(h[i+K-1]-h[i],ans)
print(ans)

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

ans=1000000000000000

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

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

a = 0
b = 0
_min = 10 ** 9

h.sort()

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

print(_min)

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

minvalue = 10**9
h.sort()
for i in range(N-K+1):
    if abs(h[K-1+i]-h[i])<minvalue:
        minvalue = abs(h[K-1+i]-h[i])

print(minvalue)

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

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

Solution (Python):
def myAnswer(N:int,K:int,H:list) -> int:
   H.sort()
   ans = 10**9
   i = 0
   for j in range(K-1,len(H)):
      ans = min(ans,H[j] - H[i])
      i +=1
   return ans


def modelAnswer():
   tmp=1
def main():
   N, K = map(int,input().split())
   H = []
   for _ in range(N):
      H.append(int(input()))
   print(myAnswer(N,K,H[:]))

if __name__ == '__main__':
   main()

Solution (Python):
import numpy as np
import sys

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

Solution (Python):
N,K = map(int, input().split())
ary1 = sorted([int(input()) for x in range(N)])
min1 = 10**9+1
for i in range(K-1,N):
  dist1 = ary1[i]-ary1[i-K+1]
  if dist1 < min1:
    min1 = dist1
    
print(min1)

Solution (Python):
N,K = map(int, input().split())
ary1 = sorted([int(input()) for x in range(N)])
min1 = 10**9+1
for i in range(K-1,N):
  dist1 = ary1[i]-ary1[i-K+1]
  if dist1 < min1:
    min1 = dist1
    
print(min1)

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

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

h.sort()

m = h[-1] 

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

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()
ans=[]
for i in range(n-k+1):
    ans.append(h[i+k-1]-h[i])
print(min(ans))

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

H.sort()

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

print(ans)

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

ans=10**9
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())
k = []
for i in range(N):
  h = int(input())
  k.append(h)
k.sort()
k.reverse()
minimum = 10**10
for i in range(N-K+1):
  if k[i]-k[i+K-1] < minimum:
    minimum = k[i]-k[i+K-1]
print(minimum)

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

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

Solution (Python):
import sys
import numpy as np

n, k = map(int, sys.stdin.readline().split())
h = np.array(sys.stdin.read().split(), dtype=np.int64)
h.sort()

def main():
    res = np.amin(h[k-1:] - h[:-k+1])
    print(res)

if __name__ ==  '__main__':
    main()

Solution (Python):





mod = 10**9 + 7

def readInts():
  return list(map(int,input().split()))
def main():
    n,k = readInts()
    A = [int(input()) for _ in range(n)]
    A = sorted(A)
    min_ = float('inf')
    for i in range(n-k+1):
        min_ = min(min_,abs(A[i] - A[i+k-1]))
    print(min_)



if __name__ == '__main__':
  main()

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

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

a.sort(reverse=True)
s = 10000000000
for i in range(k, n + 1):
    s = min(a[i - k] - a[i - 1], s)

print(s)

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

print(ans)

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

Solution (Python):
n, k = map(int, input().split())
ans = 10**9

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

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

print(ans)

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

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

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

print(ans)

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

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

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

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

h.sort()
dif_min =max(h)
for i in range(n-k+1):
    dif = h[i+k-1]-h[i]
    
    if dif < dif_min:
        dif_min = dif

print(dif_min)

Solution (Python):
def ii():return int(input())
def iim():return map(int,input().split())
def iil():return list(map(int,input().split()))
def ism():return map(str,input().split())
def isl():return list(map(str,input().split()))

n,k = iim()
H = []
for _ in range(n):
    H.append(ii())
H.sort()

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

Solution (Python):
import sys
from collections import Counter
from itertools import accumulate
def input(): return sys.stdin.readline().rstrip()
def main():
    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)
if __name__=='__main__':
    main()

Solution (Python):
import sys
from collections import Counter
from itertools import accumulate
def input(): return sys.stdin.readline().rstrip()
def main():
    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)
if __name__=='__main__':
    main()

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

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

print(ans)

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

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

Solution (Python):

from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def S(): return input().rstrip()
def LS(): return S().split()
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 solve():
    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



if __name__ == '__main__':
    solve()

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

for i in range(len(h)-k+1):
    if h[i+k-1] - h[i] < ans:
        ans = h[i+k-1] - h[i]
        if ans == 0:
            break

print(ans)

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

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

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

h.sort()

ans = float("inf")

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

print(ans)

Solution (Python):
import heapq

def main():
    n,k = map(int,input().split())
    tree_heights = []

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

    mn = 1000000000000000000000000000000
    for i in range(n-k+1):
        
        mn = min(abs(tree_heights[i]-tree_heights[i+k-1]),mn)
    print(mn)

    
    
    





if __name__ == '__main__':
    main()

Solution (Python):

url = "https://atcoder.jp//contests/abc115/tasks/abc115_c"

def main():
    tree_n, sl = list(map(int, input().split()))
    trees = [int(input()) for i in range(tree_n)]
    trees.sort()
    tmp = trees[:sl]
    print(min(trees[i + sl - 1] - trees[i] for i in range(tree_n - sl + 1)))


if __name__ == '__main__':
    main()

Solution (Python):
n,k = map(int,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=10**10
for i in range(n-k+1):
    ans=min(H[i+k-1]-H[i],ans)
print(ans)

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

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

main()

Solution (Python):
from sys import stdin

N, K = [int(x) for x in stdin.readline().rstrip().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)):
    if h[i+(K-1)]-h[i] < ans:
        ans = h[i+(K-1)]-h[i]
print(ans)

Solution (Python):

N, K = map(int, input().split())
h = [int(input()) for 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())
hlist = sorted([int(input())for i in range(n)])

ans = 10**9
i = 0

while i+(k-1) <= n-1:
  ans = min(ans, hlist[k+i-1]-hlist[i])
  i += 1

print(ans)

Solution (Python):
from collections import Counter

N, K = list(map(int,input().split()))
H = [int(input()) for _ in range(N)]
H = sorted(H,reverse = False)

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

Solution (Python):
import sys

readline = sys.stdin.readline
read = sys.stdin.read
n, k = map(int, readline().split())
h = list(map(int, read().split()))
h.sort()
print(min(h[i+k-1] - h[i] for i in range(n-k+1)))

Solution (Python):

import sys,os
from collections import defaultdict, deque
from fractions import gcd
from math import ceil, floor
sys.setrecursionlimit(10**6)
write = sys.stdout.write
dbg = (lambda *something: print(*something)) if 'TERM_PROGRAM' in os.environ else lambda *x: 0
def main(given=sys.stdin.readline):
    input = lambda: given().rstrip()
    LMIIS = lambda: list(map(int,input().split()))
    II = lambda: int(input())
    XLMIIS = lambda x: [LMIIS() for _ in range(x)]
    YN = lambda c : print('Yes') if c else print('No')
    MOD = 10**9+7

    N,K = LMIIS()
    H = [0]*N
    for i in range(N):
        H[i] = II()
    H.sort(reverse=True)
    mindiff = 10**9
    for i in range(N-K+1):
        mindiff = min(H[i] - H[i+K-1], mindiff)
    print(mindiff)

if __name__ == '__main__':
    main()

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

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

h.sort()

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

print(min_d)

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

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

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

INF = 10**9+1

h.sort()
ans = INF

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

print(ans)

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

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

for i in range(n-k+1):
  temp = h[i+k-1] - h[i]
  if min_temp > temp:
    min_temp = temp
    
print(min_temp)

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

H.sort()

ans = 1e+9

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

print(ans)

Solution (Python):
import numpy as np
N, K = list(map(int,input().split()))
a = [int(input()) for i in range(N)]
a = sorted(a, reverse = True)
ans = np.zeros(len(a)-K+1)
for i in range(len(a)-K+1):
    ans[i] = a[i]-a[i+K-1]
print(int(ans.min()))

Solution (Python):
from collections import deque
from collections import Counter
from itertools import product, permutations,combinations
from operator import itemgetter
from heapq import heappop, heappush
from bisect import bisect_left, bisect_right, bisect



import numpy as np
from fractions import gcd
from math import ceil,floor, sqrt, cos, sin, pi, factorial
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10**8)
INF = float('inf')
MOD = 10**9+7

def main():
  n, k = map(int, readline().split())
  h = list(int(readline()) for i in range(n))
  h.sort()
  ans = INF
  for i in range(n-k+1):
    ans = min(ans,h[i+k-1]-h[i])
  print(ans)
  
  
if __name__ == '__main__':
  main()

Solution (Python):

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

t = []

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

Solution (Python):
from itertools import combinations

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

'''
res = 10**10
for i in combinations(h,K):
    tmp = max(i) - min(i)
    res = min(res,tmp)
'''

d = sorted(h)
res = 1e+9
for i in range(N-K+1):
    res = min(res,d[i+K-1] - d[i])
print(res)

Solution (Python):
N,K = map(int,input().split())
H = sorted([int(input()) for n in range(N)])
a = 1e+9

for n in range(N-K+1):
  a = min(a,H[n+K-1]-H[n])

print(a)

Solution (Python):
import math
N,K=map(int,input().split())
tree=[]
ans=0
for i in range(N):
    tree.append(int(input()))
tree.sort()

for i in range(N-K+1):
    if i==0:
        ans=tree[K+i-1]-tree[i]
        
    elif ans>=tree[K+i-1]-tree[i]:
        ans = tree[K + i - 1] - tree[i]
        
print(ans)

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

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

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

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

diff = float('inf')
s = 0
e = k - 1
while e < n:
    d = tree[e] - tree[s]
    diff = min(diff, d)
    s += 1
    e += 1
print(diff)

Solution (Python):


import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

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

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

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

hs.sort()

value_list = []

for i in range(len(hs)):
    try:
        value_list.append(hs[i+k-1] - hs[i])
    except:
        break

print(min(value_list))

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

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

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

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

print(min(h[:N-K+1]))

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

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