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:
def main():
    n,k=map(int,input().split())
    h=sorted(int(input()) for _ in range(n))
    hs = []
    for i in range(n-k+1):
        hs.append(h[i+k-1] - h[i])
    print(min(hs))
    
    
if __name__ == "__main__":
    main()

Submission status: Accepted

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

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

Submission status: Accepted

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


if __name__ == '__main__':
    solve()

Submission status: Accepted

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

if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
lst = []
for i in range(n):
  lst.append(int(input()))
lst.sort()
res = 10 ** 9
for i in range(n - k + 1):
  temp = lst[i + k - 1] - lst[i]
  if temp < res:
    res = temp
print(res)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
H = [int(input()) for _ in range(N)]
H.sort()
df = 0
min_h = H[K-1]-H[0]

for i in range(1, N-K+1):
    new_h = H[i+K-1]-H[i]
    if new_h < min_h:
        min_h = new_h

print(min_h)

Submission status: Accepted

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

h.sort()

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

print(min(ans_l))

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
hh = []
minn = 10**12
for i in range(n):
    h = int(input())
    hh.append(h)
h = sorted(hh)
for i in range(n - k + 1):
    if h[i + k - 1] - h[i] < minn:
        minn = h[i + k - 1] - h[i]
print(minn)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
h=[int(input()) for i in range(n)]
h.sort()
ans=float("inf")
for i in range(len(h)-k+1):
	ma = h[i+k-1]
	mi = h[i]
	ans = min(ans,ma-mi)

print(ans)

Submission status: Accepted

Solution code:
#普通にソートしてから、配列のK個づつ区切ってパターンを調べればN - K + 1回の比較で行ける

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

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

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
arr = sorted([int(input()) for _ in range(N)])
ans = float('inf')

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

Submission status: Accepted

Solution code:
N, K, *h = map(int, open(0).read().split())

h_sort = sorted(h)

ans = 10**9

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

Submission status: Accepted

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

Submission status: Accepted

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


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
def resolve():
    N, K = list(map(int, input().split()))
    h = [int(input()) for _ in range(N)]
    ans = 10 ** 10
    h.sort()

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


resolve()

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
h=[int(input()) for i in range(n)]
h.sort()
ans=h[k-1]-h[0]
res=float("INF")
for i in range(n-k+1):
    res=h[i+k-1]-h[i]
    if ans>res:
        ans=res
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

def merge(array):                                   # line1
    mid = len(array)                                # line2
    if mid > 1:                                     # line3
        left = merge(array[:(mid//2)])               # line4
        right = merge(array[(mid//2):])              # line5
        array = []                                  # line6
        while len(left) != 0 and len(right) != 0:   # line7
            if left[0] < right[0]:                  # line8
                array.append(left.pop(0))           # line9
            else:                                   # line10
                array.append(right.pop(0))          # line11
        if len(left) != 0:                          # line12
            array.extend(left)                      # line13
        elif len(right) != 0:                       # line14
            array.extend(right)                     # line15
    return array



for i in range(n):
	a=int(input())
	S.append(a)
L=merge(S)

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:

N, K = map(int, input().split())
h = []
ans = 10**9

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

h.sort()

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())
a=[int(input()) for i in range(n)]
a.sort()
b=[]
for p in range(n-k+1):
  c=a[p+k-1]-a[p]
  b.append(c)
  
print(min(b))

Submission status: Accepted

Solution code:
import sys
import math
import itertools as it
def I():return int(sys.stdin.readline().replace("\n",""))
def I2():return map(int,sys.stdin.readline().replace("\n","").split())
def S():return str(sys.stdin.readline().replace("\n",""))
def L():return list(sys.stdin.readline().replace("\n",""))
def Intl():return [int(k) for k in sys.stdin.readline().replace("\n","").split()]
def Lx(k):return list(map(lambda x:int(x)*-k,sys.stdin.readline().replace("\n","").split()))

if __name__ == "__main__":
    n ,k = I2()
    h = [I() for _ in range(n)]
    h.sort()
    ans = float("inf")
    for i in range(n-k+1):
        a,b = h[i],h[i+k-1]
        #print(a,b)
        ans = min(ans,b-a)
    print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys

def main():
    input = sys.stdin.readline
    N, K = map(int, input().rstrip().split())
    trees = []

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

    trees.sort()
    min_diff = float("inf")

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



if __name__ == '__main__':
    main()

Submission status: Accepted

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

def quicksort(a, left: int, right: int):
    #a[left]〜a[right]をクイックソート
    pl=left
    pr=right
    pivot= a[(left +right)//2]

    #print(f'a[{left}]~a[{right}] : ', *a[left : right +1])

    while pl<=pr:
        while a[pl] <pivot: pl+=1
        while a[pr] >pivot: pr-=1
        if pl <= pr:
            a[pl], a[pr]= a[pr], a[pl]
            pl+=1
            pr-=1

    if left < pr: quicksort(a, left, pr)
    if pl < right: quicksort(a, pl, right)

def quick_sort(a):
    quicksort(a, 0, len(a)-1)
mn=10**9
quick_sort(h)
for i in range(n-k+1):
    dif=h[i+k-1]-h[i]
    if dif<mn:
        mn=dif

print(mn)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def mi():
    return map(int, input().split())

def ii():
    return int(input())

def main():
    N, K = mi()
    h = [ii() for _ in range(N)]
    h.sort()
    print(min(h[i+K-1]-h[i] for i in range(N-K+1)))

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

#n,k = 7,3
#h = [10,11,12,14,15,20,0]
h.sort()

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

print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
n, k, *h = map(int, open(0).read().split())
h.sort()
ans = []
for i in range(n-k+1):
    ans += [h[k+i-1] - h[i]]
print(min(ans))

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
def main():
    N, K = map(int, input().split())
    h = [int(input()) for i in range(N)]
    H = sorted(h)

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

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
hh = [0] * n
for i in range(n):
    hh[i] = int(input())

diff = float('inf')
hh = sorted(hh)
for i in range(n - k + 1):
    diff = min(diff, hh[i + k - 1] - hh[i])
print(diff)

Submission status: Accepted

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

S.sort()
minh=10**10
for i in range(N-K+1):
    if minh>S[i+K-1]-S[i]:
        minh=S[i+K-1]-S[i]

print(minh)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,k = map(int, input().split())
H = [int(input()) for i in range(n)]
H.sort()
ans = max(H)
for i in range(n-k+1):
  p = H[i+k-1] - H[i]
  ans = min(ans,p)
  if ans == 0:
    print(0)
    exit()
  
print(ans)

Submission status: Accepted

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

h.sort()
ans=10**10

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

print(ans)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
h = [int(input()) for _ in range(N)]
ans = float('inf')
h.sort()
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())
l=list()
for i in range(N):
  l.append(int(input()))
l.sort()
ans=int(l[K-1])-int(l[0])
for i in range(1,N-K+1):
  key=int(l[i+K-1])-int(l[i])
  if ans>key:
    ans=key
print(ans)

Submission status: Accepted

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

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

lst=[0]*(n-1)

temp=0
for i in range(n-1):
    temp=l[i+1]-l[i]
    lst[i]=temp

ll=[0]*(n)
temp=0
for ii in range(n-1):
    temp+=lst[ii]
    ll[ii+1]=temp

#print(lst)
#print(ll)
ans=10**9    
for j in range(n-k+1):
    ans=min(ans,ll[j+k-1]-ll[j])
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# 20200612_yorukatsu_c.py
N, K = map(int,input().split())
M = [int(input()) for i in range(N)]
M.sort()
lis = [0]*(N-K+1)
for i in range(N-K+1):
    lis[i] = abs(M[i]-M[i+K-1])
print(min(lis))

Submission status: Accepted

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

Submission status: Accepted

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

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

print(mn)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3


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

h.sort(reverse=True)

ans = 10**10
for i in range(0, n-k+1):
    max_ = h[i]
    min_ = h[i+k-1]
    ans = min(ans, max_-min_)
print(ans)

Submission status: Accepted

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

tmp = 10000000000

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

Submission status: Accepted

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

ans = 1000000000

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

print(ans)

Submission status: Accepted

Solution code:
# ans

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

Submission status: Accepted

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

print(ans)

Submission status: Accepted

Solution code:
def inp():return map(int,input().split())
def ip():return int(input())

n,k=inp()
h=[]
for _ in range(n):
    h.append(ip())
h.sort()

ans=10**18
i=0
while i+k-1<n:
    ans=min(ans,h[i+k-1]-h[i])
    i+=1
print(ans)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
h=[int(input()) for _ in range(n)]
h.sort()
INF=2*10**9
ans=INF
for i in range(n-k+1):
	diff=h[i+k-1]-h[i]
	if ans>diff:
		ans=diff
		if ans==0:
			print(0)
			exit()
print(ans)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
a=sorted([int(input())for i in range(n)])
mim=10**9
for i in range(n-k+1):
    sa=a[i+k-1]-a[i]
    mim=min(mim,sa)
print(mim)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
h=[int(input()) for _ in range(n)]
h_sort=sorted(h)
a=0
b=10**20
for i in range(k, n+1):
	a=h_sort[i-1] - h_sort[i-k]
	if a<b:
		b=a
print(b)

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
h = [int(input()) for i in range(n)]
h.sort()
ans = 1111111111
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=pow(10,9)
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 = 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:
#!/usr/bin/env python3

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

h = sorted(h)
ans = 1001001001
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=[0]*n
for i in range(n):
  h[i]=int(input())
h.sort()
ans=99999999999
for i in range(n-k+1):
  ans=min(ans,abs(h[i]-h[k+i-1]))
print(ans)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
apple = []
orange = []
for _ in range(n):
    apple.append(int(input()))
a = sorted(apple)
for i in range(n - k):
    b = - a[i] + a[i + k -1]
    c = a[-i-1] - a[-(i + k)]
    if b != 0 or c != 0:
        orange.append(min(b, c))
    else:
        orange.append(0)
        break
print(min(orange))

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
x = [int(input()) for i in range(n)]
x.sort()
ans = float('inf')
for i in range(n-k+1):
    ans = min(ans,x[i+k-1]-x[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 i in range(n-k+1):
    ans=min(ans,h[i+k-1]-h[i])
    if ans==0:
        print(ans)
        exit()
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
hs=sorted([int(input()) for _ in range(n)])
m=float('inf')
for l,h in zip(hs,hs[k-1:]):
    m=min(m,h-l)
print(m)

Submission status: Accepted

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

h = list(sorted([int(input()) for i in range(N)]))
h_dif = [h[i+K-1] - h[i] for i in range(N-K+1)]
print(min(h_dif))

Submission status: Accepted

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

print(min([b-s for s,b in zip(h, h[k-1:])]))

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

n, k, *h = map(int, read().split())

h.sort()
ans = 10 ** 9
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=[0]*(n-k+1)
for i in range(n-k+1):
    ans[i]=h[k+i-1]-h[i]
print(min(ans))

Submission status: Accepted

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

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

h.sort()
ans = float('inf')
for i in range(n-k+1):
    x = h[i+k-1] - h[i]
    if x < ans:
        ans = x

print (ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
hs = []
for _ in range(N):
  hs.append(int(input()))
hs.sort()
  
start, end = 0, K-1
ans = float('inf')
while end < N:
  ans = min(ans, hs[end]-hs[start])
  start += 1
  end += 1
  
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 i in range(n - k + 1):
    ans = min(ans, h[i + k - 1] - h[i])
print(ans)

Submission status: Accepted

Solution code:
def main():
    n, k = map(int, input().split()) 
    h = [int(input()) for _ 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)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

Submission status: Accepted

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

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

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

print(ans)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
h = sorted([int(input()) for x in range(n)])
d = 10**9

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

    if d == 0:
        break

print(d)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
h = [int(input()) for _ in range(N)]
h.sort()
diff = max(h)
for i in range(0, len(h)-K+1):
  if diff > h[i+K-1]-h[i]:
    diff = h[i+K-1]-h[i]
print(diff)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, K, *h = map(int, open(0).read().split())
h = sorted(list(h))
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:
n, k = map(int, input().split())
H = sorted([int(input()) for i in range(n)])
minv = float('inf')
for x, y in zip(H, H[k-1:]):
	minv = min(minv, y - x)
print(minv)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# Python3 (3.4.3)
import sys
input = sys.stdin.readline

# -------------------------------------------------------------
# function
# -------------------------------------------------------------


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

H.sort()

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

print(ans)

Submission status: Accepted

Solution code:
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[k+i-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()

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

print(mini)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
tree = [int(input()) for _ in range(n)]
tree.sort()
ans = 10 ** 9 + 7

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

print(ans)

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
arr = []

for i in range(N):
  a = int(input())
  arr.append(a)
  
arr.sort()
ans = 10**10

for i in range(N-K+1):
  a = arr[i]
  b = arr[i+K-1]
  c = b-a
  ans = min(ans,c)
  
print(ans)