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:
n, k= map(int, input().split())
h_list = []

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

h_list.sort()
result = float("inf")

for i in range(n-k+1):
    r = h_list[i+k-1] - h_list[i]
    if result >= r:
        result = r
print(result)

Submission status: Accepted

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

h.sort()
ans1=10**10
for i in range(N-K+1):
    ans2=0
    ans2=h[i+K-1]-h[i]
    if ans1 > ans2:
        ans1=ans2
print(ans1)

Submission status: Accepted

Solution code:
A,B = map(int,input().split())
P = [int(input()) for i in range(A)]
P.sort()
#print(P)
ans = 2147483647
for n in range(len(P)-B+1):
    ans = min(ans,P[n+B-1]-P[n])
    
print(str(ans))

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
#coding:utf-8
n,k = map(int,input().split())
ls = []
for i in range(n):
    ls.append(int(input()))
#ls = [100000-i for i in range(100000)]
#n = 100000
#k = 1000
ls = sorted(ls)
ans = 1000000000
for i in range(n-k+1):
    mx = ls[i+k-1]
    mn = ls[i]
    ans = min(ans,mx - mn)
print (ans)

Submission status: Accepted

Solution code:
import numpy as np
N,K = map(int,input().split(' '))
h = []
for i in [0]*N:
	h.append(int(input()))


def sort(l):
	m = sum(l)/len(l)
	a = [x for x in l if x < m]
	b = [x for x in l if m <= x]
	if a == []:
		return b
	if b == []:
		return a
	return sort(a)+sort(b)

h = np.array(h)
sh = np.sort(h)
#sh = sort(h)
ans = max(h)
for i in range(0,N-K+1):
	s = (sh[i+K-1] - sh[i])
	if s < ans:
		ans = s
print(ans)

Submission status: Accepted

Solution code:
import numpy as np

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

h.sort()

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

Submission status: Accepted

Solution code:
import numpy as np
def main():
	
	buf = input().split()
	n = int(buf[0])
	k = int(buf[1])
	l = np.zeros(n)
	for i in range(n):
		l[i] = int(input())
	
	l = np.sort(l)
	min = -1
	for i in range(n-k+1):
		bufmin = l[i+k-1] - l[i]
		if (min == -1):
			min = bufmin
		elif (bufmin < min):
			min = bufmin
		
	print(int(min))
		
main()

Submission status: Accepted

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

height_list = []
for i in range(n):
    height_list.append(int(input()))
# print(height_list)

min = 2000000000
height_list.sort()
# print(height_list)

for start in range(0, len(height_list) - k + 1):
    # print("start is {}".format(height_list[start]))
    # print("end is {}".format(height_list[start + k -1]))

    x = height_list[start + k - 1] - height_list[start]
    if x < min:
        min = x

print(min)

Submission status: Accepted

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

Submission status: Accepted

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

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

h.sort()

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

print(ans)

Submission status: Accepted

Solution code:
N, K = list(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:
def solve(n, k, hs):
    gs = sorted(hs)
    return min(gs[i + k - 1] - gs[i] for i in range(n - k + 1)) 


def main():
    n, k = (int(z) for z in input().split())
    hs = [int(input()) for _ in range(n)]
    res = solve(n, k, hs)
    print(res)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

height.sort()
dh = 10 ** 10
for i in range(n - k + 1):
  if height[i + k -1] - height[i] < dh:
    dh = height[i+k-1] - height[i]

print(dh)

Submission status: Accepted

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

d = float('inf')
for i in range(N - K + 1):
    div = s_h[i + K - 1] - s_h[i]
    if div < d:
        d = div
print(d)

Submission status: Accepted

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

MIN = 10**10
for i in range(N-K+1):
	if h[i+K-1] - h[i] <= MIN:
		MIN = h[i+K-1] - h[i]
# print(i,h[i],h[i+K-1])
print(MIN)

Submission status: Accepted

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

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

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_ = k-1
for i in range(n-k+1):
    tmp = trees[i+k_] - trees[i]
    if tmp < min_h:
        min_h = tmp

print(min_h)

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
h_list = [int(input()) for _ in range(N)]
h_list_s = sorted(h_list)
min_d = 1000000000
for k in range(0,N-K+1):
  tmp = h_list_s[k+K-1] - h_list_s[k]
  if min_d > tmp:
    min_d = tmp
    
print(min_d)

Submission status: Accepted

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

H.sort(reverse=True)

max_num = H[0]
min_num = H[K-1]
diff = max_num - min_num
l_h = len(H)
for i in range(l_h-K+1):
    max_num = H[i]
    min_num = H[i+K-1]
    _diff = max_num - min_num
    if _diff < diff:
        diff = _diff
print(diff)

Submission status: Accepted

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

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

minimum = 1000000000
for i in range(n - k + 1):
    val = highs[i + k - 1] - highs[i]
    if val < minimum:
        minimum = val

print(minimum)

Submission status: Accepted

Solution code:
tree_list = []
n = 0
k = 0
tree_max = 0
tree_min = 0

n, k = input().split(' ')
n = int(n)
k = int(k)
for i in range(n):
    tree = int(input())
    tree_list.append(tree)

tree_list.sort()
diff = tree_list[n - 1]
for i in range(n - k + 1):
    tree_min = tree_list[i]
    tree_max = tree_list[i + k - 1]
    if tree_max - tree_min < diff:
        diff = tree_max - tree_min

print(diff)

Submission status: Accepted

Solution code:
N,K = map(int,input().split(" "))
height = []
for i in range(N):
    height.append(int(input()))
height.sort()
min = 10**9
n = N-K+1
for j in range(n):
    hmin = height[j]
    hmax = height[j+K-1]
    hei = hmax-hmin
    if hei <= min:
        min = hei
print(min)

Submission status: Accepted

Solution code:
m = input()
n = int(m.split()[0])
k = int(m.split()[1])
t = []
for i in range(n):
    t.append(int(input()))
t.sort()
a = None
for i in range(len(t)-k+1):
    w = abs(-1*t[i] + t[i+k-1])
    if i == 0:
        a = w
    elif a > w:
        a = w
print(a)

Submission status: Accepted

Solution code:
# coding:utf-8

import sys
from collections import Counter


input = sys.stdin.readline
INF = float('inf')
MOD = 10 ** 9 + 7


def inpl(): return list(map(int, input().split()))


N, K = inpl()

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

C = Counter(H)

H2 = sorted(H)
S = [0]
for i in range(N - 1):
    S.append(S[-1] + H2[i + 1] - H2[i])

# print(H2)
ans = INF
for i in range(N - K + 1):
    # print(i + K - 1, S[i + K - 1] - S[i])
    ans = min(ans, S[i + K - 1] - S[i])

print(ans)

Submission status: Accepted

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

for i in range(N-K+1):
    if h_list[i+K-1] - h_list[i] < ans:
        ans = h_list[i+K-1]-h_list[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()
dim=[]
for i in range(n-k+1):
    dim.append(h[i+k-1]-h[i])
dim.sort()
print(dim[0])

Submission status: Accepted

Solution code:
n, k = tuple(map(int, input().split()))
arr = [0 for i in range(n)]
sums = 0
for i in range(n):
    inp = int(input())
    arr[i] = inp

arr.sort()

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

print(sa)

Submission status: Accepted

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

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

h.sort()
for i in range(n-k+1):
  m.append(h[i+k-1]-h[i])
m.sort()
print(m[0])

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()
d_min = h[-1] - h[0]
# print(h)
# print(d_min)
for i in range(0, N - K+1):
    h_min = h[i]
    h_max = h[i + K - 1]
    d = h_max - h_min
    # print(i, h_min, h_max, d)
    if d < d_min:
        d_min = d
    # print('min=', d_min)
print(d_min)

Submission status: Accepted

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

N = int(N)
K = int(K)

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

h_list = sorted(h_list)

hmin = max(h_list)
for i in range(N-K+1):
    h_s = h_list[i]
    h_b = h_list[i+K-1]
    if h_b - h_s < hmin:
        hmin = h_b - h_s

print(hmin)

Submission status: Accepted

Solution code:
import sys


def main() -> None:
    n, k = [int(s) for s in input().split()]
    h = []
    for i in range(n):
        h += [int(input())]
    h.sort()

    mn = sys.maxsize
    for i in range(n - k + 1):
        cnt = h[i + k - 1] - h[i]
        mn = min(mn, cnt)

    print(mn)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

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

Hs = sorted(hs)
min = 10000000000000

for i in range(N - K + 1):
    right = i + K - 1
    diff = Hs[i + K -1] - Hs[i]
    if diff < min:
        min = diff

print(min)

Submission status: Accepted

Solution code:
N, K  = list(map(int, input().split()))
h = []
for i 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())
a = sorted([int(input()) for i in range(N)])
ans = float('inf')
for i in range(N-K+1):
    ans = min(ans, a[i+K-1] - a[i])
print(ans)

Submission status: Accepted

Solution code:
from sys import stderr, setrecursionlimit
from functools import reduce
from operator import add
import statistics
setrecursionlimit(2147483647)


def f():
    return [int(i) for i in input().split()]


def debug(*x, sep=" ", end="\n"):
    for item in x:
        stderr.write(str(item))
        stderr.write(sep)
    stderr.write(end)


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

h.sort()
min_dif = 10**10
for i in range(n-k+1):
    t = h[i+k-1] - h[i]
    if min_dif > t:
        min_dif = t
print(min_dif)

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(reverse=True)
d = []
for i in range(N-K+1):
  d.append(h[i] - h[i+K-1])
print(min(d))

Submission status: Accepted

Solution code:
def main(hs, K):
    hs.sort()
    print(min(hs[i+K-1] - hs[i] for i in range(len(hs)-K+1)))



#main([10,15,11,14,12], 3)
#main([5,7,5,7,7],3)


if __name__ == "__main__":
    N, K = (int(x) for x in input().split())

    hs = []
    for i in range(N):
        hs.append(int(input()))
    main(hs, K)

Submission status: Accepted

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

h.sort()
ans = 10**12
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()
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 = list(map(int, input().split()))
trees = []

for _ in range(n):
	h = int(input())
	trees.append(h)

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

print(min(subs))

Submission status: Accepted

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

sort_height = sorted(height)
dif_height = [abs(sort_height[i] - sort_height[i+K-1]) for i in range(N-K+1)]

#print(dif_height)
print(min(dif_height))

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
hList=[]
for i in range(n):
  hList.append(int(input()))
hList.sort()
#print(hList)
ans=100000000000000
for i in range(n-k+1):
  tmp=hList[i+k-1]-hList[i]
  if(ans>tmp):
    ans=tmp
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() # sort

re = 10**9
for i in range(N - K + 1):
    min_h = h[i]
    max_h = h[i+K-1]
    diff = max_h - min_h
    if diff < re:
        re = diff
print(re)

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 = list(map(int, input().split()))

h = []

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

h.sort()

m = 9999999999999999999

for i in range(len(h)-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 = (int(i) for i in input().split())

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

h.sort()

min = 1000000000

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:
def c_christmas_eve(N, K, H):
    H.sort()
    ans = float('inf')
    for j in range(N - K + 1):
        h_max = H[j + K - 1]
        h_min = H[j]
        ans = min(ans, h_max - h_min)
    return ans

N, K = [int(i) for i in input().split()]
H = [int(input()) for _ in range(N)]
print(c_christmas_eve(N, K, H))

Submission status: Accepted

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

Submission status: Accepted

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

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

h.sort()

res = h[K-1] - h[0]

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

print(res)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

h.sort()

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

print(min)

Submission status: Accepted

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

h.sort()
ans = 999999999999999999
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 _ in range(N)]
for i in range(N):
    h[i] = int(input())
    
h.sort(reverse=True)

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

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
h_arr = []
for i in range(N):
    h_arr.append(int(input()))
h_arr = sorted(h_arr)
min_diff = 10**9
for i in range(N-K+1):
    diff = h_arr[i+K-1] - h_arr[i]
    if min_diff > diff:
        min_diff = diff
print(min_diff)

Submission status: Accepted

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

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

h.sort(reverse = False)

for s in range(0, len(h)-(K-1)):
    hmintmp = h[s+(K-1)] - h[s]

    if hmin == None or hmin > hmintmp:
        hmin = hmintmp


print(hmin)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,K = [int(x) for x in input().split()]
h = [0]*N
for i in range(N):
    h[i] = 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 = [int(i) for i in input().split()]
h_list = []
for i in range(n):
    h_list.append(int(input()))
h_list = sorted(h_list)
cur = max(h_list)
for i in range(n-k+1):
    cur = min(cur, h_list[i+k-1]-h_list[i])
print(cur)

Submission status: Accepted

Solution code:
def main():
    """
    2 <= K < N <= 10^5
    1 <= hi <= 10^9
    """
    N, K = map(int, input().split())
    h = [int(input()) for _ in range(N)]

    ans = f(N, K, h)
    print(ans)


def f(N, K, h):
    h = sorted(h, reverse=True)

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

    return ans


if __name__ == '__main__':
    main()

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]
for j in range(N-K+1):
  if ans >= H[K+j-1]-H[j]:
    ans = H[K+j-1]-H[j]
print(ans)

Submission status: Accepted

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

h.sort()

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

print(ans)

Submission status: Accepted

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

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

h.sort()
tmpmin = 10**9
for i in range(n-k+1):
    if tmpmin > h[i+k-1] - h[i]:
        tmpmin = h[i+k-1] - h[i]
print(tmpmin)

Submission status: Accepted

Solution code:
import sys
import collections
import itertools
import math
import re
from collections import deque

MOD = 10 ** 9 + 7

# N = int(input())
# A = [int(x) for x in input().split()]
# V = [[0] * 100 for _ in range(100)]

N, K = [int(x) for x in input().split()]

H = [None] * N

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

H.sort()

mi = 10 ** 12
for i in range(0, N - K + 1):
    h1 = H[i + K - 1]
    h2 = H[i]

    mi = min(mi, h1 - h2)

print(mi)

Submission status: Accepted

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

h.sort()
ans = 10**9 + 100
for i in range(N-K+1):
    d = abs(h[i]-h[i+K-1])
    if d < ans:
        ans = d

print(ans)

Submission status: Accepted

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

h_s = sorted(h, reverse=True)
print(min(h_s[i] - h_s[i+K-1] for i in range(N-K+1)))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np

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

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

h_list = sorted(h_list)

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

Submission status: Accepted

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

def main():
    """Function."""
    n, k = map(int, input().split())
    hight = [0 for i in range(n)]
    for i in range(n):
        hight[i] = int(input())
    hight.sort()

    check = [0 for i in range(n - k + 1)]
    for i in range(n - k + 1):

        check[i] = hight[i+k-1] - hight[i]

    print(min(check))

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline
n, k = map(int, input().split())
h_list = []
for _ in range(n):
    h = int(input())
    h_list.append(h)
h_list.sort()
ans = 10**9
for i in range(n-k+1):
    ans = min(ans, h_list[i+k-1]-h_list[i])
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n, k = [int(i) for i in input().split()]
h = []
for i in range(n):
    h.append(int(input()))
h.sort(reverse = True)
d = 10000000000
for i in range(n-k+1):
    if d > h[i] - h[i+k-1]:
        d = h[i] - h[i+k-1]
print(d)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
a,b = map(int,input().split())

c = []

for i in range(a):
	c.append(int(input()))

c.sort()
p = c[0]
for i in range(a):
	c[i] -= p

k = c[b-1] - c[0]

for i in range(a-b+1):
	if k >= c[i+b-1] - c[i]:
		k = c[i+b-1] - c[i]

print(k)

Submission status: Accepted

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

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(0, N-K+1):
    temp = H[i+K-1] - H[i]
    ans = min(ans, temp)


print(ans)

Submission status: Accepted

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

diff=999999999999
laterpoint=K-1
for i in range(N-K+1):
  diff=min(diff,p[laterpoint+i]-p[i])
print(diff)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,K=map(int, input().split())
ls=[]
for i in range(N):
    ls.append(int(input()))
ls2=sorted(ls)
min=float("inf")
for i in range(N-K+1):
    if min > ls2[i+K-1] - ls2[i]:
        min=ls2[i+K-1] - ls2[i]
print(min)

Submission status: Accepted

Solution code:
N,K=map(int,raw_input().split())
hlist=[]

for i in range(N):
	hlist.append(int(raw_input()))

hlist.sort()

i=0
j=K-1

minn=100000000000
for i in range(N-K+1):
	diff=hlist[i+K-1]-hlist[i]
	if diff<minn:
		minn=diff

print(minn)

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, K = 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)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

inputs.sort()
diff = 10**19


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

print(diff)

Submission status: Accepted

Solution code:
import sys
from collections import deque
import copy
import math
def get_read_func(fileobject):
    if fileobject == None :
        return raw_input
    else:
        return fileobject.readline


def main():
    if len(sys.argv) > 1:
        f = open(sys.argv[1])
    else:
        f = None
    read_func = get_read_func(f);
    input_raw = read_func().strip().split()
    [N, K] = [int(input_raw[0]), int(input_raw[1])]
    h_list = []
    for i in range(N):
        input_raw = read_func().strip().split()
        [h] = [int(input_raw[0])]
        h_list.append(h)

    h_list.sort(reverse = True)
    min_sub_h = sys.maxint
    for i in range(N - K + 1):
        min_sub_h = min(min_sub_h, h_list[i] - h_list[i + K -1])
    print min_sub_h


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n, k = (int(x) for x in input().split())

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

h.sort()

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

print(total)

Submission status: Accepted

Solution code:
import numpy as np

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

h = np.array([int(input()) for _ in range(n)])
h.sort()

d = h[k-1:] - h[:-k+1]
print(np.min(d))

Submission status: Accepted

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

smallest = float("inf")
for i in range(K-1,len(lst)):
    smallest = min(smallest,lst[i] - lst[i-K+1])
print(smallest)

Submission status: Accepted

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

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

print(min_differ)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
from pprint import pprint
N, K = map(int, input().split())
h = [int(input()) for _ in range(N)]

h.sort()

diff = [10**10 for _ in range(N-K+1)]

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

print(min(diff))

Submission status: Accepted

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

a.sort()
ans = 9999999999999999999
for i in range(len(a)-(k-1)):
    x = a[i+k-1] - a[i]
    ans = min(ans,x)
print(ans)

Submission status: Accepted

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

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

lst = sorted(hs, reverse=True)

mini = lst[0] - lst[-1]
for i in range(N - K + 1):
    tmp = lst[i] - lst[i + K - 1]
    if tmp < mini:
        mini = tmp

print(mini)

Submission status: Accepted

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

    ans = min(abs(h[i + k - 1] - h[i]), ans)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
from sys import stdin

def solve():
    n, k = map(int, stdin.readline().split())
    items = []
    for i in range(n):
        items.append(int(stdin.readline().strip()))

    items = sorted(items)
    ans = 10**9
    for i in range(n-k+1):
        diff = items[i+k-1] - items[i]
        if diff<ans:
            ans = diff

    print(ans)

solve()