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 = [int(input()) for i in range(n)]
h = sorted(h)
ans = 10**9
for i in range(n-k+1):
  num = (h[i+(k-1)]-h[i])
  if num <= ans:
    ans = num
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):
import sys
import numpy as np

stdin = sys.stdin
def ns(): return stdin.readline().rstrip()
def ni(): return int(stdin.readline().rstrip())
def nm(): return map(int, stdin.readline().split())
def nl(): return list(map(int, stdin.readline().split()))


def main():
    n, k = nm()
    H = [ni() for _ in range(n)]
    H = np.array(sorted(H))
    dif = H[1:] - H[:-1]
    cum_dif = np.append(0, np.cumsum(dif))
    ans = cum_dif[k - 1:] - cum_dif[:-(k - 1)]
    print(np.min(ans))


if __name__ == '__main__':
    main()

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

H.sort()

res = H[-1] - H[0]

for i in range(N - K + 1):
    t1 = H[i]
    t2 = H[i + K - 1]
    res = min(res, abs(t2 - t1))

print(res)

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

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

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

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

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

res = min(l)
print(res)

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

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

MIN=h[-1]-h[0]

for n in range(K-1,N):
    MIN=min(MIN,h[n]-h[n-K+1])
print(MIN)

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

Solution (Python):
def solve():
  N, K = map(int, input().split())
  H = [int(input()) for _ in range(N)]
  H.sort()
  ans = float('inf')
  for i in range(N):
    if i+K>N:
      break
    ans = min(H[i+K-1]-H[i],ans)
  return ans
print(solve())

Solution (Python):
n,k=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())
l=sorted(int(input()) for _ in range(n))
a=10**9
for i in range(n-k+1):
  a=min(a,l[i+k-1]-l[i])
print(a)

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

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

Solution (Python):
def resolve():
    n, k = map(int, input().split())
    H_sort = list(sorted([int(input()) for _ in range(n)],reverse=True))
    ans = 10**9
    for i in range(n-k+1):
        ans = min(ans, H_sort[i]-H_sort[i+k-1])
    print(ans)
resolve()

Solution (Python):
def resolve():
    n, k = map(int, input().split())
    H = []
    for _ in range(n):
        H.append(int(input()))
    H_sort = list(sorted(H, reverse=True))
    ans = 10**9
    for i in range(n-k+1):
        ans = min(ans, H_sort[i]-H_sort[i+k-1])
    print(ans)
resolve()

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

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

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

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

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

Solution (Python):
N,K = list(map(int,input().split()))
h = []
for i in range(N):
    h.append(int(input()))
sorted_h = sorted(h)
minDelta = 10 ** 10
for i in range(N-K+1):
    delta = sorted_h[i+K-1] - sorted_h[i]
    if minDelta > delta:
        minDelta = delta
print(minDelta)

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

Solution (Python):
num, sel = map(int, input().split(" "))
a = sorted(list((int)(input()) for i in range(num)))
mini = 10 ** 9
for i in range(num - sel + 1):
  if mini > a[i + sel - 1] - a[i]:
    mini = a[i + sel - 1] - a[i]
print(mini)

Solution (Python):
def main():
    n, k = map(int, input().split())
    inlis = list()
    for _ in range(n):
        h = int(input())
        inlis.append(h)
    inlis.sort()
    ans = 10 ** 10
    for i in range(n-k+1):
        tmp = inlis[i+k-1] - inlis[i]
        if tmp < ans:
            ans = tmp
    print(ans)
            



if __name__ == "__main__":
    main()

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

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

h=sorted(h)

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

Solution (Python):
import bisect, collections, copy, heapq, itertools, math, string, sys
input = lambda: sys.stdin.readline().rstrip() 
sys.setrecursionlimit(10**7)
INF = float('inf')
def I(): return int(input())
def F(): return float(input())
def SS(): return input()
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LSS(): return input().split()

def resolve():
    N, K = LI()
    h = [I() for _ 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)

if __name__ == '__main__':
    resolve()

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

tree_heights = []

for i in range(N):
    height = int(input())
    tree_heights.append(height)
    
tree_heights = sorted(tree_heights)

ans = 10**9

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

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

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

Solution (Python):
def main():
    n, k = map(int, input().split())
    H = [int(input()) for _ in range(n)]
    H.sort()
    ans = 10 ** 9 + 2
    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())
trees = [int(input()) for _ in range(n)]
trees.sort()
diff_trees_height = []

for i in range(n - (k - 1)):
    diff = abs(trees[i] - trees[i + k - 1])
    diff_trees_height.append(diff)
print(min(diff_trees_height))

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

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

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

print(min(A))

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






























































def calculation(lines):
    
    
    N, K = list(map(int, lines[0].split()))
    values = list()
    for i in range(N):
        values.append(int(lines[i+1]))
    
    
    values.sort()

    dif = values[K-1] - values[0]

    for i in range(1, N-K+1):
        tmp = values[K-1+i] - values[i]
        if dif > tmp:
            dif = tmp
    return [dif]



def get_input_lines():
    line = input()
    N, M = list(map(int, line.split()))
    lines = list()
    lines.append(line)
    for _ in range(N):
        lines.append(input())
    return lines



def get_testdata(pattern):
    if pattern == 1:
        lines_input = ['5 3', '10', '15', '11', '14', '12']
        lines_export = [2]
    if pattern == 2:
        lines_input = ['5 3', '5', '7', '5', '7', '7']
        lines_export = [0]
    return lines_input, lines_export



def get_mode():
    import sys
    args = sys.argv
    if len(args) == 1:
        mode = 0
    else:
        mode = int(args[1])
    return mode



def main():
    import time
    started = time.time()
    mode = get_mode()
    if mode == 0:
        lines_input = get_input_lines()
    else:
        lines_input, lines_export = get_testdata(mode)

    lines_result = calculation(lines_input)

    for line_result in lines_result:
        print(line_result)

    
    
    
    
    
    
    
    
    
    
    



if __name__ == '__main__':
    main()

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

Solution (Python):
import math

n, k = map(int, input().split(" "))
hs = sorted([int(input()) for _ in range(n)], reverse=True)

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

print(ans)

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):
  d = h[i+k-1] - h[i]
  ans = min(ans, d)
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)
hmin = max(h)
for i in range(N-K+1):
  hmin = min([hmin, h[i+K-1]-h[i]])
  
print(hmin)

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

for i in range(N):
    num = int(input())
    l.append(num)

ans = max(l) - min(l)
l.sort()

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

print(ans)

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

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

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

Solution (Python):
n, k = map(int, input().split())
H = [int(input()) for _ in range(n)]
ans = 1e9
H.sort()

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

print(ans)

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

def main():
    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)
            
if __name__ == '__main__':
    main()

Solution (Python):
n,k,*hh = map(int, open(0).read().split())

hh.sort()

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

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

print(ans)

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

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):
N,K = map(int,input().split())
h = []
for i in range(0,N):
    h.append(int(input()))
h.sort()
ans = 10**100
for i in range(0,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()))

hd = []
h.sort(reverse=True)
for i in range(N-K+1):
    hd.append(h[i]-h[i+K-1])

print(min(hd))

Solution (Python):
import math

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

print(ans)

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

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

Solution (Python):
import sys
import itertools

import time
import math
import heapq
from collections import defaultdict
sys.setrecursionlimit(10 ** 7)
 
INF = 10 ** 18
MOD = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines



N, X = map(int, input().split())
H = [0] * N
for i in range(N):
    H[i] = int(input())

H = sorted(H)

left = 0
right = X
ans = INF
while right <= N:
    mn = H[left]
    mx = H[right - 1]
    ans = min(ans, mx - mn)
    left += 1
    right += 1
print(ans)

Solution (Python):

N,K = map(int,input().split())
H=[None]*N

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

H.sort()

ans=10**9
for i in range(0,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 = 1000000000
for i in range(n-k+1):
    diff = h[i+k-1] - h[i]
    ans = min(ans, diff)
    
print(ans)

Solution (Python):
N, K = 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):
import sys
from collections import defaultdict

sys.setrecursionlimit(10**7)
input = sys.stdin.readline

n,k = map(int, input().split())
h = []
d = defaultdict(int)
cnt = []
for i in range(n):
    x = int(input())
    h.append(x)
    d[x] += 1

for i in d.items():
    if i[1] >= k:
        print(0)
        exit()

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

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

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

Solution (Python):
n,k=map(int,input().split())
a=[]
for x in range(n):
  a.append(int(input()))
ans=1000000009
a=sorted(a)
for y in range(n-k+1):
  ans=min(a[y+k-1]-a[y],ans)
  
print(ans)

Solution (Python):
n,k = map(int,input().split())
h = list(int(input()) for i in range(n))
h.sort()
p = max(h) - min(h)

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

print(p)

Solution (Python):


import copy
import random
import bisect 
import fractions 
import math
import sys
import collections
from decimal import Decimal 

mod = 10**9+7
sys.setrecursionlimit(mod) 

d = collections.deque()
def LI(): return list(map(int, sys.stdin.readline().split()))

N, K = LI()
h = [0 for i in range(N)]
for i in range(N):
    h[i] = int(input())

h.sort()
ans = mod
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()
m = float("inf")
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 = [int(input()) for _ in range(n)]
h.sort()

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

print(ans)

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

h.sort()
min1 = 10**9

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

print(min1)

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

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

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

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

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

Solution (Python):
def solve():
    n, k = map(int, input().split())
    s = []
    for i in range(n):
        s.append(int(input()))
    s.sort()
    res = 10 ** 10
    for i in range(n + 1 - k):
        tmp = s[i + k - 1] - s[i]
        res = min(res, tmp)
    print(res)

solve()

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

nums = [None] * (N-K+1)
for i in range(N-K+1):
    nums[i] = H[i+K-1] - H[i]

print(min(nums))

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

nums = [0]*(n-k+1)
for i in range(n-k+1):
  nums[i] = h[i+k-1] - h[i]
print(min(nums))

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

Solution (Python):

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

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

Solution (Python):
N,K = (int(X) for X in input().split())
H = sorted(int(input()) for X in range(0,N))
MIN = pow(10,9)
for T in range(0,N-K+1):
    D = H[T+K-1]-H[T]
    if D<MIN: MIN = D
print(MIN)

Solution (Python):

import sys
input = sys.stdin.readline
N,K = (int(i) for i in input().split())
h =[0]*N
for i in range(N):
    h[i] = int(input())
h.sort()


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

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

N,K = list(map(int,input().split()))
H = sorted([int(input()) 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 = map(int,input().split())

T = []

for i in range(N):
    t = int(input())
    T.append(t)

T.sort()

A = float("infinity") 
for i in range(N-K+1):
    a = T[i+K-1] - T[i]
    A = min(A,a)

print(A)

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

Solution (Python):

n, k, *h = map(int, open(0).read().split())
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):









import sys
input = sys.stdin.readline

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

Solution (Python):

import sys
from collections import deque, defaultdict
from math import sqrt, factorial





def solve():
    n, k = [int(x) for x in input().split()]
    a = []
    for _ in range(n):
        a.append(int(input()))
    a.sort()
    ans = float('inf')
    for i in range(n-k+1):
        ans = min(ans, a[i+k-1]-a[i])
    print(ans)
t = 1

for case in range(1,t+1):
    ans = solve()


"""

a aa c ca c a b a ab a b c

"""

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

Solution (Python):
N,K = map(int,input().split())
h=[]
for _ in range(N):
    h.append(int(input()))
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=[int(input())for _ in range(N)]
h.sort()
ans=10**10
for i in range(N-K+1):
	tmp=h[i+K-1]-h[i]
	if tmp<ans:
		ans=tmp
print(ans)

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

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

Solution (Python):
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
def s(): return input()
def k(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def X(): return list(input())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
def gcd(*numbers): reduce(math.gcd, numbers)
sys.setrecursionlimit(10 ** 9)
mod = 10**9+7
count = 0
ans = 0

N, K = I()
a = []
for i in range(N):
    x = k()
    a.append(x)

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

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

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

ans = 10**10
for i in range(N-K+1):
    ans = min(ans, a[i+K-1]-a[i])
    if ans == 0:
        print(0)
        sys.exit()
print(ans)

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

ans = 0
t = float("inf")
for i in range(n-k+1):
  if abs(h[i+k-1] - h[i]) < t:
    t = abs(h[i+k-1] - h[i])
    ans = i

print(h[ans+k-1] - h[ans])

Solution (Python):
import collections
n,k = map(int, raw_input().split(' '))
heights = [int(raw_input()) for _ in range(n)]
heights.sort()
q = collections.deque([])
r =  float('inf') 
for h in heights:
	q.append(h)
	if len(q) == k + 1: q.popleft()
	if k and len(q) == k:
		r =min(r, q[-1] - q[0])
print r if k else 0

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

Solution (Python):

def solve():
    Height = 10**9
    for i in range(0,N-K+1):
        hmax = h[i+K-1]
        hmin = h[i]
        Height = min(Height, hmax-hmin)



    print(Height)

if __name__=="__main__":
    N,K = list(map(int, input().split()))
    h = [int(input()) for _ in range(N)]
    h.sort()
    solve()

Solution (Python):
import numpy as np
N,K=map(int,input().split())
H=np.array([input() for _ in range(N)],dtype=np.int32)
K-=1
H.sort()
ans=(H[K:]-H[:-K]).min()
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())

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

M=10**10
H.sort()
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=[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):
INF = float("inf")

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

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

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

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

ans=10**10
for h in range(K-1,N):
    h_min=H[h-K+1]
    h_max=H[h]
    if ans > h_max-h_min:
        ans=h_max-h_min
print(ans)

Solution (Python):





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

    h = sorted(h)

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

    return diff


print(main())

Solution (Python):
N, K = 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())
T = [int(input()) for i in range(N)]

T.sort()
P = []
for i in range(N-K+1):
    P.append(T[i+K-1] - T[i])

print(min(P))

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

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

print(ans)

Solution (Python):
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline

in_n = lambda: int(readline())
in_nn = lambda: map(int, readline().split())
in_nl = lambda: list(map(int, readline().split()))
in_na = lambda: map(int, read().split())
in_s = lambda: readline().rstrip().decode('utf-8')


def main():

    N, K = in_nn()
    h = sorted(list(in_na()))

    ans = 10**9 + 7

    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()
print(min(h[i+K-1] - h[i] for i in range(N-K+1)))

Solution (Python):
import sys
input = sys.stdin.readline
maxa=10**10
L=list()
N,K=map(int,input().split())
for i in range(N):
  S=int(input())
  L.append(S)
L=sorted(L)
for i in range(N-K+1):
  maxa=min(maxa,L[i+K-1]-L[i])
print(maxa)

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


resolve()

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

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

H = sorted(H)
ans = H[K-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())
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**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 = []
for i in range(N):
    H.append(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):
def main():
    n,k=map(int,input().split())
    h=sorted(int(input()) for _ in range(n))
    hs = []
    for i in range(n-k+1):
        hs.append(h[i+k-1] - h[i])
    print(min(hs))
    
    
if __name__ == "__main__":
    main()

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

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

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


if __name__ == '__main__':
    solve()

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

if __name__ == '__main__':
    solve()

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

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

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

for i in range(1, N-K+1):
    new_h = H[i+K-1]-H[i]
    if new_h < min_h:
        min_h = new_h

print(min_h)

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

h.sort()

ans_l = list()
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 _ in range(N):
    h.append(int(input()))

h.sort()
ans = 10**10
for i in range(N-K+1):
    hmin = h[i]
    hmax = h[i+K-1]
    ans = min(ans, (hmax-hmin))
print(ans)

Solution (Python):
n, k = map(int, input().split())
hh = []
minn = 10**12
for i in range(n):
    h = int(input())
    hh.append(h)
h = sorted(hh)
for i in range(n - k + 1):
    if h[i + k - 1] - h[i] < minn:
        minn = h[i + k - 1] - h[i]
print(minn)

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

print(ans)

Solution (Python):


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

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