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

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

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

Solution (Python):
n, k = 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,*h = map(int,open(0).read().split())
h = sorted(h)
print(min([abs(h[i]-h[i+k-1]) for i in range(len(h)-k+1)]))

Solution (Python):
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    
    from itertools import combinations, permutations, accumulate, groupby
    
    from bisect import bisect_left,bisect_right
    from heapq import heapify, heappop, heappush
    from math import floor, ceil
    

    
    

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

if __name__ == '__main__':
    main()

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

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

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

Solution (Python):
import sys, math, itertools, collections, bisect
n,k=map(int,input().split())
h=[]
for i in range(n):
    tmp=int(input())
    h.append(tmp)
h.sort()
min=10000000000
for i in range(len(h)-k+1):
    tmp2=h[i+k-1]-h[i]
    if min>tmp2:
        min=tmp2
print(min)

Solution (Python):

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


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

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

Solution (Python):
n,k = list(map(int, input().split()))
h = [int(input()) for _ 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())
mylist=[]
temp=0
amount=9999999999
for i in range(N):
    mylist.append(int(input()))

mylist.sort()


for i in range(N-K+1):
    
    
    temp=mylist[i+K-1]-mylist[i]
    
    amount=min(temp,amount)
    
print(amount)

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

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

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

mindiff = float('inf')
for i in range(N-K+1):
    diff = sor[i+K-1] - sor[i] 
    if diff <= mindiff:
        mindiff = diff
print(mindiff)

Solution (Python):
def main():
    from builtins import int, map, list, float, print, min
    import sys
    sys.setrecursionlimit(10 ** 6)

    input = sys.stdin.readline
    input_list = (lambda: input().rstrip().split())
    input_number = (lambda: int(input()))
    input_number_list = (lambda: list(map(int, input_list())))

    N, K = input_number_list()
    h = [input_number() 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)


if __name__ == '__main__':
    main()

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

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

A = sorted(A)

t = A[-1]

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

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

h.sort()
ans = 10000000000
tmp = sum(h[:K])
for i in range(N - K + 1):
    ans = min(ans, h[i + K - 1] - h[i])
print(ans)

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


if __name__ == '__main__':
    main()

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

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

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

    ans.sort()

    print(ans[0])


if __name__ == '__main__':
    main()

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=[]
for i in range(N):
    h=int(input())
    H.append(h)
H.sort()
i=0
ans=max(H)-min(H)+1
while i+K-1<len(H):
    min_ans=H[i+K-1]-H[i]
    if ans>min_ans:
        ans=min_ans
    i=i+1
print(ans)

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

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

Solution (Python):
import sys
from bisect import *
from heapq import *
from collections import *
from itertools import *
from functools import *
from math import *
from fractions import *

sys.setrecursionlimit(100000000)
input = lambda: sys.stdin.readline().rstrip()

def main():
    N, K = map(int, input().split())
    h = sorted(int(input()) for _ in range(N))
    print(min(j - i for i, j in zip(h, h[K - 1:])))

main()

Solution (Python):
n,k=map(int,input().split())
a=[int(input()) for i in range(n)]
a=sorted(a)
ans=10**9
for i in range(k-1,n):
    tmp=a[i]-a[i-(k-1)]
    ans=min(ans,tmp)
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):
    t=h[i+k-1]-h[i]
    ans=min(t,ans)
print(ans)

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

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

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

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

i = 0
ans = 10 ** 9
while i + K - 1 < N:
  n = abs(h[i+K-1] - h[i])
  if ans > n: ans = n
  i += 1
print(ans)

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

tree = []
for _ in range(n):
    tree.append(int(input()))

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

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

Solution (Python):
N, K = map(int, input().split())
trees = list(int(input()) for _ in range(N))
trees.sort()
ans = float("inf")
for i in range(N - K + 1):
    tmp_ans = trees[i + K - 1] - trees[i]
    if tmp_ans < ans:
        ans = tmp_ans
print(ans)

Solution (Python):

import sys
sys.setrecursionlimit(10000000)
INF = 1<<32


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

    h.sort()
    ans = INF

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

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

h.sort()
ans = 10 ** 5 * 10 ** 10
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()
min_ = float('inf')
for i in range(n-k+1):
    min_ = min(H[i+k-1]-H[i], min_)
print(min_)

Solution (Python):

import sys
input = sys.stdin.readline

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

min_diff = 10**9
for hl, hr in zip(h, h[k-1:]):
    min_diff = min(min_diff, hr - hl)
print(min_diff)

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

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

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

print(min(res))

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

ans = 10**18
for i in range(N - K + 1):
    ans = min(ans, H[i + K - 1] - H[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, acos, atan, asin
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 fractions import gcd
def input(): return sys.stdin.readline().strip()
def STR(): return input()
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 = sys.maxsize
mod = 10 ** 9 + 7

n, k = MAP()
h = [INT() 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())
h=[int(input())for _ in range(n)]
h.sort(reverse=1)
print(min(h[i-k+1]-h[i] for i in range(k-1,n)))

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

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

h.sort()

ans = 10**9+7

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

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 _ in range(N)]
h.sort()
Min = h[K] - h[0]
for i in range(N-K+1):
  if h[K+i-1] - h[i] < Min:
    Min = h[K+i-1] - h[i]
    
print(Min)

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

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

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

ans = float('inf')

for i in range(n-k+1):

    ans = min(ans,abs(h[i+k-1]-h[i]))

print(ans)

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

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

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

print(min(s))

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

list_H.sort()
ans = []

for i in range(n-k+1):
    ans.append(list_H[i+k-1]-list_H[i])

print(min(ans))

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

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

Solution (Python):
N,K = map(int, input().split())
A = [0]*N
DP=[0]*N

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

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

Solution (Python):
N,K=map(int,input().split())
L=[0]*N
for i in range(N):
  L[i]=int(input())
L.sort()
ans=1000000000
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())
h = sorted(list(int(input()) for _ in range(n)))

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

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

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

h=sorted(h)
bet=[h[N-1]]*(N-K+1)

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

print(min(bet))

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

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


n, k = map(int, input().split())
H = sorted(int(input()) for _ in range(n))

answer = float("inf")
for i in range(n - k + 1):
    temporary = H[i + k - 1] - H[i]
    if temporary < answer:
        answer = temporary

print(answer)

Solution (Python):
n,k = map(int, input().split())
h = [int(input()) for i in range(n)]
h.sort()
ans = h[k-1] - h[0]
for j in range(0,n-k+1):
  if  h[j+k-1] - h[j] < ans:
    ans = h[j+k-1] - h[j]
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 = h[k - 1] - h[0]
for i in range(n - (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):
n,k=map(int,input().split())
h=[0 for _ in range(n)]
for i in range(n):
    h[i]=int(input())
h.sort()
ans=10**9+7
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()))
h_sorted = sorted(h)
ans = False
for i in range(N - K + 1):
    if ans is not False:
        ans = min(ans, h_sorted[K + i - 1] - h_sorted[i])
    else:
        ans = h_sorted[K + i - 1] - h_sorted[i]
print(ans)

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

minH = 10 ** 9
for x in range(N - K + 1):
    sumh = H[x + K - 1] - H[x]
    
    if minH > sumh:
        minH = sumh
print(minH)

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


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


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

print(min(arr))

Solution (Python):




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

    H.sort()
    result = float("inf")

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


if __name__ == "__main__":
    main()

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

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

Solution (Python):
def main():
    n, k = [int(e) for e in input().split()]
    H = [int(input()) for i in range(n)]

    H = sorted(H)

    ans = float('inf')
    for ki in range(n - k + 1):
        diff = H[ki + k - 1] - H[ki]
        ans = min(diff, ans)

    print(ans)

if __name__ == "__main__":
    main()

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

h.sort()
ans = 1000000000

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 = 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 = []
ans = 10**10

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

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

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

tmp = 1000000000
for i in range(n - m + 1):
  tmp = min(tmp, a[i + m - 1] - a[i])
print(tmp)

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

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

print(min(diff))

Solution (Python):
import bisect




import itertools



import sys



ipti = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)

def main():
    n, k = list(map(int,ipti().split()))
    h = [0] * n
    for i in range(n):
        h[i] = int(input())

    h.sort()

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

    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines

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


if __name__ == "__main__":
    main()

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

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

print(seed)

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

H = [0]*N
for i in range(N):
  H[i] = int(input())
H.sort()
m = H[-1]-H[0]
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())
t = []
for _ in range(n):
    h = int(input())
    t.append(h)
t.sort()
m = 1145141919810
for i in range(0, n - k + 1):
    if m > t[i + k - 1] - t[i]:
        m = t[i + k - 1] - t[i]
print(m)

Solution (Python):

def submit():
    n, k = map(int, input().split())
    hlist = [int(input()) for _ in range(n)]

    hlist.sort()
    min_diff = float('inf')
    for i in range(0, n - k + 1):
        minh = hlist[i]
        maxh = hlist[i + k - 1]
        if maxh - minh < min_diff:
            min_diff = maxh - minh

    print(min_diff)
        

if __name__ == "__main__":
    submit()

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

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

Solution (Python):
x,y=list(map(int, input().split()))
list=[]
for i in range(x):
	list.append(int(input()))
list.sort()
ans=10**10

for j in range(x-y+1):
	if list[j+y-1]-list[j]<ans:
		ans=list[j+y-1]-list[j]
print(ans)

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

H.sort()

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

print(min_diff)

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

hl = []
for _ in range(n):
    hl.append(int(input()))

hl.sort()

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

print(ans)

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


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

print(ans)

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

H.sort()

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

print(ans)

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

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

Solution (Python):
N, K = map(int, input().split())
h_list = []
min1 = 10**10
for i in range(N):
    h = int(input())
    h_list.append(h)
h_list = sorted(h_list, reverse = True)
for i in range(N-K+1):
    cnt = h_list[i]-h_list[i+K-1]
    min1 = min(min1, cnt)
print(min1)

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

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

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

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

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

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

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

h.sort()

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

Solution (Python):


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

tree = list(sorted([int(input().rstrip()) for i in range(n)]))
ans = min([tree[i+k-1] - tree[i] for i in range(n-k+1)])
print(ans)

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

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

print(min(diff))

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

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

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

Solution (Python):
n, k = map(int, input().split())
H = sorted(int(input()) for _ in range(n))
tmp = H[k - 1] - H[0]
best = tmp
for i in range(1, n - k + 1):
  tmp = H[k + i - 1] - H[i]
  if tmp < best:
    best = tmp
print(best)

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

mh = int(1e9) + 1
for l_idx in range(len(hs) - k + 1):
    r_idx = l_idx + k - 1
    mh = min(mh, hs[r_idx] - hs[l_idx])
print(mh)

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

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

h = np.array(h)
h_sort = np.sort(h)
h_shift = np.roll(h_sort, -K+1)
dif = (h_shift - h_sort)[:-K+1]
ans = np.min(dif[dif>=0])
print(int(ans))

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

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

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

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

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

t = [[] for i in range(n-1)]
t[0] = diff[0]
for i in range(1,n-1):
    t[i] = t[i-1]+diff[i]
t = [0] + t





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

print(ans)

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

Solution (Python):
N,K,*h=map(int,open(0).read().split())
h.sort()
if K == N:
    print(h[-1] - h[0])
else:
    dif_h=[]
    for i in range(N-1):
        dif_h.append(h[i+1] - h[i])
    _min = base = sum(dif_h[:K-1])
    for i in range(K-1,N-1):
        base += dif_h[i]
        base -= dif_h[i-K+1]
        _min = min(base,_min)
    print(_min)

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

if __name__ == '__main__':
    main()

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

for i in range(N-(K-1)):
    
    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 = []
for i in range(n):
    h.append(int(input()))

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

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

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

ans = 1000000000000000000000

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

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

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

remainder_lst = []
for i in range(n-k+1):
    remainder = height_lst[i+k-1] - height_lst[i]
    remainder_lst.append(remainder)

answer = min(remainder_lst)
print(answer)

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


answer = float('inf')
for i in range(n - k + 1):
    cost = h[k - 1 + i] - h[i]
    
    if answer > cost:
        answer = cost

print(answer)

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

Solution (Python):
import numpy as np

N, K = map(int, input().split())
h = np.array([int(input()) for _ in range(N)])
h.sort()
h = h[::-1]

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

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

print(ans)