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



Sample input:
5 3
10
15
11
14
12


Sample output:
2

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
N, K = map(int, input().split())
h = [None] * N
for i in range(N):
    h[i] = int(input())
h.sort()
print(min([h[i + K - 1] - h[i] for i in range(N - K + 1)]))

Submission status: Accepted

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

_min = h[k-1] - h[0]
for i in range(k, len(h)):
    if h[i] - h[i - k + 1] <= _min:
        _min = h[i] - h[i - k + 1]
print(_min)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
NK = input()
NK = NK.split(" ")
N = int(NK[0])
K = int(NK[1])


h = []
for i in range(N):
  h.append(int(input()))
  
  
h.sort()
ans = h[-1] - h[0]

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

Submission status: Accepted

Solution code:
N,K = map(int,raw_input().split())
a = []
for i in range(N):
    a.append(int(raw_input()))
a.sort()
b = []
for i in range(N-K+1):
    b.append(a[i+K-1]-a[i])
print(min(b))

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

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)

Submission status: Accepted

Solution code:
import sys
n=input()
list = n.split(" ")
n=int(list[0])
k=int(list[1])

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

def merge_sort(alist):
  if len(alist) <= 1:
    return alist
  
  mid = int(len(alist)/2)
  left = alist[:mid]
  right = alist[mid:]
  
  left = merge_sort(left)
  right = merge_sort(right)
  
  return merge(left, right)

def merge(left,right):
  sorted_list = []
  left_index = 0
  right_index = 0
  
  while left_index < len(left) and right_index < len(right):
    if left[left_index] >= right[right_index]:
      sorted_list.append(left[left_index])
      left_index += 1
    else:
      sorted_list.append(right[right_index])
      right_index += 1
      
  if left:
    sorted_list.extend(left[left_index:])
    
  if right:
    sorted_list.extend(right[right_index:])
    
    return sorted_list

trees = merge_sort(trees)

ans=sys.maxsize
flag=0
temp = []

for i in range(len(trees)-k+1):
  diff = trees[i]-trees[i+k-1]
  if(diff==0):
    ans=0
    break
  if(ans>diff):
    ans=diff
  temp.clear()

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n,k = input().split()
n=int(n)
k=int(k)
h = [int(input()) for i in range(n)]
h.sort()
#print(h)
ans=10000000000

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

Submission status: Accepted

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

Submission status: Accepted

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

H.sort()
ans = H[-1] - H[0]

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, K = map(int,input().split())
trees = []
for i in range (N):
    temp = int(input())
    trees.append(temp)
trees.sort()
ans = 10**9
for i in range (N-K+1):
    low = trees[i]
    high = trees[i+K-1]
    diff = high-low
    if diff < ans:
        ans = diff
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/mnt/c/Users/moiki/bash/env/bin/python
N, K = map(int, input().split())
# = int(input())
h = []
sums = [0]
for i in range(N):
    h.append(int(input()))

h = sorted(h)
diff = []
bef = 0
for e,i in enumerate(h):
    if e == 0:
        bef = i
    else:
        diff.append(i - bef)
        bef = i
        # sums.append(diff[e-1])

diffin = 0
for n in diff:
    sums.append(sums[diffin] + n  )
    diffin += 1

# print("sums: {}".format(sums))
mins = 10e9
leng = len(sums)
for i in range(leng-K+1):
    # print("sums[{}]: {}, sums[{}]: {}".format(i+K-1,sums[i+K-1], i, sums[i]))
    mins = min(mins, sums[i+K-1] - sums[i])
print(mins)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

c = [a[i + k - 1] - a[i] for i in range(n - k + 1)]

print(min(c))

Submission status: Accepted

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

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

print(min(ans))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
l=[int(input()) for i in range(n)]
l.sort()
mi=1000000000
for i in range(n-k+1):
    mi=min(mi,l[i+k-1]-l[i])
print(mi)

Submission status: Accepted

Solution code:
N,K=list(map(int,input().split()))
h=[int(input()) for i in range(N)]
 
h.sort()
 
m="a"
for i in range(N-K+1):
	if m=="a" or m>h[i+K-1]-h[i]: m=h[i+K-1]-h[i]
 
print(m)

Submission status: Accepted

Solution code:
N,K = list(map(int,input().strip().split()))
ans = int(10e10)

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

hlist.sort()

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

print(ans)

Submission status: Accepted

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

h.sort()

m="a"
for i in range(N-K+1):
	if m=="a" or m>h[i+K-1]-h[i]: m=h[i+K-1]-h[i]

print(m)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
l=[int(input()) for i in range(n)]
l.sort()
mi=10000000000000000
for i in range(n-k+1):
    mi=min(mi,l[i+k-1]-l[i])
print(mi)

Submission status: Accepted

Solution code:
s = input().split(" ")
N = int(s[0])
K = int(s[1])

h = []
for i in range(N) : 
    s = input()
    h.append(int(s))
    
h = sorted(h)
minDiff = h[len(h) - 1] - h[0]
for i in range(N) : 
    try : 
        diff = h[i + K - 1] - h[i]
        minDiff = min(minDiff , diff)
        
    except : 
        if i == 0 : minDiff = 0
        continue
print(minDiff)

Submission status: Accepted

Solution code:
N,K = list(map(int, input().split()))
H = []
for i in range(N):
  H.append(int(input()))
H.sort()
min = H[K-1] - H[0]
for i in range(K, N):    
  t = H[i] - H[i-K+1]
  if min > t:
    min = t
print(min)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,K = list(map(int, input().split()))
h = [int(input()) for i in range(N)]
candidate = None
h.sort()
for i in range(N-K+1):
    tmp = h[i+K-1] - h[i]
    if candidate == None or tmp < candidate:
        candidate = tmp
print(candidate)

Submission status: Accepted

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

Submission status: Accepted

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

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

print(int(m))

Submission status: Accepted

Solution code:
def main():
  N, K = map(int, input().split(' '))
  # N本の木があって，i本目の木はh_i[m]
  # K本選んで電飾する.
  # 木の内一番高いのをh_max[m], 低いものをh_min[m]
  # min(h_max - h_min)をしたい.
  trees = [int(input()) for _ in range(N)]
  trees.sort()
  trees = [n - trees[0] for n in trees]
  # print(trees[K-1] - trees[0]) -> これだと，最初の方はかなり分散が大きい配列で，後ろの方が分散が小さい配列だとダメ
  # 例[0, 100, 101, 102] から2個選ぶ時, 上記だと100が帰ってくる.
  # MIN = 100000000000000
  # print(trees)
  # for n in range(K):
  # for n in range(N - K + 1):
  #   t = trees[n+K-1] - trees[n]
  #   MIN = min(t, MIN)
  MIN = min([trees[n+K-1] - trees[n] for n in range(N-K+1)])
  print(MIN)

main()

Submission status: Accepted

Solution code:
from itertools import combinations

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

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

hs.sort()

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

print(R)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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)]))

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
H = [int(input()) for i in range(N)]
H.sort()
ans = float('inf')
now = sum(H[:K])
for i in range(N-K+1):
    ans = min(ans, abs(H[i+K-1]-H[i]))
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,k = map(int, input().split())
d = sorted([int(input()) for _ in range(n)],reverse=True)
ret = 9999999999
#print(d)
for i in range(n-k+1):
    #print(i,i+k-1)
    ret = min(ret,d[i]-d[i+k-1])
print(ret)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
l=[int(input()) for i in range(n)]
l.sort(reverse=True)
#print(l,n,k)
ans=float('inf')
for i in range(n-k+1):
    a=l[i]-l[i+k-1]
    ans=min(ans,a)
print(ans)

Submission status: Accepted

Solution code:
import collections

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

h.sort(reverse=True)
# c = collections.Counter(h)
# flg = False
# for i in c:
#     if c[i] >= k:
#         print(0)
#         flg = True
#         break

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from itertools import starmap
from operator import sub

n, k = map(int, input().split())
hhh = sorted(int(input()) for _ in range(n))
print(min(starmap(sub, zip(hhh[k - 1:], hhh))))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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()

#print(trees)
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)

Submission status: Accepted

Solution code:
N, K, *H = map(int, open(0).read().split())
H.sort()
print(min(y - x for x, y in zip(H, H[K - 1:])))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
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
#print(h)
for i in range(len(h)-K+1):
    #print(h[i], h[i+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)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
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)
#print(h)
for i in range(len(h)-K+1):
    #print(h[i], h[i+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)

Submission status: Accepted

Solution code:
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)]))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# coding: utf-8
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)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# coding: utf-8
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)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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')

Submission status: Accepted

Solution code:
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:])))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

for i in range(n):
    h.append(int(input()))
min = 10 ** 10
#  print(h)
h = sorted(h)
#  print(h)
for i in range(n-k+1):
    #  print(h[i:i+k-1])
    div = h[i+k-1] - h[i]
    if div < min:
        min = div
    if min == 0:
        print(min)
        exit()
print(min)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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
        #if iDiff == 0:
        #   break
    print(iDiff)
解()

Submission status: Accepted

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

Submission status: Accepted

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

h = []

#print(n)
#print(k)

for i in range(n):

    h.append(int(input()))

h.sort()

min = 9999999999

#print(h)

for j in range(n):

    z = j + k-1

    if z > n-1:
        break

    sa = h[z] - h[j]

    #print(sa)

    if sa < min:

        min = sa


print(min)

Submission status: Accepted

Solution code:
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'))))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# vim: set fileencoding=utf-8:
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()

Submission status: Accepted

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

Submission status: Accepted

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