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:
import sys

def main():
  n, k = list(map(int, input().split()))
  h = [int(input()) for i in range(n)]
  h.sort(reverse=True)

  cnt = sys.maxsize
  for i in range(n - k + 1):
    if cnt > h[i] - h[i+k-1]:
      cnt = h[i] - h[i+k-1]
  print(cnt)

if __name__ == '__main__':
  main()

Submission status: Accepted

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

def main():
    N, K = [int(x) for x in stdin.readline().rstrip().split()]
    trees = [int(input()) for i in range(N)]
    trees.sort()

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

    print(min(s))



if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:

def solve():
    N, K = map(int, input().split())
    data = [int(input()) for _ in range(N)]
    data.sort()
    ans = []
    cnt = 0
    while True:
        if data[cnt] >= data[-cnt-K]:
            break
        h_max1 = data[cnt + K - 1]
        h_min1 = data[cnt]
        h_max2 = data[-cnt-1]
        h_min2 = data[-cnt - K]
        tmp1 = h_max1 - h_min1
        tmp2 = h_max2 - h_min2
        ans.append(tmp1)
        ans.append(tmp2)
        cnt = cnt + 1
        if data[cnt] >= data[-cnt-K]:
            break

    print(min(ans))


solve()

Submission status: Accepted

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

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

heights.sort()
min_diff = max(heights) - min(heights) 
for i in range(N-K+1):
    diff = heights[i+K-1] - heights[i]
    if diff < min_diff:
        min_diff = diff

print(min_diff)

Submission status: Accepted

Solution code:
tree_num,choice_num = map(int,input().split())
tree_list = sorted([int(input()) for _ in range(tree_num)])
mi = 999999999999999999999999
for idx in range(tree_num-choice_num+1):
    cand = tree_list[idx+choice_num-1] - tree_list[idx]
    if cand < mi:
        mi = cand
print(mi)

Submission status: Accepted

Solution code:
import sys
N,K = list(map(int, input().strip().split(' ')))
trees = [int(line.strip()) for line in sys.stdin]
trees = sorted(trees)
print(min([trees[i+(K-1)]-trees[i] for i in range(len(trees)-(K-1))]))

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

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

print(score)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def fastio():
    import sys
    from io import StringIO 
    from atexit import register
    global input
    sys.stdin = StringIO(sys.stdin.read())
    input = lambda : sys.stdin.readline().rstrip('\r\n')
    sys.stdout = StringIO()
    register(lambda : sys.__stdout__.write(sys.stdout.getvalue()))
fastio()

MOD = 10**9 + 7
I = lambda:list(map(int,input().split()))

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

Submission status: Accepted

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

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

sort_h = sorted(h)
diffmin=10**9

for times in range(n-k+1):

  diff = sort_h[k+times-1]-sort_h[times]
  if diffmin > diff:
    diffmin=diff

print(diffmin)

Submission status: Accepted

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

h = sorted(h)

tmp_min = 10**10
for i in range(n):
    if i+k-1>=n:
        break
    tmp = h[i+k-1]-h[i]
    if tmp_min>tmp:
        tmp_min = tmp
print(tmp_min)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, k = [int(s) for s in input().split(' ')]
# または N, k = map(int, input().split())
h = []
while len(h) != N:
    h.append(int(input()))
h.sort()
# print(h)
min = 1000000000
for i in range(0, len(h)-k+1):
    x = h[i+k-1]-h[i]
    # print(x)
    if min > x:
        min = x
print(min)

Submission status: Accepted

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

for i in range(N - K + 1):
  if res > a[i+K-1] - a[i]:
    res = a[i+K-1] - a[i]

print(res)

Submission status: Accepted

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

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:
N, k = [int(s) for s in input().split(' ')]
 
h = []
while len(h) != N:
    h.append(int(input()))
  
min_height = 1000000000
h.sort()
for start in range(0, N - k + 1):
    end = start + k - 1
    diff = h[end] - h[start]
    if diff < min_height:
        min_height = diff
print(min_height)

Submission status: Accepted

Solution code:
import heapq
 
N, k = [int(s) for s in input().split(' ')]
 
h = []
while len(h) != N:
    heapq.heappush(h, int(input()))
  
min_height = 1000000000
h = heapq.nsmallest(N, h)
for start in range(0, N - k + 1):
    end = start + k - 1
    diff = h[end] - h[start]
    if diff < min_height:
        min_height = diff
print(min_height)

Submission status: Accepted

Solution code:
# coding: utf-8

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

trees=[]

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

trees.sort()

min=trees[k-1]-trees[0]

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

print(min)

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# AtCoder Beginner Contest 115：C - Christmas Eve


def main():
    N, K = [int(i) for i in raw_input().strip().split(' ')]
    H = []
    for _ in range(N):
        h = int(raw_input().strip())
        H.append(h)
    H.sort()

    min_h = 10**10
    for i in xrange(N-K+1):
        h = H[i+K-1] - H[i]
        if h < min_h:
            min_h = h
    print min_h


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n,k=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:
n,k=map(int,input().split())
h=sorted([int(input()) for i in range(n)])
d=10e9
for i in range(n-k+1):
    d=min(d,h[i+k-1]-h[i])
print(d)

Submission status: Accepted

Solution code:
def main():
    N, K = list(map(int, input().split()))
    hs = [int(input()) for i in range(N)]
    hs.sort()
    i = 0
    r = 10 ** 9
    while True:
        if i + K > N:
            break
        r = min(r, hs[i + K - 1] - hs[i])
        i += 1
    print(r)


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

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

print(value)

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:
n, k = map(int, input().split())
A = [int(input()) for i in range(n)]
A.sort(reverse=True)
B = []

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

Submission status: Accepted

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

def main():
    n, k = map(int, input().split())
    hn = [int(input()) for i in range(n)]
    hn = sorted(hn)
    mi = 9999999999
    for i in range(n - k + 1):
        di = hn[i + k - 1] - hn[i]
        mi = min(di, mi)
    print(mi)

main()

Submission status: Accepted

Solution code:
# Christmas Eve

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

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

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

Submission status: Accepted

Solution code:
n,k,*h=map(int,open(0).read().split());h.sort();print(min(y-x for x,y in zip(h,h[k-1:])))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np

treenum, num = map(int,input().split())
trees = [int(input()) for _ in range(treenum)]

sa = np.sort(trees)
kekka = np.min(sa[num-1:]-sa[:-1*num+1])

print(kekka)

Submission status: Accepted

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

h = sorted(h)

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

print(ans)

Submission status: Accepted

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

answer = min(trees[x + k - 1] - trees[x] for x in range(0, n - k + 1))
print(answer)

Submission status: Accepted

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

ps = []
for n in range(N):
        ps.append(int(input()))
ps = sorted(ps)

mins = []
for start in range(N-K+1):
        mins.append( ps[start+K-1] - ps[start] ) 

print(min(mins))

Submission status: Accepted

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

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

g = [ h[i+K-1]-h[i] for i in range(len(h[:-K+1])) ]
print(min(g))

Submission status: Accepted

Solution code:
a, b = map(int, input().split())
A = []
for i in range(a):
    A.append(int(input()))
j = 0
A.sort()

B = []
for i in range(a-b+1):
    B.append(A[i+b-1] - A[i])
print(min(B))

Submission status: Accepted

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

m = float('inf')

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

print(m)

Submission status: Accepted

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

h = []

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

ans = 2*10**9

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

Submission status: Accepted

Solution code:
""" Atcoder """

import sys
input = sys.stdin.readline

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

H.sort()

min_h = 1000000000

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

Submission status: Accepted

Solution code:
n, k = input().split()
n = int(n)
k = int(k)
ans = 1000000000000
lis = []
for i in range(n):
    lis.append(int(input())) 

lis.sort() #小さい順に並べ替え

for i in range(n-k+1):
    sa = lis[i+k-1] - lis[i]
    if sa <= ans:
        ans = sa

print(ans)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
def find_min_diff(hs, n, k):
    l, r = 0, k-1
    diff = hs[r] - hs[l]
    for _ in range(1, n - r):
        l, r = l+1, r+1
        diff = min(diff, hs[r] - hs[l])
    return diff


def main():
    n, k = [int(v) for v in input().split()]
    hs = sorted(int(input()) for _ in range(n))
    min_diff = find_min_diff(hs, n, k)
    print(min_diff)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
H = [int(input()) for i in range(n)]
H.sort(reverse=True)
ans = 10 ** 9

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
s = list(map(int,input().split()))
h = [int(input()) for i in range(s[0])]
h.sort()
min = 0
temp = 0
for i in range(s[0]-s[1]+1):
    temp = abs(h[i]-h[i+s[1]-1])

    if i == 0:
        min = temp
    elif temp < min:
        min = temp
print(min)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np
N,K = map(int,input().split())

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


res = min(list((np.array(h[K-1:]) - np.array(h[:-K+1]))))
print(res)

Submission status: Accepted

Solution code:
#C
N, K = [int(x) for x in input().split()]
H = [int(input()) for i in range(N)]
#print(N,K,H) #
H.sort()
dif = 10**9
for i in range(N-K+1):
    if H[i+K-1] - H[i]<dif: dif = H[i+K-1] - H[i]
        
print(dif)

Submission status: Accepted

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

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

h_list.sort()
answer = h_list[N-1] - h_list[0]

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

print(answer)

Submission status: Accepted

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

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

cand = [0]*(n-k+1)
cand[0] = h[k-1] - h[0]


for j in range(1,n-k+1):
    cand[j] = h[k+j-1] - h[j]

print(min(cand))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Wed Jan  2 09:59:01 2019

@author: Yamazaki Kenichi
"""

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

h.sort()

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
hs = [int(input()) for _ in range(n)]
hs.sort()
print(min(hs[i + k - 1] - hs[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(reverse=True)

ans = 10**(10)
for i in range(N-K+1):
    tmp = H[i] - H[i+K-1]
    if tmp < ans: ans = tmp

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Tue Jan  1 10:30:05 2019
ABC115C
@author: maezawa
"""

n, k = list(map(int, input().split()))
h = [0]*n
for i in range(n):
    h[i] = int(input())
    
h.sort()
ans = h[k-1] - h[0]
for i in range(n-k+1) :
    dh = h[i+k-1] - h[i]
    if ans > dh:
        ans = dh
print(ans)

Submission status: Accepted

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

import numpy as np
hlist = np.sort(hlist)

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
#ABC C-Christmas Eve
n , k = list(map(int, input().split()))
p = []
for i in range(n):
        p.append(int(input()))
p.sort()
m = p[n-1] - p[0]
for i in range(n-k+1):
    t = p[i+k-1] - p[i]
    m = min(t,m)
print(m)

Submission status: Accepted

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

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

Submission status: Accepted

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

s = sorted(h)
sa = max(s)-min(s)

for i in range(n-k+1):
    a = s[i+k-1] - s[i]
    if a < sa:
        sa = a

print(sa)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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(' '))
items = sorted([int(input()) for _ in range(N)])
x = min([items[i + K - 1] - items[i] for i in range(N - K + 1)])
print(x)

Submission status: Accepted

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

H.sort(reverse=True)

ans = H[0] - H[K-1]
for n in range(0, N - K + 1):
    dif = H[n] - H[n + K -1]
    if (dif < ans):
        ans = dif

print(ans)

Submission status: Accepted

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

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())
hList = []
for i in range(N):
  hList.append(int(input()))
hList_sorted = sorted(hList)
kList = []
for i in range(N - K + 1):
  dis = abs(hList_sorted[i] - hList_sorted[i + K - 1])
  kList.append(dis)
print(min(kList))

Submission status: Accepted

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

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

h_array.sort()

#最小値を更新していくので、初期値を無限として設定しておく
result = float("inf")
#indexに注意
for i in range(K - 1, N):
    result = min(result, h_array[i] - h_array[i - K + 1])

print(result)

Submission status: Accepted

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

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

h_array.sort()

#最小値を更新していくので、初期値を無限として設定しておく
result = float("inf")
#indexに注意
for i in range(K - 1, N):
    result = min(result, h_array[i] - h_array[i - K + 1])

print(result)

Submission status: Accepted

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

Submission status: Accepted

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

childs = [ int(input()) for _ in range(N)]
min_sub = 10**9
childs.sort()

for n in range(K-1,N):
    min_sub = min(min_sub, childs[n]-childs[n-(K-1)])

print(min_sub)

Submission status: Accepted

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

Submission status: Accepted

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

H=[]

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

H.sort()

ans = 1e10

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter
n,k=map(int,input().split())
l=[]
for i in range(n):
    l.append(int(input()))
l.sort()
d=Counter(l)
for i in d:
    if(d[i]>=k):
        print(0)
        exit()
ans=10**9
for i in range(n-k+1):
    if(l[k+i-1]-l[i]<ans):
        ans=l[k+i-1]-l[i]
print(ans)

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    n,k = LI()
    a = sorted([I() for _ in range(n)])
    r = inf
    for i in range(n-k+1):
        t = a[i+k-1] - a[i]
        if r > t:
            r = t

    return r


print(main())

Submission status: Accepted

Solution code:
N,K=map(int,raw_input().split())
P=[]
m=0;

for i in range(N):
  P.append(int(raw_input()))
  
P.sort()
K-=1

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

Submission status: Accepted

Solution code:
N,K=map(int,raw_input().split())
P=[]
m=0;

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


print min(P[i+K-1]-P[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)])
print(min([h[i+K-1] - h[i] for i in range(N-K+1)]))

Submission status: Accepted

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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N,K,*H = map(int,open(0).read().split())
H.sort()
a=10**9
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, *H = map(int, open(0).read().split())
H.sort()
a = float('inf')
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 = sorted([int(input()) for _ 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:
a,b= map(int,input().split(" "))
ar = []
for i in range(a):
    l = int(input())
    ar.append(l)
ar.sort()
c = []
for i in range((a-b)+1):
    c.append(ar[(i+b)-1] - ar[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+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)]
sh = sorted(h)
print(min([sh[i+k-1]-sh[i] for i in range(n-k+1)]))

Submission status: Accepted

Solution code:

# coding: utf-8

# In[33]:


"""問題文

とある世界では、今日はクリスマスイブです。

高羽氏の庭には N
本の木が植えられています。i 本目の木 (1≤i≤N) の高さは hi

メートルです。

彼は、これらの木のうち K

本を選んで電飾を施すことにしました。より美しい光景をつくるために、できるだけ近い高さの木を飾り付けたいです。

より具体的には、飾り付ける木のうち最も高いものの高さを hmax
メートル、最も低いものの高さを hmin メートルとすると、hmax−hmin が小さいほど好ましいです。hmax−hmin

は最小でいくつにすることができるでしょうか？
制約

    2≤K<N≤105

1≤hi≤109
hi

    は整数である。

入力

入力は以下の形式で標準入力から与えられる。

N

 K


h1


h2


:


hN


出力

hmax−hmin

としてありうる最小の値を出力せよ。
入力例 1
Copy

5 3
10
15
11
14
12

出力例 1
Copy

2
"""


# In[45]:


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


# In[46]:


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


# In[49]:


p.sort()


# In[56]:


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

Submission status: Accepted

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

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

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

h.sort()

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

print(d_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()
dif = 10000000000
for i in range(n-k+1):
    if dif>h[i+k-1]-h[i]:
        dif = h[i+k-1]-h[i]

print(dif)

Submission status: Accepted

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

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

Submission status: Accepted

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

m = 10e9
for i in range(N-K+1):
    dh = h[K-1+i]-h[0+i]
    m = min(m, dh)

print(m)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np
N,K=input().split()
N = int(N)
K = int(K)
e = [0]*N

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

#for i in range(N):
#    for j in range(N):      
#        if e[i] < e[j]:
#            tmp = e[i]
#            e[i] = e[j]
#            e[j] = tmp

e = list(sorted(e))

f = [0]*(N - K + 1)
for i in range(N-K+1):
    f[i] = e[K + i - 1] - e[i]

print(np.min(np.array(f)))

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import numpy as np
n,k = map(int,input().split())
item = [int(input()) for i in range(n)]
item = np.array(item)
item = np.sort(item)

ans = []

for i in range(len(item)-k+1):
  temp = item[i+k-1]-item[i]
  ans.append(temp)
  
ans = np.array(ans)
print(ans.min())

Submission status: Accepted

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

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

Submission status: Accepted

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

list2=list()
h.sort()

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

Submission status: Accepted

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

ans = float("inf")
for i in range(K-1, N):
    a=h[i]-h[i-K+1]
    if a<ans:
        ans=a
print(ans)

Submission status: Accepted

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

h_list = list(sorted(h_list))

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

Submission status: Accepted

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

h_list = list(sorted(h_list))

min_diff = 10 ** 10
for i in range(N):
    if K - 1 + i >= N:
        break
    diff = h_list[K - 1 + i] - h_list[i]
    if diff < min_diff:
        min_diff = diff
print(min_diff)

Submission status: Accepted

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


def main():
    n, k = map(int, input().split())
    h = sorted([int(input()) for _ in range(n)])
    ans = float('inf')

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

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n,k=[int(i) for i in input().split()]
a=[0]*n
for i in range(0,n,1):
    a[i]=int(input())
a.sort(reverse=True)
sa=[1000000000]*100001
j=0
while(n!=(j+k-1)):
    sa[j]=a[j]-a[j+k-1]
    j=j+1

print(min(sa))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
from sys import stdin

s_in = lambda: stdin.readline()[:-1]  # s = s_in()
d_in = lambda: int(stdin.readline())  # N = d_in()
ds_in = lambda: list(map(int, stdin.readline().split()))  # List = ds_in()


N, K = ds_in()
height_list = []
for _ in range(N):
    height_list.append(d_in())

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