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())
l=[]
for _ in range(n):
    l.append(int(input()))
l.sort()
a=0
for i in range(n-k+1):
    if i==0:
        a=l[i+k-1]-l[i]
    else:
        a=min(a,abs(l[i+k-1]-l[i]))
print(a)

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

Solution (Python):
import sys
n, k = map(int, input().split())
lines = sys.stdin.readlines()
trees = [None] * n
for i in range(n):
    trees[i] = int(lines[i])

trees.sort()


min_h = sys.maxsize
k_1 = k - 1
for i in range(n-k+1):
    height_diff = trees[i + k_1] - trees[i]
    if height_diff < min_h:
        min_h = height_diff


print(min_h)

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, *H = map(int, open(0).read().split())
H.sort()
print(min(H[i + K - 1] - H[i] for i in range(N - K + 1)))

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

Solution (Python):
import numpy as np
N,K = map(int,input().split())
list = []
for i in range(N):
 list.append(int(input()))
list = sorted(list);
print(min(list[i+K-1] - list[i] for i in range(N-K+1)))

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

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

h.sort()

ans = float('inf')
for l, h in zip(h[:-K+1], h[K-1:]):
    ans = min(ans, h - l)
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=sorted(h)
mi=float('inf')
 
for i in range(n-k+1):
    mi = min(mi, h[i+k-1]-h[i])
 
print(mi)

Solution (Python):
N, K=map(int, input().split())
h=[int(input()) for i in range(N)]
h.sort()
diff=None
for i in range(N-(K-1)):
    td=h[i+(K-1)]-h[i]
    if diff is None or td<diff:
        diff=td

print(diff)

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

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

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

print(dif)

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

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, raw_input().split())
l=[]
for i in range(n):
	a = int(raw_input())
	l.append(a)

l.sort()
min=l[n-1]
for i in range(n-k+1):
	ini = l[k+i-1]-l[i]
	min = ini if min > ini else min
	
print min

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

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

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

print(ans)

Solution (Python):
N, K = map(int, input().split())
h = [int(input()) for 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=list(int(input()) for i in range(n))
h.sort()
mi=float('inf')

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

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

Solution (Python):
import math

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

crr_min = 10**9

for i in range(0,math.ceil((n-k+1)/2)):
    tmp1 = h[i+k-1] - h[i]
    tmp2 = h[-1-i] - h[-i-k]
    if tmp1==0 or tmp2==0:
        crr_min=0
        break

    if tmp1<crr_min:
        crr_min = tmp1

    if tmp2<crr_min:
        crr_min = tmp2


print(crr_min)

Solution (Python):

"""
Created on Sat Dec  8 20:51:13 2018

@author: Owner
"""

import collections
import scipy.misc
import sys
import numpy as np
import math
from operator import itemgetter
import itertools

def prime_decomposition(n):
  i = 2
  table = []
  while i * i <= n:
    while n % i == 0:
      n /= i
      table.append(int(i))
    i += 1
  if n > 1:
    table.append(int(n))
  return table   

def digit(i):
    if i > 0:
        return digit(i//10) + [i%10]
    else:
        return []

"""
N, X = map(int, input().split())

x = [0]*N
x[:] = map(int, input().split())

P = [0]*M
Y = [0]*M
for m in range(M):
    P[m], Y[m] = map(int, input().split())
    
all(nstr.count(c) for c in '753')
"""
N, K = map(int, input().split())


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

h.sort()
diff_min = math.pow(10, 9)
diff_min = 10**9

for i in range(len(h)-K+1):
    
    temp_diff = h[i+K-1] - h[i]
    
    if diff_min > temp_diff:
        diff_min = temp_diff
    if diff_min == 0:
        break
"""
for l in list(itertools.combinations(h,K)):
    
    temp_diff = l[np.argmax(l)] - l[np.argmin(l)]
    if diff_min > temp_diff:
        diff_min = temp_diff
    if diff_min == 0:
        break
"""     
print(diff_min)

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

h_diff = 999999999
h.sort()
for i in range(N-K+1):
    if h_diff > h[i+K-1] - h[i]:
        h_diff = h[i+K-1] - h[i]

print(h_diff)

Solution (Python):

"""
Created on Sat Dec  8 20:51:13 2018

@author: Owner
"""

import collections
import scipy.misc
import sys
import numpy as np
import math
from operator import itemgetter
import itertools

def prime_decomposition(n):
  i = 2
  table = []
  while i * i <= n:
    while n % i == 0:
      n /= i
      table.append(int(i))
    i += 1
  if n > 1:
    table.append(int(n))
  return table   

def digit(i):
    if i > 0:
        return digit(i//10) + [i%10]
    else:
        return []

"""
N, X = map(int, input().split())

x = [0]*N
x[:] = map(int, input().split())

P = [0]*M
Y = [0]*M
for m in range(M):
    P[m], Y[m] = map(int, input().split())
    
all(nstr.count(c) for c in '753')
"""
N, K = map(int, input().split())


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

h.sort()
diff_min = math.pow(10, 9)

for i in range(len(h)-K+1):
    
    temp_diff = h[i+K-1] - h[i]
    
    if diff_min > temp_diff:
        diff_min = temp_diff
    if diff_min == 0:
        break
"""
for l in list(itertools.combinations(h,K)):
    
    temp_diff = l[np.argmax(l)] - l[np.argmin(l)]
    if diff_min > temp_diff:
        diff_min = temp_diff
    if diff_min == 0:
        break
"""     
print(diff_min)

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

a.sort()


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

Solution (Python):
c = float('inf')
N, K = map(int,input().split())
K-=1
t = tuple(sorted(int(input()) for i in range(N)))
for i in range(len(t)-K):
  b = t[i+K]-t[i]
  c = b if b<c else c
  if not c:
    break
print(c)

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

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

Solution (Python):

N, K = map(int, input().split())
lst = sorted([int(input()) for _ in range(N)], reverse=True)

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

Solution (Python):


from collections import defaultdict, deque

def solve():
    N, K = map(int, input().split())
    H = defaultdict(int)
    for _ in range(N):
        H[int(input())] += 1
    if K in H.values():
        res = 0
    else:
        H = sorted(list(H.items()))
        res = float('inf')
        i = 0
        j = 0
        b = H[0][0]
        t = H[0][0]
        k = H[0][1]
        h = 0
        while j < len(H)-1:
            if k < K:
                j += 1 
                k += H[j][1]
                h += H[j][0] - t
                t = H[j][0]
            if k >= K:
                res = min(res, h)
                k -= H[i][1]
                i += 1
                h -= H[i][0] - b
                b = H[i][0]

    return str(res)

if __name__ == '__main__':
    print(solve())

Solution (Python):
N, K = map(int, input().split())
tree = [int(input()) for n in range(N)]
diff = 10 ** 9
tree.sort()
for i in range(N - (K - 1)):
    if tree[i] == tree[i + 1] == tree[i + 2]:
        diff = 0
        break
    elif tree[i + K - 1] - tree[i] < diff:
        diff = tree[i + K - 1] - tree[i]
print(diff)

Solution (Python):
line = input().split(" ")
n = int(line[0])
k = int(line[1])
h = []
sa = 9999999999999999999999


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

h = sorted(h)

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

print(sa)

Solution (Python):
l = input().split(" ")
trees = int(l[0])
lights = int(l[1])
ls=[]
for i in range(trees):
    ls.append(int(input()))
ls = sorted(ls,reverse=True)
h = ls[0]-ls[lights-1]
for i in range(trees -lights):
   if h >min(ls[i]-ls[i+lights-1],ls[i+1]-ls[i+lights]):
       h = min(ls[i]-ls[i+lights-1],ls[i+1]-ls[i+lights])
print(h)

Solution (Python):
n=input().split()
n=[int(i) for i in n]
h=[0]*n[0]
for i in range(n[0]):
    h[i]=int(input())
h.sort()
q=-1
for i in range(0,len(h)-n[1]+1):
    a=h[i+n[1]-1]-h[i]
    if q==-1 or q>a:
        q=a
print(q)

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

Solution (Python):

import itertools

N, K = map(int, input().split())
lst = sorted([int(input()) for _ in range(N)], reverse=True)

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

Solution (Python):
n,k = map(int, input().split())
h = [int(input()) for _ in range(n)]
h.sort(reverse=True)
result = set([])
for i in range(n - k + 1):
    result.add(h[i] - h[i + k -1])

print(min(result))

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

print(ans)

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

if list(np.diff(hs)) == list(np.zeros(n-1, dtype=int)):
    print('0')
else:
    sorted_hs = np.sort(hs)
    try:
        print(min([min(np.diff(sorted_hs[i::k-1])) for i in range(k) if not len(sorted_hs[i::k-1]) == 1]))
    except:
        print('0')

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

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

h.sort()


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

print(min)

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

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

trees = sorted(trees,reverse=True)
average = sum(trees)/len(trees)
min_dist = 1e+10
if abs(max(trees) - average) > abs(min(trees) - average):
    for i in range(n)[k-1:]:
        if min_dist > abs(trees[i] - trees[i-k+1]):

            min_dist = abs(trees[i] - trees[i-k+1])
else:
    for i in range(n-k+1):
        if min_dist > abs(trees[i+k-1] - trees[i]):
            min_dist = abs(trees[i+k-1] - trees[i])
print(min_dist)

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

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

for i in range(N):
        li.append(int(input()))

li.sort()

co = K - 1
hmin = li[co] - li[0]
for i in range(N-K+1):
        if hmin > li[i+co] - li[i]:
                hmin = li[i+co] - li[i]
                if hmin == 0:
                        break
print(hmin)

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

for i in range(n):
    h.append(int(input()))
min = 10 ** 10

h = sorted(h)

for i in range(n-k+1):
    
    div = h[i+k-1] - h[i]
    if div < min:
        min = div
    if min == 0:
        print(min)
        exit()
print(min)

Solution (Python):
n,k = 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):
import sys
def 解():
    N,K=[int(_) for _ in sys.stdin.readline().split()]
    aH=sorted(int(_) for _ in sys.stdin.readlines())

    iDiff = aH[-1]
    for i in range(N-K+1):
        d = aH[i+K-1] - aH[i]
        if d < iDiff:
            iDiff = d
        
        
    print(iDiff)
解()

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

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

h = []




for i in range(n):

    h.append(int(input()))

h.sort()

min = 9999999999



for j in range(n):

    z = j + k-1

    if z > n-1:
        break

    sa = h[z] - h[j]

    

    if sa < min:

        min = sa


print(min)

Solution (Python):
import numpy as np

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

a = np.sort(hs)
print(int(np.min(np.convolve(np.diff(a), np.ones(k - 1), 'valid'))))

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

Solution (Python):
N,K = map(int,input().split())
h = [int(input()) for _ in range(N)]
H = sorted(h, reverse=True)
delta_h = H[0]
for i in range(N-K+1):
  delta_hi = H[i]-H[i+K-1]
  if delta_h > delta_hi:
    delta_h = delta_hi
print(delta_h)

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

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

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

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

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

Solution (Python):

import collections


def main():
    N, K = map(int, raw_input().split())
    h = [input() for _ in xrange(N)]
    h = sorted(h)
    col = collections.Counter(h)
    """
    Kが最頻出回数以下であれば、answerは0となり即終了
    """
    ans = 0
    if K <= max(col.values()):
        pass
    else:
        """
        そうでない場合は、面倒だがソート済みのhに対して、 i = 0としたとき、 abs(h[i] - h[i+K])を全空間に対して行う
        """
        ans = 9999999999999999
        for i in xrange(N - K + 1):
            ans = min((abs(h[i] - h[i + K - 1])), ans)

    print(ans)


if __name__ == "__main__":
    main()

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

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

h.sort()
minimum = h[N - 1]- h[0]

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

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

Solution (Python):
N_K = input().split()
N = int(N_K[0])
K = int(N_K[1])
Hs = sorted([int(input()) for i in range(N)])
Hs_diff_K = [Hs[K+i-1] - Hs[i] for i in range(N) if i < N-K+1]
print(min(Hs_diff_K))

Solution (Python):
a, b = map(int, input().split())
p = list(map(int, [input() for i in range(a)]))
m = 1000000000

p.sort()

for i in range(a-b+1):
  if p[i+b-1] - p[i] < m:
    m = p[i+b-1] -p[i]

print(m)

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

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

h.sort()

iter = len(h) - K

c_hmin = 99999999999999999999999999999999

for i in range(iter+1):

    hmin = h[K+i-1] - h[i]
    if c_hmin > hmin:
        c_hmin = hmin

print(c_hmin)

Solution (Python):
a, b = (int(i) for i in input().split()) 

list=[]
 
for i in range(a):
    list.append(int(input()))

new_list = sorted(list)

sabun = None

for i in range(len(new_list)):

    bbb = new_list[i+b-1] - new_list[i]

    if sabun == None:
        sabun = bbb
        continue

    if sabun > bbb:
        sabun = bbb

    if i+b == len(new_list):
        break
    
print(sabun)

Solution (Python):

l = [int(x) for x in input().split(' ')]
n,k = l
h = []

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

i,j = 0,k-1

r = float('inf')
while j < len(h):
	mn,mx=h[i],h[j]
	r = min(r, mx-mn)
	i,j=i+1,j+1

print(r)

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

hmax=0
hmin=99999999999999

n = int(n)
k = int(k)

n_list=[]
while True:
    try:
        n_list.append(int(input()))
    except EOFError:
        break

n_list.sort()

for i in range(0, n-k+1):
    diff = n_list[i+k-1] - n_list[ i ]
    if hmin > diff:
        hmin = diff

print( hmin )

Solution (Python):
import numpy as np
n, k = map(int, input().split())
h = np.array([int(input()) for _ in range(n)],dtype=int)
h.sort()
print(min([h[x+k-1] - h[x] for x in range(n-k+1)]))

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

Solution (Python):

def solve():
  n,k = map(int,input().split())
  t = []
  hl = []
  mi = 1000000000
  for i in range(n):
    hl.append(int(input()))
  hl.sort(reverse=True)
  for i in range(n):
    h = hl[i]
    if i + 1 > k:
       t.pop(0)
    t.append(h)
    if len(t) == k:
      sm = t[0] - t[k - 1]
      if mi > sm:
        mi = sm
  print(mi)

if __name__ == '__main__':
  solve()

Solution (Python):
a=[]
a=input().split(" ")
N=int(a[0])
K=int(a[1])
b=[]
b=[int(input()) for i in range(N)]
b.sort()
c=[]
for i in range(0,N-K+1) :
    b1=b[K-1+i]
    b2=b[i]
    d=b1-b2
    c.append(d)
c.sort()
p=c[0]
print(p)

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

p = []

for i in range(n) :
    t = int(input())
    p.append(t)
    
    

p.sort()

ans = 10**9+1
for i in range(n-k+1) :
    max = p[i+k-1]
    min = p[i]
    t = max - min
    if ans > t :
        ans = t
print(ans)

Solution (Python):
n, k = input().strip().split(' ')
n, k = [int(n),int(k)]
l = []
for i in range(n):
    h = input()
    h = int(h)
    l.append(h)
dl = sorted(l)
ans = float('inf')
for j in range(n-k+1):
    if abs(dl[j+k-1] - dl[j]) <= ans:
        ans = abs(dl[j+k-1] - dl[j])
print(int(ans))

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

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

print(min_l)

Solution (Python):
import collections

H, W = map(int, input().split())
S = [int(input()) for _ in range(H)]
sa = 1000000000000
c = collections.Counter(S)
B = c.most_common()
if B[0][1] >= W:
  print("0")

else:
  A = sorted(S, reverse=True)
  for i in range(len(A)-W+1):
    if sa > A[i]-A[i+W-1]:
      sa = A[i]-A[i+W-1]
  
    if sa == 1:
      break

  print(sa)

Solution (Python):
import numpy as np
N, K = map(int,input().split())
p = []
for i in range(N):
    p.append(int(input()))
p.sort()
p = np.array(p)
before = p[:N-K+1]
after = p[K-1:]
print(np.min(after-before))

Solution (Python):
NK = list(map(int, input().split()))
a = [0]*NK[0]
for i in range(len(a)):
    a[i] = int(input())
a.sort()
N = NK[0]
K = NK[1]

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

print(minimum)

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

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

h.sort()
mn = 1e+10
for i in range(N - K + 1):
    diff = h[i+K-1] - h[i]
    mn = min(mn, diff)
print(mn)

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

Solution (Python):

 
tmp=input()
n = int(tmp.split(" ")[0])
k = int(tmp.split(" ")[1])
 
h=[]
 
for i in range(n):
    h.append(int(input()))
 
h.sort()
 
x=int(0)
y=int(h[k-1]-h[0])
 
for i in range(n-k+1):
    x=h[i+k-1]-h[i]
    if x < y:
        y=x
 
print(y)

Solution (Python):
n,k = map(int,input().split())
list1 = []
for i in range(n):
    x = int(input())
    list1.append(x)
y = 10000000000
a = -1
list1.sort()
for i in range(n-k+1):
    z = list1[k+i-1]-list1[0+i]
    if y > z:
        y = z
print(y)

Solution (Python):
n,k = map(int,input().split())
h_list = [int(input()) for _ in range(n)]
h_list.sort(reverse=True)
i = 0
j = k-1
ans = 10**9

while j < n:
    diff = h_list[i]-h_list[j]
    ans = min(ans,diff)
    i += 1
    j += 1
print(ans)

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

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

t = 0

for i in range(n - k + 1):
    _t = h[i] - h[i + k - 1]
    if _t == 0:
        print(0)
        exit()
    else:
        if t == 0 or _t < t:
            t = _t

print(t)

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

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

    a = l[i+k-1]-l[i]
    if m > a:
        m = a
print(m)

Solution (Python):
N, X = [int(i) for i in input().split(' ')]
from collections import deque

import sys
hs = []
for line in sys.stdin:
    h, = line.strip().split(" ")
    hs.append(int(h))
hs = deque(sorted(hs))
counter = 0

minh = None
for i in range(N):
    if (i + (X-1)) > N-1:
        break
    ch = hs[i+(X-1)] - hs[i]
    if minh == None:
        minh = ch
    elif ch < minh:
        minh = ch
print(minh)

Solution (Python):
import numpy as np
n, k = map(int, input().split())
a = [int(input()) for x in range(n)]
a.sort()
a = np.array(a)
print(np.min(a[k - 1:] - a[:n - k + 1]))

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

Solution (Python):



N = list(map(int, (input().strip().split(' '))))
K = N[1]
N = N[0]
i = 0
h = []
while i < N:
  hi = int(input().strip())
  h.append(hi)
  i += 1
h = sorted(h)

i = 0
min_diff = 10000000000
while i < N - K + 1:
  diff = h[i+K-1] - h[i]
  if min_diff > diff:
    min_diff = diff
  i += 1
print(min_diff)

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

tree = sorted(tree)

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

print(minimum)

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

Solution (Python):
import numpy as np
from sys import stdin
N,K=[int(x) for x in stdin.readline().rstrip().split()]
data=[]
for i in range(N):
    data.append(int(input()))
data.sort()
data.reverse()
ans=float("inf")
for i in range(N-K+1):
    tmp=data[i]-data[i+K-1]
    if ans > tmp:
        ans=tmp
print(ans)

Solution (Python):

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

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

sort_h = sorted(h)
diff_h = 0
ans = []
for i in range(len(h)-K+1):
    diff_h = sort_h[i+(K-1)] - sort_h[i] 
    ans.append(diff_h)

print(min(ans))

Solution (Python):
import itertools

total, select = map(int, input().split(' '))

trees = []

for _ in range(total):
  trees.append(int(input()))

"""
result = 10000000000
for t in list(itertools.combinations(trees, select)):
  result = min(result, max(t)-min(t))
"""

trees.sort()

result = 10000000000
for i in range(total-select+1):  
  result = min(result, trees[i+select-1] - trees[i])

print(result)

Solution (Python):
N, K = map(int,input().split())
H = [int(input()) for k in range(N)]
H = sorted(H)
ans = float("inf")
for k in range(N-K+1):
    ans = min(ans, H[k+K-1]-H[k])
print(ans)

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

arr = []
for i in range(n):
	arr += [int(input())]
	
arr2 = sorted(arr)

ans = float('inf')

for y in range(n-k+1):
	mx = arr2[y+k-1]
	mn = arr2[y]
	ans = min(ans,mx-mn)

print(ans)

Solution (Python):
n, k = map(int, input().split())
h = []
for i in range(n):
    h.append(int(input()))
h.sort(reverse = True)
ans = 10000000000
maxh = h[:k]
maxn = 0
for i in range(k, n):
    if ans > maxh[maxn] - maxh[(maxn-1)%k]:
        ans = maxh[maxn] - maxh[(maxn-1)%k]
    maxh[maxn] = h[i]
    maxn = (maxn+1) % k
if ans > maxh[maxn] - maxh[(maxn-1)%k]:
    ans = maxh[maxn] - maxh[(maxn-1)%k]
print(ans)

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

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

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


h.sort()

smallest = 10**9

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

print(smallest)

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 = list(map(int,input().split()))
h = []
max_a = 1000000000

for i in range(n_k[0]):
    h.append(int(input()))

h.sort(reverse = True)

for j in range(n_k[0] - (n_k[1]-1)):
    a = h[j] - h[j + (n_k[1]-1)]
    if a <= max_a:
        max_a = a

print(int(max_a))

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

for _ in range(N-K+1):
    num = hlist[_]-hlist[_+(K-1)]
    sa.append(num)
sa.sort()
print(sa[0])

Solution (Python):
n,k = map(int,input().split())
hei_list = []
for i in range(n):
    tmp = int(input())
    hei_list.append(tmp)
hei_list.sort()
for i in range(n-k+1):
    tmp_dist = hei_list[i + k - 1] - hei_list[i]
    if i == 0:
        min_dist = tmp_dist
    elif tmp_dist < min_dist:
        min_dist = tmp_dist
print(min_dist)

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

s = sorted(h)
candidate = 10 ** 9

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

print(candidate)

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

sa.sort()
print(sa[0])

Solution (Python):
N,n = map(int,input().split())
 
 
N = [int(input()) for i in range(N)]
N = sorted(N)
Q = []
for i in range(len(N)-n+1):
	max1 = int(*N[i+n-1:i+n])
	min1 = int(*N[i:i+1])
	Q.append(max1 - min1)
print(min(Q))

Solution (Python):
N,K = map(int,input().split())
list = []
m = -1
for i in range(N):
    list.append(int(input()))
List = sorted(list)
for i in range(N-K+1):
    mt = List[i+K-1]-List[i]
    if m == -1 or m > mt:
        m = mt
print(m)

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

shlist = sorted(hlist)
result = 1000000000
for i in range(n-k+1): 
  sa = abs(shlist[i] - shlist[i+k-1])
  if result > sa:
    result = sa

print(result)

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

Solution (Python):
import sys
N,K=map(int, raw_input().split())

H=[]
for i in range(N):
	h=int( sys.stdin.readline() ) 
	H.append(h)

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

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