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())
l=sorted([int(input()) for i in range(n)])
ans=1000000000
for i in range(n-k+1):
	ans=min(l[i+k-1]-l[i],ans)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

heights.sort()

ans = float('inf')
for i in range(len(heights)):
    if i + k - 1 >= len(heights):
        break
    ans = min(ans, heights[i + k - 1] -  heights[i])

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

h_list.sort(reverse = True)
diff = [h_list[i] - h_list[i+K-1] for i in range(N-K+1)]
print(min(diff))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
INF = float("inf")


def solve(N: int, K: int, h: "List[int]"):
    h.sort()
    print(min(h[i+K-1]-h[i] for i in range(N-K+1)))

    return


def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    h = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, K, h)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

h_list.sort(reverse=True)

sa = [0 for _ in range(N-K+1)]
for i in range(N-K+1):
    sa[i] = h_list[i] - h_list[i+K-1]

print(min(sa))

Submission status: Accepted

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


def solve(N: int, K: int, h: "List[int]"):
    minDiff = 10 ** 18
    for i in range(N - K + 1):
        minDiff = min(minDiff,h[i+K - 1] - h[i])
    print(minDiff)
    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    q = []
    for i in range(N):
        q.append(int(next(tokens)))
    q.sort()
    solve(N, K, q)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
H = [int(input()) for _ in range(N)]
ans = 10**10
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, raw_input().split())
tree = []
for i in range(0, N):
	tree.append(int(raw_input()))
tree.sort()
min = tree[N-1]
for i in range(0, N-K+1):
	if tree[i+K-1] - tree[i] < min: 
		min = tree[i+K-1] - tree[i]

print min

Submission status: Accepted

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

h = []

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

h.sort(reverse=True)

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

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

min = 10**9
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 = map(int, input().split())
h = [int(input()) for i in range(N)]

h.sort()

min_h = 1000000000

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

print(min_h)

Submission status: Accepted

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

Submission status: Accepted

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

h = sorted(h)
hK = h[0:K]
ans = hK[K-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()))
H = sorted([int(input()) for _ in range(N)])

min_h = 1e15
for i in range(N-K+1):
    tmp = H[i+K-1] - H[i]
    if tmp < min_h:
        min_h = tmp
print(min_h)

Submission status: Accepted

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

h.sort()

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

print(min(diff))

Submission status: Accepted

Solution code:
from sys import stdin
n, k = [int(_) for _ in stdin.readline().rstrip().split()]
h = [int(stdin.readline().rstrip()) 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 _ in range(N)]
h.sort()
ans = 10 ** 9
for i in range(0, 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_list = [int(input()) for i in range(n)]
h_list.sort()

sub_list = []
for i in range(n-k+1):
	sub_list.append(h_list[i+k-1]-h_list[i])
print(min(sub_list))

Submission status: Accepted

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

ans = h[K - 1] - h[0]
for i in range(1, 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 = map(int,input().split())
lis = [int(input()) for x in range(N)]
lis.sort()
print(min(lis[i+K-1] - lis[i] for i in range(N-K+1)))

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
h = sorted([int(input()) for i in range(n)])
ans = 10e9 + 1

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

print(ans)

Submission status: Accepted

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

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

print(min(diff))

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
a = []
for i in range(n):
	a.append(int(input()))
a = sorted(a)
counter = a[k - 1] - a[0]
for i in range(n - k + 1):
	b = a[i + k - 1] - a[i]
	if counter > b:
		counter = b
print(counter)

Submission status: Accepted

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

if __name__ == '__main__':
    main()

Submission status: Accepted

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

h.sort()
min = 10**9
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=map(int,input().split())
trees= [int(input()) for i in range(n)]
trees.sort()
print(min(trees[i+k-1]-trees[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()
dif = [h[i+K-1] - h[i] for i in range(N-K+1)]
res = min(dif)
print(res)

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

Submission status: Accepted

Solution code:
trees_num, illuminations_num = map(int, input().split())
trees = sorted(int(input()) for _ in range(trees_num))

diffs = []

for i in range(trees_num - illuminations_num + 1):
    diffs.append(trees[i+illuminations_num-1] - trees[i])

print(min(diffs))

Submission status: Accepted

Solution code:
n, k = list(map(int, input().split()))
h_li = []
for i in range(n):
    h = int(input())
    h_li.append(h)
h_li.sort()
d = 10 ** 10
m = k - 1
while m < n:
    if d > (h_li[m] - h_li[m - k + 1]):
        d = h_li[m] - h_li[m - k + 1]
    m += 1
print(d)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

h = sorted(h)

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

item=[]
for i in range(n):
    b=int(input())
    item.append(b)
    
item.sort()
temp=[]

for i in range(n-p+1):
    a=item[i+p-1]-item[i]
    temp.append(a)
    
temp.sort()
print(min(temp))

Submission status: Accepted

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

s = [0] * N
for i in range(1, N):
    s[i] = s[i-1] + (hn[i] - hn[i-1])
m = min(s[i+K-1] - s[i] for i in range(0, N-K+1))

print(m)

Submission status: Accepted

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

Submission status: Accepted

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

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

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

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

Submission status: Accepted

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


def solve(N: int, K: int, h: "List[int]"):
    h.sort()
    ret = float('inf')
    for i in range(K - 1, N):
        ret = min(ret, h[i] - h[i - K + 1])
    print(ret)
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    h = [ int(next(tokens)) for _ in range(N) ]  # type: "List[int]"
    solve(N, K, h)

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()
sabun = [h[i+k-1] - h[i] for i in range(n-k+1)]
ans = min(sabun)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import collections
def c():
    n, k = map(int, input().split())
    h = []
    for i in range(n):
        h.append(int(input()))
    h.sort()
    d = collections.deque(maxlen = k)
    mins = []
    while len(d) < k:
        d.append(h.pop())
    while h:
        mins.append(d[0] - d[k-1])
        d.append(h.pop())
    mins.append(d[0] - d[k-1])
    print(min(mins))
c()

Submission status: Accepted

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

ans = 1100000000
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=h[N-1]-h[0]

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

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
lengths = list(int(input()) for i in range(N))
sorted_lengths = sorted(lengths)
min_score = 2**100

for i in range(N-K+1):
  score = sorted_lengths[i+K-1] - sorted_lengths[i]
  min_score = min(min_score, score)

print(min_score)

Submission status: Accepted

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

h = sorted(h)
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 = max(h) - min(h)
for i in range(N - K + 1):
	k = h[i + K - 1] - h[i]
	if k < ans:
		ans = k
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):
    if ans > h[i+K-1] - h[i]:
        ans = h[i+K-1] - h[i]

print(ans)

Submission status: Accepted

Solution code:
def main():
    N, K = map(int, input().split())
    h = []
    for i in range(N):
        h.append(int(input()))
    h.sort()
    min = float('inf')
    for i in range(N - K + 1):
        if h[i + K - 1] - h[i]  < min:
            min =  h[i + K - 1] - h[i]
    print(min)
main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
L = []
for i in range(N):
  L.append(int(input()))
L.sort()
ans = float('inf')
for i in range(N-K+1):
  cur = L[i+K-1]-L[i]
  if cur <= ans:
    ans = cur
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
s = [int(input()) for i in range(n)]
s.sort()
c = s[k-1] - s[0]
for i in range(1,n-k+1):
    t = s[i+k-1] - s[i] 
    if t < c:
        c = t

print(c)

Submission status: Accepted

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

print(c)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())  # 複数数値入力
li = list()
for n in range(N):
    li.append(int(input()))

li.sort()

ans = float("inf")
for i in range(N - K + 1):
    if ans > li[i + K - 1] - li[i]:
        ans = li[i + K - 1] - li[i]
print(ans)

Submission status: Accepted

Solution code:
# 木の高さをソート, それぞれの差をリストに格納する, 小さい方から数えてK番目の値が可能な最小値

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

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

# print('h =', h)
dif_h = []

K = K -1

for i in range(K, N):
    dif_h.append(h[i]-h[i-K])

ascending_dif_h = dif_h.sort()
# print('as_dif_h =', dif_h)

print(dif_h[0])

Submission status: Accepted

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

Submission status: Accepted

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

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

d= h[k-1]-h[0]

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

Submission status: Accepted

Solution code:
from bisect import bisect_left, bisect_right
from collections import Counter, defaultdict, deque, OrderedDict
from copy import deepcopy
from functools import lru_cache, reduce
from math import ceil, floor
from sys import setrecursionlimit

import heapq
import itertools
import operator


inf = float('inf')


# globals
N, K = 0, 0
H = []


def set_inputs():
    global N, K, H
    N, K = get_li()
    H = get_data(N, [int])
    return


def main():
    setrecursionlimit(100000)
    set_inputs()
    sorted_h = sorted(H)
    diff = inf
    for _min, _max in zip(sorted_h, sorted_h[K-1:]):
        diff = min(diff, _max - _min)
    print(diff)
    return


def get_int():
    return int(input())


def get_float():
    return float(input())


def get_str():
    return input().strip()


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


def get_lf():
    return [float(f) for f in input().split()]


def get_lc():
    return list(input().strip())


def get_data(n, types):
    if len(types) == 1:
        return [types[0](input()) for _ in range(n)]
    return zip(*(
        [t(x) for t, x in zip(types, input().split())]
        for _ in range(n)
    ))


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())
h.sort()
ans = []
for x, y in zip(h[:N-K+1], h[K-1:]):
    ans.append(y-x)
print(min(ans))

Submission status: Accepted

Solution code:
N, K = map(int, input().split(" "))
treehight = []
difference = []
for i in range(N):
    treehight.append(int(input()))
treehight.sort()
for j in range(N - K + 1):
    difference.append(treehight[j+K-1]-treehight[j])

print(min(difference))

Submission status: Accepted

Solution code:
def gcd_core(a,b):
    if b == 0:
        return a
    else:
        return gcd_core(b,a%b)

def gcd(arr):
    g = gcd_core(arr[0],arr[1])
    for i in range(2,len(arr)):
        g = gcd_core(g,arr[i])
    return g

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

arr.sort()
ans = 1000000000000000000
for i in range(n-k+1):
    if ans > arr[i+k-1]-arr[i]:
        ans = arr[i+k-1]-arr[i]

print(ans)

Submission status: Accepted

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

c_sum = [0]
for i in range(len(h)-1):
    c_sum.append(c_sum[-1] + h[i+1]-h[i])

ans = float('inf')
for i in range(len(h)-K+1):
    ans = min(ans, c_sum[i+K-1] - c_sum[i])

print(ans)

Submission status: Accepted

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

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

print( min( list_h[i + K - 1] - list_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()

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

Submission status: Accepted

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

Submission status: Accepted

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

li_h = list()
for i in range(n):
    li_h.append(int(input()))

s_li_h = sorted(li_h)

wk = k - 1
li_diff = list()
for i in range(wk, n):
    li_diff.append(s_li_h[i] - s_li_h[i - wk])

print(min(li_diff))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
tree = []
for i in range(N):
  tree.append(int(input()))
tree.sort()
mini = 1000000000
for i in range(N-K+1):
  mini = min(mini, tree[i+K-1]-tree[i])
print(mini)

Submission status: Accepted

Solution code:
from sys import exit
N,K = [int(n) for n in input().split()]
K-=1
h = [int(input()) for _ in range(N)]
h = sorted(h)
minm = 10**9
# print(h)

for i in range(0,N-K):
    # print(h[i],h[i+K])
    if h[i+K]-h[i] < minm:
        minm = h[i+K]-h[i]
print(minm)
# S = str(input())
# L = len(S)
# T = str(input())
# exit()

Submission status: Accepted

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

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

loopn = n-k+1

ans = 10**10

for i in range(loopn):
    sa = h[i+k-1] - h[i]
    if sa < ans:
        ans = sa
        
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(k-1, n):
  ans = min(ans, h[i] - h[i-k+1])

print(ans)

Submission status: Accepted

Solution code:
import sys
stdin = sys.stdin
read_int = lambda : list(map(int,stdin.readline().split()))
read_str = lambda : stdin.readline().rstrip()

N,K = read_int()
h = [read_int()[0] for _ in range(N)]
h.sort()

def solve():
    ans = float('INF')
    for i in range(N - K + 1):
        ans = min(ans, h[i + K - 1] - h[i])
    return ans

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

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 _ in range(N)]

h = sorted(h)
t = [0] * N
for i in range(N - 1):
    t[i + 1] = t[i] + (h[i + 1] - h[i])
ans = float('inf')
for j in range(N - K + 1):
    ans = min(ans, t[j + K - 1] - t[j])
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

ans = 1e9+7
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(ans,h[i+k-1] - h[i])
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
hs = [int(input()) for _ in range(N)]
hs_sort = sorted(hs)
diff_min = 1e10
for i in range(N-K+1):
    diff = hs_sort[i+K-1] - hs_sort[i]
    if diff <= diff_min:
        diff_min = diff
    else:
        continue
print(diff_min)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import numpy as np
N, K = map(int,input().split())
H = np.array([int(input()) for _ in range(0, N)])
H = np.sort(H)
H_diff = np.diff(H)
best = int(np.min(np.convolve((H_diff), np.ones(K-1), mode='valid')))
print(best)

Submission status: Accepted

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

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

list.sort()

init = max(list) - min(list)

diff = [b-a for a,b in zip(list,list[K-1::])]

print(min(diff))

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
h=[]
for _ in range(n):
    h.append(int(input()))
h=sorted(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(min)

Submission status: Accepted

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

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 = sorted(h_list)
diff_h = [h_list[i+k-1] - h_list[i] for i in range(len(h_list) - k + 1)]
print(min(diff_h))

Submission status: Accepted

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

print(min)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS():return list(map(list, sys.stdin.readline().split()))
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = I()
    return l
def LIR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LI()
    return l
def SR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = S()
    return l
def LSR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = SR()
    return l
mod = 1000000007

#A
def A():
    return

#B
def B():
    return

#C
def C():
    n,k = LI()
    h = IR(n)
    h.sort()
    ans = float("inf")
    for i in range(n-k+1):
        ans = min(ans,h[i+k-1]-h[i])
    print(ans)

#D
def D():
    return

#E
def E():
    return

#F
def F():
    return

#G
def G():
    return

#H
def H():
    return

#Solve
if __name__ == "__main__":
    C()

Submission status: Accepted

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

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

tree.sort()

print(min(tree[i+K-1] - tree[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 = sorted(h)
tmp = 0
ans = 10000000000
loop = N-K+1
for i in range(loop):
    tmp = h[K+i-1] - h[0+i]
    if(tmp < ans):
        ans = tmp
print(ans)

Submission status: Accepted

Solution code:
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)]
h = sorted(h)
ResMin = float('inf')

for i in range(len(h)-K+1):
    tmp = h[i+K-1] - h[i]
    if tmp < ResMin:
        ResMin = tmp

print(ResMin)

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
h=[int(input()) for i in range(N)]
h=sorted(h)
a,b=0,K-1
answer=10**9

while(b<=N-1):
    answer=min(answer,h[b]-h[a])
    a+=1
    b+=1

print(answer)