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

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

h_list.sort()

min_diff = 9999999999
for i in range(N - K + 1):
    diff = abs(h_list[i] - h_list[i + K - 1])
    min_diff = diff if diff < min_diff else min_diff

print(min_diff)

Submission status: Accepted

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

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

A.sort(reverse=True)
    
for i in range(n - k + 1):
    diff = A[i] - A[i + k -1]
    D.append(diff)
    
print(min(D))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
S = input()
S = S.split()
N = int(S[0])
K = int(S[1])
h = [int(input()) for i in range(N)]
h.sort()
ans = 10**9 + 1
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, raw_input().split())
hs = []
for i in range(N):
  hs.append(int(raw_input()))

hs.sort()

minv = abs(hs[0]-hs[K-1])

for i in range(len(hs)-K+1):
  tmp = hs[i+K-1] - hs[i]
  if minv > tmp:
    minv = tmp
print minv

Submission status: Accepted

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

h_list.sort()

min_index = 0
max_index = min_index + K - 1

hmax_hmin = 99999999999999

while len(h_list) >= max_index + 1:
  if hmax_hmin > h_list[max_index] - h_list[min_index]:
  	hmax_hmin = h_list[max_index] - h_list[min_index]
  
  min_index += 1
  max_index += 1

print(hmax_hmin)

Submission status: Accepted

Solution code:
N ,K = map(int,input().split())
h = [int(input()) for i in range(N)]
h.sort()
# print(h)
min=h[N-1]
for i in range(N):
  if(i+K>N):
    break
  tmp = h[i+K-1]-h[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.sort()

ans = 1000000000

for i in range(0, n-k+1):
    temp = h[i+k-1] - h[i]
    ans = temp if ans > temp else ans

print(ans)

Submission status: Accepted

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

sys.setrecursionlimit(10000)


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

H.sort()
res = 100000000000000
for i in range(N-K+1):
    res = min(res, H[i+K-1] - H[i])

print(res)

Submission status: Accepted

Solution code:
a = input().split()
a0 = int(a[0])
k = int(a[1])
b = []
for i in range(a0):
    c = int(input())
    b.append(c)
b.sort()
d = []
for i in range(len(b)-k+1):
    d.append(b[i+k-1]-b[i])

print(min(d))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
answer=0
tree=[]
def tree_keisan(x,y): #最大の木と最小の木の差を出す
    ans=tree[x+y-1]-tree[x]
    return ans
for i in range(n):
    tree.append(int(input()))
tree.sort()
answer=tree_keisan(0,k)
for i in range(n-k+1):
    sa=tree_keisan(i,k)
    if answer>sa:
        answer=sa
print(answer)

Submission status: Accepted

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

t = []
for i in range(0, n):
  t.append(int(input()))

t.sort()
min = max(t) - min(t)

for i in range(0, n - k + 1):
  d = t[i + k - 1] - t[i]
  if min > d:
    min = d

print(min)

Submission status: Accepted

Solution code:
firstLine = input().split(" ")
valn = int(firstLine[0])
valk = int(firstLine[1])
valh = []

for vali in range(valn):
    valh.append(int(input()))

valh.sort()

smallest = 1000000001
for vali in range(valn - valk + 1):
    diff = valh[vali + valk - 1] - valh[vali]
    if diff < smallest:
        smallest = diff

print(smallest)

Submission status: Accepted

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

heights.sort()

# あり得る最大のサイズ差
answer = max(heights) - min(heights)

for i in range(0, n-k+1):
    candidate = heights[i+k-1] - heights[i]
    if candidate < answer:
        answer = candidate

print(answer)

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
S = [int(input()) for _ in range(H)]
S.sort()
res = None
for i in range(len(S) - W + 1):
    sdiff = S[i + W - 1] - S[i]
    if res is None:
        res = sdiff
    if res > sdiff:
        res = sdiff

print(res)

Submission status: Accepted

Solution code:
n,k = [int(x) for x in input().split()]
l = sorted([int(input()) for x in range(n)])
m = [(l[x+k-1]-l[x]) for x in range(n-k+1)]
print(min(m))

Submission status: Accepted

Solution code:
M = list(map(int,input().split()))
N = M[0]
K = M[1]
h = list()
for x in range(0,N):
    h.append(int(input()))
sorted_h = sorted(h)
sub_h_org = sorted_h[N-1] - sorted_h[0]
for y in range(0,N-K+1):
    sub_h = sorted_h[y+K-1] - sorted_h[y]
    if sub_h < sub_h_org:
        sub_h_org = sub_h
print(sub_h_org)

Submission status: Accepted

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

h.sort()
for i in range(K-1, N):
    dp[i] = h[i] - h[i-(K-1)]

print(min(dp))

Submission status: Accepted

Solution code:
N, K = (int(x) for x in 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())
t = [int(input()) for i in range(N)]
t.sort()
l = []

for j in range(N-K+1):
    l.append(t[j+K-1] - t[j])

print(min(l))

Submission status: Accepted

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

h.sort()

DEF_MIN = max(h)-min(h)

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

print(DEF_MIN)

Submission status: Accepted

Solution code:
#abc115c

n,k=map(int,raw_input().split())
h=[]
for i in xrange(n):
 h.append(int(raw_input()))
h.sort()
res=float('inf')
for i in xrange(n-k+1):
 res=min(res,h[i+k-1]-h[i])
print res

Submission status: Accepted

Solution code:
nnk = list(map(lambda x: int(x), input().split(" ")))
n = nnk[0]
k = nnk[1]

hs = [int(input()) for l in range(n)]
hs.sort()

num = n - k + 1

hmmm = []
for l in range(num):
    hmmm.append(hs[k+l-1] - hs[l])

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

Submission status: Accepted

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

hs = [int(input()) for l in range(n)]
hs.sort()

hmmm = []
for m in range(n-k+1):
    hmmm.append(hs[k+m-1] - hs[m])

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

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(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())
heights = sorted([int(input()) for _ in range(N)])
ans = float('inf')
for i in range(N - K + 1):
    if abs(heights[i] - heights[i + K - 1]) < ans:
        ans = abs(heights[i] - heights[i + K - 1])
print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
N, K = map(int, input().rstrip().split())
heights = sorted([int(input().rstrip()) for _ in range(N)])
ans = float('inf')
for i in range(N - K + 1):
    if abs(heights[i] - heights[i + K - 1]) < ans:
        ans = abs(heights[i] - heights[i + K - 1])
print(ans)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
heights = sorted([int(input()) for _ in range(N)])
ans = float('inf')
for i in range(N - K + 1):
    if abs(heights[i] - heights[i + K - 1]) < ans:
        ans = abs(heights[i] - heights[i + K - 1])
print(ans)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
heights = sorted([int(input()) for _ in range(N)])
ans = max(heights)
for i in range(N - K + 1):
    if abs(heights[i] - heights[i + K - 1]) < ans:
        ans = abs(heights[i] - heights[i + K - 1])
print(ans)

Submission status: Accepted

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

a.sort()

mindiff = 1000000000

for i in range(int(N)-(S - 1)):
    v = a[i+S-1]-a[i]

    if mindiff > v: mindiff = v

print(mindiff)

Submission status: Accepted

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

Submission status: Accepted

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

a=sorted(a)

ans=max(a)

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

print(ans)

Submission status: Accepted

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

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

ans = float("inf")
h = sorted(h)

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())
tree_list = []
dif = 999999999
for i in range(n):
    tree_list.append(int(input()))
tree_list.sort()
for i in range(n-k+1):
    if dif > (tree_list[i+k-1] - tree_list[i]):
        dif = tree_list[i+k-1] - tree_list[i]
print(dif)

Submission status: Accepted

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

p=[int(input()) for i in range(int(N))]
p.sort()
ans=1000000000

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

print(int(ans))

Submission status: Accepted

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

arr = []
for i in xrange(n):
    a = int(raw_input())
    arr.append(a)

arr.sort()
# print arr

min = 1000000001
# res = -1

for i in xrange((n-k)+1):
    diff = arr[i+k-1] - arr[i]

    if diff < min:
        min = diff
        # res = i

print min

Submission status: Accepted

Solution code:
N,K=(int(x) for x in input().split())
i=1
p=[]
while i<=N:
    p.append(int(input()))
    i=i+1

p.sort()
high=[]
i=0
while i+K<=N:
        L=int(K+i)
        a = p[int(L-1)]
        b = p[i]
        deff = int(a - b)
        high.append(int(deff))
        i = i + 1

print(int(min(high)))

Submission status: Accepted

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

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

ans = 0xdeadbeef

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())
trees = sorted([int(input()) for _ in range(n)])
print(min([trees[i+k-1] - trees[i] for i in range(n-k+1)]))

Submission status: Accepted

Solution code:
Num = []
Num = list(map(int,input().split(" "))) #0-all 1-pick
lists = []
for i in range(0,Num[0]):
    lists.append(int(input()))
lists.sort()
lists.reverse()
dist = lists[0] - lists[0+Num[1]-1]
for j in range(1,Num[0] - Num[1] + 1):
    tmp = lists[j] - lists[j+Num[1]-1]
    if (dist > tmp):
        dist = tmp
print(dist)

Submission status: Accepted

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

for i in range(n):
    tree = int(input())
    T.append(tree)

T.sort(reverse=True)
                 
for i in range(n - k + 1):
    diff = T[i] - T[i + k -1]
    D.append(diff)
    
print(min(D))

Submission status: Accepted

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

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

print(min(li))

Submission status: Accepted

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

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

trees.sort()

min_height = trees[-1]

for i in range(N-K+1):
	tmp = trees[i+K-1] - trees[i]
	min_height = tmp if (tmp < min_height) else min_height

print(min_height)

Submission status: Accepted

Solution code:
from sys import stdin
import numpy as np
import sys

N, K = [int(x) for x in stdin.readline().rstrip().split()]
h = np.array(list(map(int, [stdin.readline().rstrip() for _ in range(N)])))

h.sort()
hmin = 2**30

for i in range(0, N-K+1):
    hdiff = h[i+K-1] - h[i]
    if hdiff < hmin:
        hmin = hdiff

print(hmin)

Submission status: Accepted

Solution code:
N, K = list(map(int, input().strip().split(" ")))
hList = []
for n in range(N):
    hList.append(int(input().strip()))

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

Submission status: Accepted

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

h.sort()
ans = 1e9 + 1
s = 0
while s + k - 1 <= n - 1:
  mm = h[s+k-1] - h[s]
  if mm < ans:
    ans = mm
  s += 1
print(int(ans))

Submission status: Accepted

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

Submission status: Accepted

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

h = sorted(h)

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

print(sub_min)

Submission status: Accepted

Solution code:
N, K = list(map(int, input().split()))
height = []
mindiv = 1000000000
for i in range(N):
    temp = int(input())
    height.append(temp)
height.sort()
for i in range(N - (K - 1)):
    if (height[i + K - 1] - height[i]) < mindiv:
        mindiv = height[i + K - 1] - height[i]

print(mindiv)

Submission status: Accepted

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

Submission status: Accepted

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

N, K = map(int, input().split())
hN = [int(input()) for i in range(N)]

hN.sort()

ans = float('inf')
for i in range(N-K+1):
    ans = min(hN[i+K-1] - hN[i], ans)
print(ans)

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
h = []
for _ in range(0,n):
    hh = int(input())
    h.append(hh)
h.sort()
ans = float("inf")
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 = sorted([int(input()) for _ in range(N)])

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

Submission status: Accepted

Solution code:
from sys import stdin
a = stdin.readline().split(" ")
num=int(a[0])
find=int(a[1])
j=1
w2=[]
while j<=num:
    z = stdin.readline().rstrip('\n')
    w2.append(int(z))
    j+=1
w2.sort()
minnum=w2[find-1]-w2[0]
for i in range(len(w2)-find+1):
    minnum=min(minnum,w2[i+find-1]-w2[i])
print(minnum)

Submission status: Accepted

Solution code:
# coding: utf-8


def main():
    n, k = map(int, input().split())
    h = []
    for _ in range(n):
        h.append(int(input()))

    h.sort()

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

    print(min_diff)


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()
min=max(h)-min(h)

for j in range(n-k+1):
  sa=h[j+k-1] - h[j]
  if min > sa:
    min=sa

print(min)

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
H=sorted(int(input())for _ in[0]*N)
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()
mindif=h[K-1]-h[0]
for i in range(N-K+1):
    dif=h[i+K-1]-h[i]
    if mindif>dif:
        mindif=dif
print(mindif)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

import sys


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


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


N, K = inpl()
H = [int(input()) for _ in range(N)]

H.sort()
ans = 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 = input().split(' ')
n = int(n)
k = int(k)
h = []
for i in range(n):
    h.append(int(input()))
h.sort()
diffs = []
for i in range(n-k+1):
    diffs.append(h[i+k-1] - h[i])
print(min(diffs))

Submission status: Accepted

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

Submission status: Accepted

Solution code:

total_cnt, select_cnt = map(int, input().split())

tree_list = []

for i in range(total_cnt):
    tree_high = int(input())
    tree_list.append(tree_high)

tree_sorted_list = sorted(tree_list)

diff_list = []

for j in range(total_cnt-select_cnt+1):
    diff = tree_sorted_list[j+select_cnt-1] - tree_sorted_list[j]
    diff_list.append(diff)
    
print(min(diff_list))

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

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

    print(min(ans))

if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    n, k = map(lambda n: int(n), input().split(' '))
    prices = []
    for _ in range(n):
        p = int(input())
        prices.append(p)
    prices.sort()

    min_diff = prices[-1] - prices[0]
    for i in range(n-k+1):
        diff = prices[i+k-1] - prices[i]
        if diff < min_diff: 
            min_diff = 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()
ans = 1<<31
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 python

from collections import deque
import itertools as it
import sys
import math

sys.setrecursionlimit(1000000)

INF = 10 ** 18
MOD = 10 ** 9 + 7

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

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())
listT = []
index=0
while index < n:
    h = int(input())
    listT.append(h)
    index+=1
listT.sort()
if n == k:
    print(listT[len(listT)-1]-listT[0])
else:
    mini=1000000000
    index =0
    while index < n-k+1:
        if listT[index+k-1]-listT[index] < mini:
            mini = listT[index+k-1]-listT[index]
        index+=1
    print(mini)

Submission status: Accepted

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

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

Submission status: Accepted

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

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

print(min_diff)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
total, window = map(int, input().split())

trees_height = sorted([int(input()) for _ in range(total)])
diff = []

for i in range(total-window+1):
    diff.append(trees_height[i+window-1] - trees_height[i])

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)]
sort = sorted(h)
min = 1000000000
for j in range(N-K+1):
  if sort[j+K-1] - sort[j] < min:
    min = sort[j+K-1] - sort[j]
    
print(min)

Submission status: Accepted

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

a.sort()
b = a[k-1] - a[0]
for i in range(n-k+1):
    if a[i+k-1] - a[i] < b:
        b = a[i+k-1] - a[i]
print(b)

Submission status: Accepted

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

tree_list = []
sort_list = []
for x in range(N):
    tree_list.append(int(input()))

sort_list = sorted(tree_list)

min_heigt = sort_list[M-1] - sort_list[0]

for x in range(N-M+1):
    nagasa =sort_list[x+M-1] - sort_list[x] 
    if min_heigt > nagasa:
        min_heigt = nagasa

print(min_heigt)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
h=[int(input())for _ in [0]*n]
h.sort()
ans = 1234567890
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(_) for _ in input().split()]
h = [int(input()) for i in range(N)]

xs = sorted(h)
result = min(xs[i + K - 1] - xs[i] for i in range(N - K + 1))

print(result)

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:
# -*- coding: utf-8 -*-
N,K = map(int,input().split())
c = [int(input()) for i in range(N)]
c.sort()
output = c[K-1] - c[0]
for i in range(N-K):
    if output > c[K+i] - c[i+1]:output = c[K+i] - c[i+1]
print(output)

Submission status: Accepted

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

xs = sorted(h)
result = xs[-1]
for i in range(N - K + 1):
    r = xs[i + K - 1] - xs[i]
    result = min(result, r)

print(result)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
hi = list(map(lambda x: int(input()), range(N)))

hi.sort()
diff = map( lambda i: hi[i+K-1]-hi[i], range(N-K+1) )
min_diff = min(diff)

print(min_diff)

Submission status: Accepted

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

hi = []
i = 0
while i + (K - 1) < N:
    hi.append(int(h[i + (K - 1)] - h[i]))
    i += 1

print(min(hi))

Submission status: Accepted

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

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

print(m)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def solve(string):
    n, k = map(int, string.split("\n")[0].split(" "))
    ins = list(map(int, string.split("\n")[1:]))
    ins.sort()
    return str(min([ins[i + k - 1] - ins[i] for i in range(len(ins) - k + 1)]))


if __name__ == '__main__':
    n, k = map(int, input().split(" "))
    ins = [input() for _ in range(n)]
    print(solve("{} {}\n{}".format(n, k, "\n".join(ins))))

Submission status: Accepted

Solution code:
# Title: Christmas Eve
# Link: https://abc115.contest.atcoder.jp/tasks/abc115_c

import sys


sys.setrecursionlimit(10 ** 6)

read_list_int = lambda: list(map(int, sys.stdin.readline().strip().split(' ')))
read_single_int = lambda: int(sys.stdin.readline().strip())


def solution(n: int, k: int, hs: list):
    sorted_hs = sorted(hs)
    min_diff = 99999999999999999
    for start in range(0, n-k+1):
        diff = sorted_hs[start+k-1] - sorted_hs[start]
        if min_diff > diff:
            min_diff = diff
    return min_diff


def main():
    n, k = read_list_int()
    hs = []
    for _ in range(n):
        hs.append(read_single_int())
    print(solution(n, k, hs))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
h = []
ans = 1000000001
for i in range(n):
    h.append(int(input()))
h.sort()
for j in range(n-(k-1)):
    if ans == 0:
        break
    elif ans > h[j+k-1]-h[j]:
        ans = h[j+k-1]-h[j]
print(ans)

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
h = list(int(input()) for i in range(N))
h.sort(reverse=True)
dif = float('inf') 
for i in range(K-1,N):
    dif = min(dif,h[i-K+1]-h[i])
print(dif)

Submission status: Accepted

Solution code:
n, k = map(int,input().split())
lis = []
for _ in range(n):
  lis.append(int(input()))
ans = 100000000000000
lis.sort()
for i in range(n-k+1):
  s = lis[i+k-1]-lis[i]
  if s < ans:
    ans = s
print(ans)

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
height = []
for i in range(N):
    height.append(int(input()))
height_s = sorted(height)
diff = []
i = 0
while True:
    if i + K == len(height_s) + 1:
        break
    diff.append(height_s[i + K -1]-height_s[i])
    i += 1
s = sorted(diff)

print(s[0])

Submission status: Accepted

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

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

sorted_li = sorted(h_list)
min_a = 10**10
for i in range(N-K+1):
    a = sorted_li[i+K-1]-sorted_li[i]
    min_a = [min_a,a][a < min_a]
print(min_a)

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

Submission status: Accepted

Solution code:
import sys

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

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

print(mn)

Submission status: Accepted

Solution code:
N, K = (int(x) for x in input().split())

trees = list()

for i in range(N):
  trees.append(int(input()))
  
trees = sorted(trees)
diff_list = []

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

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
N,K = [int(i) for i in input().split()]

l = [int(input()) for j in range(N)]
l = sorted(l)
dif = 1000000000
for a in range(N-K+1):
    d = l[a+K-1] - l[a]
    #print(a)
    #print(d)
    if dif >= d:
        dif = d

print(dif)

Submission status: Accepted

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

cnt = 0
L = []
for i in range(N - K + 1):
    L.append(A[i + K - 1] - A[i])
    
print(min(L))

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n, k = map(int,input().split())
a = sorted([int(input()) for _ in range(n)])
ans = a[-1] - a[0]
for i in range(0,n-k+1):
    ans = min(ans, a[i + k - 1] - a[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 = sorted(H)
mi = max(H)
for i in range(N-K+1):
    mi = min(abs(H[i]-H[i+K-1]), mi)
    
print(mi)

Submission status: Accepted

Solution code:
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[k+i-1]-h[i])
print(ans)

Submission status: Accepted

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

h.sort()

sa = []
for i in range(0, N + 1 - K):
    h1 = h[i]
    h2 = h[i + K - 1]
    sa.append(h2 - h1)

print('%d' % (min(sa)))