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 = []
for i in range(n):
  h.append(int(input()))
h.sort()
g = float('inf')
for i in range(n-k+1):
  g = min(g, h[i+k-1] - h[i])
print(g)

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

tree_list.sort()
min = 1145141919810
for i in range(K-1,N):

    if min > (tree_list[i] - tree_list[i-K+1]):
        min = tree_list[i] - tree_list[i-K+1]

print(min)

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

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

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

H.sort(reverse=True)

diff = list()
for n in range(N-K+1):
    diff.append(H[n] - H[n+K-1])

diff.sort()
print(diff.pop(0))

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

minimum=1000000000
for j in range(N-K+1):
  minimum=min(minimum, h[j+K-1]-h[j])
print(minimum)

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


if __name__ == '__main__':
    main()

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

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

main()

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

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

h.sort()
c = h[n] 

for j in range(1,n-k+2) :
    d = h[j+k-1] - h[j]
    c = d + 0 if c > d else c + 0

print(c)

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

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

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

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

Solution (Python):
import numpy as np

all_tree, sel_tree = map(int, input().split())
tree = []
for i in range(all_tree):
    tree.append(int(input()))

tree = np.array(tree)
tree = np.sort(tree)

highest = 10 ** 9
for begin in range(all_tree - sel_tree + 1):
    
    end = begin + sel_tree - 1
    diff = tree[end] - tree[begin]
    highest = min(highest, diff)
    
    
print(highest)

Solution (Python):
List = input()
List = List.split()
N = int(List[0])
K = int(List[1])
H = []
for i in range(N):
    height = int(input())
    H.append(height)
H.sort()
distance = 0
minDis = H[K-1] - H[0]
for i in range(N-K+1):
    distance = H[i+K-1] - H[i]
    if distance < minDis:
        minDis = distance
print(minDis)

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

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

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

Solution (Python):
a,b=input().split()
c,d=int(a),int(b)
l=[int(input()) for i in range(c)]
L=sorted(l)
x=L[d-1]-L[0]
for i in range(c-d+1):
    if L[d+i-1]-L[i]<x:
        x=L[d+i-1]-L[i]
print(x)

Solution (Python):
from heapq import heappush, heappop
import bisect

class Solver(object):
    def __init__(self):
        self.trees = []
        self.n, self.k = list(map(int, input().split(" ")))
        for _ in range(self.n):
            self.trees.append(int(input()))
            
        self.trees.sort()

    def solve(self):
        global_min = self.trees[self.k-1] - self.trees[0]
        for i in range(1, self.n - self.k + 1):  
            shortest = self.trees[i]
            tallest = self.trees[i+self.k-1]
            local_min = tallest - shortest
            if local_min < global_min:
                global_min = local_min
        print(global_min)


if __name__ == "__main__":
    s = Solver()
    s.solve()

Solution (Python):


from sys import stdin

input = stdin.readline

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

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

hdiff = [None]*(N-K+1)

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

print(min(hdiff))

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

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

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

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

Solution (Python):
def merge(a,b): 
    r =[]
    
    ai = 0 
    bi = 0 
    for i in range(0, len(a)+len(b)):
        if (bi >= len(b) or (ai < len(a) and a[ai] < b[bi])):
         
            r.append(a[ai])
            ai = ai + 1
        else:
            r.append(b[bi])
            bi = bi + 1 
    return r
def mergesort(a): 
    n = len(a)
    if n <= 1: 
        return a
    else:
        l = mergesort(a[0 : n//2])
        r = mergesort(a[n//2 : n])
        
        return merge(l,r)
        
N,K=map(int,input().split())
h=[]
for i in range(0,N):
    h.append(int(input()))
h_New=mergesort(h)
h_k=[]
for i in range(0,N-K+1):
    h_k.append(h_New[i+K-1]-h_New[i])
print(min(h_k))

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

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

sorted_hs = sorted(hs)

ret = None
for i in range(n-k+1):
    if ret is None or ret > sorted_hs[i+k-1] - sorted_hs[i]:
        ret = sorted_hs[i+k-1] - sorted_hs[i]

print(ret)

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

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(len(H) - 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()
h = [h[i + k - 1] - h[i] for i in range(n - k + 1)]

print(min(h))

Solution (Python):
n_tree, n_choice = [int(data) for data in input().split()]
heights = []
for i in range(n_tree):
  heights.append(int(input()))

diff = float("inf")
heights = sorted(heights)
for i in range(n_tree - n_choice + 1):
  h_min = heights[i]
  h_max = heights[i + n_choice - 1]
  if (h_max - h_min < diff):
    diff = h_max - h_min

print(diff)

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

a = []

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

print(min(a))

Solution (Python):
import numpy as np
n, k = (int(i) for i in input().split())

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

sorted_h = np.sort(h)
diff = np.hstack((sorted_h[k-1:], np.zeros(k-1)))

print(int(np.min(np.abs(diff - sorted_h)[:-k+1])))

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

hn.sort()

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

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

    h.sort()

    minh = 10000000000
    for i in range(n - k + 1):
        d = h[i + k - 1] - h[i] 
        if d == 0:
            print(0)
            return
        
        if d < minh:
            minh = d
    
    print(minh)

if __name__ == '__main__':
    main()

Solution (Python):
import math
import sys
from operator import itemgetter







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

if __name__ == '__main__':
	
	n, k = map(int, input().split())
	h = []
	for i in range(n):
		h.append(int(input()))
	
	solve(n, k, h)

Solution (Python):
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache
import sys
sys.setrecursionlimit(10000)
INF = float("inf")
YES, Yes, yes, NO, No, no = "YES", "Yes", "yes", "NO", "No", "no"
dy4, dx4 = [0, 1, 0, -1], [1, 0, -1, 0]
dy8, dx8 = [0, -1, 0, 1, 1, -1, -1, 1], [1, 0, -1, 0, 1, 1, -1, -1]


def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W


def ceil(a, b):
    return (a + b - 1) // b



def gcd(a, b):
    if b == 0:
        return a
    return gcd(b, a % b)



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


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


if __name__ == '__main__':
    main()

Solution (Python):
import sys

def main():
  input = sys.stdin.readline
  N, K = map(int, input().split())
  H = [int(input()) for _ in range(N)]
  H = sorted(H)
  ans = max(H) - min(H)
  for i in range(N-K+1):
    res = H[i+K-1] - H[i]
    ans = min(ans, res)
  print(ans)


if __name__ == '__main__':
  main()

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

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

Solution (Python):



def main():
  n, k = map(int, input().split())
  hs = [int(input()) for _ in range(n)]
  hs.sort()
  _min = hs[-1]
  for i in range(n-k+1):
    kouho = hs[i+k-1] - hs[i]
    if kouho < _min:
      _min = kouho
  print(_min)
  pass

if __name__=='__main__':
  main()

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

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

for i in range(n-k+1):
    hmin = H[i]
    hmax = H[i+k-1]
    d.append(hmax - hmin)

print(min(d))

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

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

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

l = sorted(l)
  

base_d = l[k-1] - l[0]

for i in range(n-k+1):
  d = l[i+k-1] - l[i]
  if base_d >= d:
    base_d = d
    
    
print(base_d)

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

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

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

print(r)

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

Solution (Python):

n,k = (int(i) for i in input().split())
t = [int(input()) for i in range(n)] 

t = sorted(t)
min = t[k-1]-t[0]
for i in range(n-k+1):
    s = t[i+k-1]-t[i]
    if s < min:
        min = s

print(min)

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

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

print(ans)

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

H = []

D = []

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

H.sort()


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

print(min(D))

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

H = []

D = []

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

H.sort()


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

print(min(D))

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

H.sort()

ans = 10**100

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

Solution (Python):
import numpy as np

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
    tmp = abs(H[i+k-1]-H[i])
    if ans>tmp:
        ans = tmp

print(ans)

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

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

Solution (Python):

if __name__ == '__main__':
    n,k=map(int, input().split())
    l = sorted(list(map(int, [input() for i in range(n)])),reverse=False)
    
    ans=1000000000
    for i in range(n-k+1):
        m=int(l[i+k-1]-l[i])
        if ans>m:
            ans=m
    print(ans)

Solution (Python):
if __name__ == '__main__':
    N, K = map(int, input().split())
    H = [int(input()) for _ in range(N)]
    H.sort()
    dif = [H[K-1+i] - H[0+i] for i in range(N-K+1)]
    print(min(dif))

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

print(ans)

Solution (Python):
l = map(long, raw_input().split())

height = []
for i in range(l[0]):
    height.append(long(input()))
    
height.sort()

diff = 1000000000000L
for x in range(len(height)-l[1]+1):
  if diff > height[x+l[1]-1] - height[x]:
    diff = height[x+l[1]-1] - height[x]
    
print diff

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

Solution (Python):


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

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


    print(min_d)


if __name__ == '__main__':
    main()

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

        min = tree_list[i+k-1]-tree_list[i]
print(int(min))

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

        min = tree_list[i+k-1]-tree_list[i]
print(int(min))

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

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

Solution (Python):
def run(N, K, h):
    h = sorted(h)
    diff = [h[i+1] - h[i] for i in range(N-1)]
    diff_all = max(h) - min(h)
    r_diff_sum = [0]
    for d in diff:
        r_diff_sum.append(r_diff_sum[-1] + d)
    l_diff_sum = [0]
    for d in reversed(diff):
        l_diff_sum.append(l_diff_sum[-1] + d)
    ret = diff_all
    
    for i in range(N - K + 1):
        ret = min(ret, diff_all - r_diff_sum[i] - l_diff_sum[N - K - i])
    return ret


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


if __name__ == '__main__':
    main()

Solution (Python):
start = list(map(int, input().split()))
n,k = start[0], start[1]

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

lst.sort()
lst.reverse()

diff_lst = list()
for i in range(n-k+1):
    diff_lst.append(lst[i] - lst[i+k-1])
print(min(diff_lst))

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

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

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

print(minp)

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

Solution (Python):
from statistics import mean, median,variance,stdev
import sys
import math

x = input().split()





a = []
b = int(x[0])
num = int(x[1])
for i in range(b):
    a.append(int(input()))
a = sorted(a)
min = a[num-1]-a[0]
for i in range(num,b):
    if min > a[i]-a[i-num+1]:
        min = a[i]-a[i-num+1]
if min > a[b-1]-a[b-num]:
    min = a[b-1]-a[b-num]
print(min)

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

Solution (Python):
line=[]
d=0

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

for j in range(n):
  p=int(input())
  line.append(p)
  

line.sort()


min=line[k]-line[0]
for mm in range(0,n-k+1):
  d=(line[mm+k-1]-line[mm])
  if min>d:
    min=d
    
print(min)

Solution (Python):
N,K = [int(x) for x in input().split()]
h = [int(input()) for _ in range(N)]
i=0
hdif = 1000000000000
h.sort()
while(i+K-1<N):
    hdif=min(h[i+K-1]-h[i],hdif)
    i+=1
print(hdif)

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

h.sort(reverse=True)

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

Solution (Python):
N, K = map(int,input().split())
trees = []
for i in range(N):
    h = int(input())
    trees.append(h)
trees = list(sorted(trees))
ans = 10000000000
for q in range(N-K+1):
    ans = min(ans,trees[q+K-1]-trees[q])
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 = []
for _ in range(N):
    H.append(int(input()))
H.sort()
D = []

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

print(min(D))

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

ans = float('inf')
for i in range(n-k+1):
    if ans > hl[i+k-1] - hl[i]:
        ans = hl[i+k-1] - hl[i]
print(ans)

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

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

Solution (Python):
N,K = map(int,input().split())
L = sorted([int(input()) for i in range(N)])
ans = float("inf")
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=[int(input()) for i in range(N)]

h=sorted(h)

min=1000000000000
for i in range(N-K+1):
    t=h[i+K-1]-h[i]
    if(t<min):
        min=t

print(min)

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

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

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

H.sort()

R = [H[i+K-1] - H[i] for i in range(N+1-K)]

print(min(R))

Solution (Python):
N, K = map(int, input().split())
lst = []
for _ in range(N):
    lst.append(int(input()))
lst = sorted(lst)
ret = 100000000000
 
for idx, x in enumerate(lst[:-K+1], 0):
    tmp = lst[idx + K-1] - x
    if tmp < ret:
        ret = tmp
print(ret)

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

R = []

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

R = sorted(R)
minv = 2000000000

for j in range(n-k+1):
    minv = min(minv,R[j+k-1]-R[j])

print(minv)

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

h.sort()


min = 10 ** 20

for i in range(N):
  if i+K > N:
    break
  diff = h[i+K-1] - h[i]
  if diff < min:
    min = diff

print(min)

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

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

x.sort()

y=[]

for j in range(n-k+1):
  p=x[j+k-1]-x[j]
  y.append(p)

y.sort()

print(y[0])

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

min = 10 ** 20

for i in range(N):
  if i+K > N:
    break
  diff = h[i] - h[i+K-1]
  if diff < min:
    min = diff

print(min)

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

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

h = sorted(h)


min = 10 ** 9 + 1
for i in range(n - k + 1):
  h_min = h[i]
  h_max = h[i + k - 1]
  h_diff = h_max - h_min
  if h_diff < min:
    min = h_diff

print(min)

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

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

Solution (Python):
N, K = map(int, input().split())
h = []
for i in range(N):
    h.append(int(input()))
h.sort(reverse=True)
delta = set()
for j in range(N - K + 1):
    delta.add(h[j] - h[j+K-1])
print(min(delta))

Solution (Python):

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

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

ans = min(gap)
print(ans)

Solution (Python):
import sys

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

  cnt = sys.maxsize
  for i in range(n - k + 1):
    if cnt > h[i] - h[i+k-1]:
      cnt = h[i] - h[i+k-1]
  print(cnt)

if __name__ == '__main__':
  main()

Solution (Python):

from sys import stdin

def main():
    N, K = [int(x) for x in stdin.readline().rstrip().split()]
    trees = [int(input()) for i in range(N)]
    trees.sort()

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

    print(min(s))



if __name__ == '__main__':
    main()

Solution (Python):

def solve():
    N, K = map(int, input().split())
    data = [int(input()) for _ in range(N)]
    data.sort()
    ans = []
    cnt = 0
    while True:
        if data[cnt] >= data[-cnt-K]:
            break
        h_max1 = data[cnt + K - 1]
        h_min1 = data[cnt]
        h_max2 = data[-cnt-1]
        h_min2 = data[-cnt - K]
        tmp1 = h_max1 - h_min1
        tmp2 = h_max2 - h_min2
        ans.append(tmp1)
        ans.append(tmp2)
        cnt = cnt + 1
        if data[cnt] >= data[-cnt-K]:
            break

    print(min(ans))


solve()

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

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

Solution (Python):
import itertools

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

heights.sort()
min_diff = max(heights) - min(heights) 
for i in range(N-K+1):
    diff = heights[i+K-1] - heights[i]
    if diff < min_diff:
        min_diff = diff

print(min_diff)

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):
tree_num,choice_num = map(int,input().split())
tree_list = sorted([int(input()) for _ in range(tree_num)])
mi = 999999999999999999999999
for idx in range(tree_num-choice_num+1):
    cand = tree_list[idx+choice_num-1] - tree_list[idx]
    if cand < mi:
        mi = cand
print(mi)

Solution (Python):
import sys
N,K = list(map(int, input().strip().split(' ')))
trees = [int(line.strip()) for line in sys.stdin]
trees = sorted(trees)
print(min([trees[i+(K-1)]-trees[i] for i in range(len(trees)-(K-1))]))

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

h.sort()

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

print(score)

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

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

Solution (Python):
n,k = map(int,input().split())
h = sorted(list(map(int, [input() for i 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())
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 fastio():
    import sys
    from io import StringIO 
    from atexit import register
    global input
    sys.stdin = StringIO(sys.stdin.read())
    input = lambda : sys.stdin.readline().rstrip('\r\n')
    sys.stdout = StringIO()
    register(lambda : sys.__stdout__.write(sys.stdout.getvalue()))
fastio()

MOD = 10**9 + 7
I = lambda:list(map(int,input().split()))

n, k = I()
l = []
for i in range(n): 
    l.append(I()[0])
l.sort()
ans = MOD

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

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

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

sort_h = sorted(h)
diffmin=10**9

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

  diff = sort_h[k+times-1]-sort_h[times]
  if diffmin > diff:
    diffmin=diff

print(diffmin)

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

h = sorted(h)

tmp_min = 10**10
for i in range(n):
    if i+k-1>=n:
        break
    tmp = h[i+k-1]-h[i]
    if tmp_min>tmp:
        tmp_min = tmp
print(tmp_min)

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

Solution (Python):
N, k = [int(s) for s in input().split(' ')]

h = []
while len(h) != N:
    h.append(int(input()))
h.sort()

min = 1000000000
for i in range(0, len(h)-k+1):
    x = h[i+k-1]-h[i]
    
    if min > x:
        min = x
print(min)

Solution (Python):
N, K = map(int, input().split())
h = sorted([int(input()) for i 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())
a = [int(input()) for i in range(N)]
a = sorted(a)
res = float('inf')

for i in range(N - K + 1):
  if res > a[i+K-1] - a[i]:
    res = a[i+K-1] - a[i]

print(res)

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

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

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

Solution (Python):
N, k = [int(s) for s in input().split(' ')]
 
h = []
while len(h) != N:
    h.append(int(input()))
  
min_height = 1000000000
h.sort()
for start in range(0, N - k + 1):
    end = start + k - 1
    diff = h[end] - h[start]
    if diff < min_height:
        min_height = diff
print(min_height)