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):
def smaller(x, y):
	if x < y:
		return x
	return y

N, K = map(int, input().split())
H = []
for i in range(0, N):
	H.append(int(input()))
H.sort()
ans = 5000000000000000
for i in range(0, N-K+1):
	ans = smaller(ans, H[i+K-1]-H[i])
print(ans)

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

diff = heights[K-1] - heights[0]
ans = diff

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

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

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

    print(ans)


main()

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

li.sort()
num = 0
li2 = []
for i in range(len(li)-k+1):
    li2.append(li[i+k-1] - li[i])

print(min(li2))

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

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

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

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

print(ans)

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

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

Solution (Python):
import math
n,k = map(int,input().split(' '))
v =[]
for i in range(0,n):
    x = int(input())
    v.append(x)
v.sort()
ans = 20000000000000
for i in range(0,n):
    if i >= k - 1:
        ans = min(ans,abs(v[i] - v[i-k+1]))
print(ans)

Solution (Python):


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

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

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

Solution (Python):
import sys 
import numpy as np

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

h = np.zeros(N)

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

h = np.sort(h)

m = h[N-1] - h[0]

for n in range(0,N-K+1):
    d = h[n+K-1] - h[n]
    if d < m:
        m = d

print(int(m))

Solution (Python):



N, K = map(int, input().split())
arr = [int(input()) for i in range(N)]
arr.sort()
num = 0
min_num = pow(10,9)

for i in range(N - K + 1):
    num = arr[i + K - 1] - arr[i]
    if min_num > num:
        min_num = num
print(min_num)

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

print(min)

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 = sorted(h)

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

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

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(0,n-k+1):
    h_max = h[i+k-1]
    h_min = h[i]
    c = h_max - h_min
    if c < ans:
        ans = c
print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
'''
allinputs = iter(input().splitlines())
input = lambda : next(allinputs)

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

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

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

    mn = 10**10

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

if __name__ == "__main__":
    main()

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

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

    mn = 10**10

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

if __name__ == "__main__":
    main()

Solution (Python):
n, k = map(int, input().split())
tree = [int(input()) for _ in range(n)]
tree.sort()
result = float('INF')
cumulative = [0]
for i in range(n - 1):
    cumulative.append(tree[i + 1] - tree[i] + cumulative[i])
for i in range(n - k + 1):
    result = min(result, cumulative[i + k - 1] - cumulative[i])
print(result)

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

hlist=[]
for i in range(N):
  hi=int(input())
  hlist.append(hi)
  
hlist.sort()


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

Solution (Python):
n, k = map(int, raw_input().split())
H = [int(raw_input()) for i in range(n)]
H.sort()
min_diff = 10**9 + 1
for i in range(n - k + 1):
    diff = H[i+k-1] - H[i]
    if min_diff > diff:
        min_diff = diff
print min_diff

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


m = k - 1
height = sorted([int(input()) for i in range(n)]) + [0] * m   
ans = min([height[i+m] - height[i] for i in range(n)][0:-m])  


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)

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 = map(int,input().split())
a = [int(input()) for i in range(n)]
a.sort()

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

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

Solution (Python):
from collections import deque

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

hq = deque() 
dif = float("inf")
for i in range(n) :
    hmax = hl[i]
    hq.append(hmax)
    if len(hq) == k :
        hmin = hq.popleft()
        dif = min(dif, hmax - hmin)

print(dif)

Solution (Python):
n,k,*h=map(int,open(0).read().split())
h.sort()
print(min(y-x for x,y in zip(h,h[k-1:])))

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

ans = 10**9
for j in range(N - K+1):
    sa = tree[j + K-1] - tree[j]
    if sa < ans:
        ans = sa
print(ans)

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

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=[int(input()) for _ in range(N)]
h.sort()

def h_find(list,n):
    k=list[n-1]-list[0]
    for i in range(1,len(list)-n+1):
        k2=list[i+n-1]-list[i]
        if k2<k:
            k=k2
        if k==0:
            break
    return k

print(h_find(h,K))

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

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

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

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

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

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

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

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

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

from collections import defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inpl(): return list(map(int, input().split()))
def inpl_str(): return list(input().split())

N,K = inpl()
hh = [int(input()) for _ in range(N)]
hh.sort()
l,r = 0,K-1
ans = INF
while True:
    tmp = hh[r] - hh[l]
    ans = min(ans,tmp)
    l += 1
    r += 1
    if r == N:
        break

print(ans)

Solution (Python):
n, k = map(int, input().split())
dat_h = []
for i in range(n):
    dat_h.append(int(input()))
dat_h.sort(reverse=True)
res = 100000000000
for i in range(n-k+1):
    res = min(res, dat_h[i] - dat_h[i+k-1])
print(res)

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

A.sort()

mini=10000000000
for i in range(N-K+1):
    mini=min(mini,A[i+K-1]-A[i])

print(mini)

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

Solution (Python):
n,k = map(int, input().split())
ls = [int(input()) for _ in range(n)]
ls.sort()
tmp = 10 ** 9
for i in range(n-k+1):
    diff = ls[i+k-1] - ls[i]
    if diff < tmp:
        tmp = diff
print(tmp)

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

List = sorted(List)

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

Solution (Python):

import sys
input = sys.stdin.readline

def main():
    n, k = list(map(int, input().rstrip().split()))
    lst = sorted(list(map(int, [input().rstrip() for _ in range(n)])))
    ans = lst[k - 1] - lst[0]
    for i, j in zip(lst, lst[k - 1 :]):
        ans = min(ans, j - i)
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):

def main():
    n, k = list(map(int, input().rstrip().split()))
    lst = sorted(list(map(int, [input().rstrip() for _ in range(n)])))
    ans = lst[k - 1] - lst[0]
    for i, j in zip(lst, lst[k - 1 :]):
        ans = min(ans, j - 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):
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())
A = [int(input()) for _ in range(N)]
A.sort()
m = []
for i in range(N-K+1):
    m.append(A[K-1+i]-A[i])
print(min(m))

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

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

height_list = [0] * N

for n in range(N):
    height_list[n] = int(input())
height_list = sorted(height_list, reverse=True)

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

for i in range(N - K + 1):
    diff_list[i] = height_list[i] - height_list[i + K - 1]
print(min(diff_list))

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

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

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

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


H.sort()


ans = H[-1] - H[0]

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

print(ans)

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()
answer = (H[K:] - H[:-K]).min()
print(answer)

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):
h,k = [int(x) for x in input().split()]
a = [int(input()) for _ in range(h)]
a.sort()
i = 0
b = []
while i <= h-k:
  b.append(a[i+k-1] - a[i])
  i += 1
print(min(b))

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

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

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

print(min(difference))

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

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

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

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

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

print(ans)

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

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

h.sort(reverse=True)
ans = []

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

print(min(ans))

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

print(min(p))

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

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


H = sorted([int(input()) for i in range(N)],reverse=True)




L = []

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

print(min(L))

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

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

h.sort()

difference = []

for i in range(N-K+1):
    
    difference.append(h[K-1+i]-h[i])
    
print(min(difference))

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

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

Solution (Python):

import sys
INPUT = sys.stdin.readline

def SINGLE_INT(): return int(INPUT())
def MULTIPLE_INT_LIST(): return list(map(int, INPUT().split()))
def MULTIPLE_INT_MAP(): return map(int, INPUT().split())
def SINGLE_STRING(): return INPUT()
def MULTIPLE_STRING(): return INPUT().split()

N, K = MULTIPLE_INT_MAP()
trees = [int(input()) for _ in range(N)]
trees.sort()
ans = float('inf')



print(min([trees[i+K-1]-trees[i] for i in range(N-K+1)]))

Solution (Python):

import sys
INPUT = sys.stdin.readline

def SINGLE_INT(): return int(INPUT())
def MULTIPLE_INT_LIST(): return list(map(int, INPUT().split()))
def MULTIPLE_INT_MAP(): return map(int, INPUT().split())
def SINGLE_STRING(): return INPUT()
def MULTIPLE_STRING(): return INPUT().split()

N, K = MULTIPLE_INT_MAP()
trees = [int(input()) for _ in range(N)]
trees.sort()
ans = float('inf')

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

print(ans)

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

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

Solution (Python):

import sys
input = sys.stdin.readline

N, K = map(int, input().split())
h = [int(input()) for _ in range(N)]
h.sort()
m = 10 ** 10
for i in range(N-K+1):
    tmp = h[i+K-1] - h[i]
    if tmp < m:
        m = tmp
print(m)

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

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):
def resolve():
    (N, K) = list(map(int, input().split(" ")))
    Hs = [int(input()) for i in range(N)]
    Hs.sort()
    diffmin = float('inf')
    for i in range(0, N-K+1):
        diff = Hs[i+K-1] - Hs[i]
        if diffmin > diff:
            diffmin = diff
    print(diffmin)

if '__main__' == __name__:
    resolve()

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

Solution (Python):
def quick_sort(arr):
    left = []
    right = []
    if len(arr) <= 1:
        return arr

    
    
    ref = arr[0]
    ref_count = 0

    for ele in arr:
        if ele < ref:
            left.append(ele)
        elif ele > ref:
            right.append(ele)
        else:
            ref_count += 1
    left = quick_sort(left)
    right = quick_sort(right)
    return left + [ref] * ref_count + right

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

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
def input(): return sys.stdin.readline().rstrip()
def mi(): return map(int, input().split())


N, K = mi()
Hs = sorted([int(input()) for _ in range(N)])
gaps = [0] * (N-1)
for i in range(N-1):
  gaps[i] = Hs[i+1] - Hs[i]

mingapsum = 10**9


nowgapsum = sum(gaps[0:K-1])
mingapsum = min(nowgapsum, mingapsum)

prevgap = gaps[0]
for i in range(1, N-K+1):
  nowgapsum = nowgapsum - prevgap + gaps[K-2+i]
  prevgap = gaps[i]
  mingapsum = min(nowgapsum, mingapsum)

print(mingapsum)

Solution (Python):
N, K = map(int, input().split())
h = [int(input()) for i in range(N)]
h.sort()
ans=[]
for i in range(len(h)-K+1):
  b=h[K+i-1]-h[i]
  ans.append(b)
print(min(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 resolve():
    N,K=map(int,input().split())
    l=[]
    mx=10**9

    for i in range(N):
        l.append(int(input()))
    l.sort()
    for i in range(N-K+1):
        mx=min(mx,l[i+K-1]-l[i])
    print(mx)
resolve()

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

h.sort()
ans = 1e9


for i in range(0,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())

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

print(ans)

Solution (Python):

"""
C - Christmas Eve
https://atcoder.jp/contests/abc115/tasks/abc115_c

"""
import sys


def solve(heights, K):
    return min([r - l for l, r in zip(heights, heights[K - 1:])])


def main(args):
    N, K = map(int, input().split())
    heights = sorted([int(input()) for _ in range(N)])
    ans = solve(heights, K)
    print(ans)


if __name__ == '__main__':
    main(sys.argv[1:])

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

h_list.sort(reverse=True)



min = h_list[0] - h_list[N-1]

for i in range(N-K+1):
  diff = h_list[i] - h_list[i+K-1]
  
  if min>diff:
    min = diff
    
print(min)

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

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

tree = h[:K]
height = []
height.append(tree[-1]-tree[0])

for n in range(K,N):
    tree.pop(0)
    tree.append(h[n])
    height.append(tree[-1]-tree[0])  

print(min(height))

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):
in_1 = input().split()
n = int(in_1[0])
k = int(in_1[1])

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

trees_height.sort()


def sub_minmax(list):
    return max(list)-min(list)

subs = []

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


print(min(subs))

Solution (Python):
x = input().split()
height = [int(input()) for n in range(int(x[0]))]
height.sort()
check_new = 0
check_old = max(height)
for n in range(int(x[0]) - int(x[1]) + 1):
    check_new = height[int(x[1]) - 1 + n] - height[n]
    if check_new <= check_old:
        check_old = check_new
    
print(check_old)

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

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

hs.sort()
mini = 10 ** 10
for i in range(n-k+1):
    sample = abs(hs[i] - hs[i+k-1])
    if mini > sample:
        mini = sample

print(mini)

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time

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

def LI(): return list(map(int,input().split()))
def I(): return int(input())
def LS(): return input().split()
def S(): return input()

def main():
  n,k=LI()

  l=[]
  for _ in range(n):
    l.append(I())

  l.sort()

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

  return mn

print(main())

Solution (Python):



n, k = map(int, input().split())
a = []
o = 10000000000
for i in range(n):
    a.append(int(input()))
a.sort()


for i in range(n-k+1):
    t = a[i+k-1]-a[i]
    if o > t:
        o = t

print(o)

Solution (Python):
n,k=map(int,input().split())
h=sorted([int(input()) for _ in range(n)])
gap=[h[i]-h[i-1] for i in range(1,n)]
tmp=0
lst=[gap[0]]
for i in range(1,len(gap)):
    lst.append(gap[i]+lst[i-1])

ans=[lst[k-2]]
for i in range(k,n):
    ans.append(lst[i-1]-lst[i-k])

print(min(ans))

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

arr = [int(input()) for _ in range(n)]

arr.sort()

print(min(abs(y - x) for x, y in zip(arr, arr[k-1:])))

Solution (Python):

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

res = 1000000000000
for i in range(N-K+1):
    res = min(res, inl[i+K-1] - inl[i])

print(res)

Solution (Python):
import heapq

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

for _ in range(n):
    
    heapq.heappush(h, int(input()))

h = [heapq.heappop(h) for _ in range(n)]

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

print(ans)

Solution (Python):
import heapq

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

for _ in range(n):
    
    heapq.heappush(h, int(input()))

h = [heapq.heappop(h) for _ in range(n)]

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

print(ans)

Solution (Python):
import heapq

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

for _ in range(n):
    
    heapq.heappush(h, int(input()))

h = [heapq.heappop(h) for _ in range(n)]

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

print(ans)

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

H = sorted(H)
que = deque()
mi = float('inf')
for i in range(N):
    que.append(H[i])
    if len(que) >= K:
        mi = min(mi, que[-1] - que[0])
        que.popleft()

print(mi)

Solution (Python):



import sys

input = sys.stdin.readline

def main():
    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)]))

main()

Solution (Python):

n, k = [ int(v) for v in input().split() ]
tree_list = [0]*n

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

tree_list = sorted(tree_list)


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

Solution (Python):
import sys
input = sys.stdin.readline
n, k = [ int(v) for v in input().split() ]
tree_list = [0]*n

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

tree_list = sorted(tree_list)


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

Solution (Python):
N,K=map(int,input().split())
h=[int(input()) for i in range(N)]
h.sort()
h.reverse()
S=100000000000
for j in range(N-K+1):
    s=h[j]-h[j+K-1]
    if s<S:
        S=s
print(S)

Solution (Python):

import sys
input = sys.stdin.readline

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

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

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


print(min(l))

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

Solution (Python):

import sys
input = sys.stdin.readline

n, k = list(map(int, input().rstrip().split()))
lst = sorted(list(map(int, [input().rstrip() for _ in range(n)])))
ans = lst[k - 1] - lst[0]
for i, j in zip(lst, lst[k - 1 :]):
    ans = min(ans, j - i)
print(ans)

Solution (Python):

n, k = list(map(int, input().rstrip().split()))
lst = sorted(list(map(int, [input().rstrip() for _ in range(n)])))
ans = lst[k - 1] - lst[0]
for i, j in zip(lst, lst[k - 1 :]):
    ans = min(ans, j - i)
print(ans)

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

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

Solution (Python):
n, k = [int(i) for i in input().split()]
tall = []
dis = []
for i in range(n):
  tall.append(int(input()))
tall.sort(reverse = True)
for i in range(n-k+1):
  dis.append(tall[i] - tall[i+k-1])
print(min(dis))

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

Solution (Python):
from sys import stdin
N, K = [int(x) for x in stdin.readline().rstrip().split()]
h = [int(stdin.readline().rstrip()) for _ in range(N)]
h.sort()
diff = [h[k + K - 1] - h[k] for k in range(N - K + 1)]
diff.sort()
print(diff[0])

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

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

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