Problem:


Score : 300 points

Problem Statement

In some other world, today is Christmas Eve.

There are N trees planted in Mr. Takaha's garden. The height of the i-th tree (1 \leq i \leq N) is h_i meters.

He decides to choose K trees from these trees and decorate them with electric lights. To make the scenery more beautiful, the heights of the decorated trees should be as close to each other as possible.

More specifically, let the height of the tallest decorated tree be h_{max} meters, and the height of the shortest decorated tree be h_{min} meters. The smaller the value h_{max} - h_{min} is, the better. What is the minimum possible value of h_{max} - h_{min}?

Constraints

2 \leq K < N \leq 10^5

1 \leq h_i \leq 10^9

h_i is an integer.

Input

Input is given from Standard Input in the following format:

N K
h_1
h_2
:
h_N

Output

Print the minimum possible value of h_{max} - h_{min}.

Sample Input 1

5 3
10
15
11
14
12

Sample Output 1

2

If we decorate the first, third and fifth trees, h_{max} = 12, h_{min} = 10 so h_{max} - h_{min} = 2. This is optimal.

Sample Input 2

5 3
5
7
5
7
7

Sample Output 2

0

If we decorate the second, fourth and fifth trees, h_{max} = 7, h_{min} = 7 so h_{max} - h_{min} = 0. This is optimal.

There are not too many trees in these sample inputs, but note that there can be at most one hundred thousand trees (we just can't put a sample with a hundred thousand lines here).



Sample input:
5 3
10
15
11
14
12


Sample output:
2

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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 collections import defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inpl(): return list(map(int, input().split()))
def inpl_str(): return list(input().split())

N,K = inpl()
hh = [int(input()) for _ in range(N)]
hh.sort()
l,r = 0,K-1
ans = INF
while True:
    tmp = hh[r] - hh[l]
    ans = min(ans,tmp)
    l += 1
    r += 1
    if r == N:
        break

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

A.sort()
#print(A)
mini=10000000000
for i in range(N-K+1):
    mini=min(mini,A[i+K-1]-A[i])

print(mini)

Submission status: Accepted

Solution code:
n, k = list(map(int, input().split()))
h = [int(input()) for i in range(n)]
h.sort()
ans = 1e9
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())
ls = [int(input()) for _ in range(n)]
ls.sort()
tmp = 10 ** 9
for i in range(n-k+1):
    diff = ls[i+k-1] - ls[i]
    if diff < tmp:
        tmp = diff
print(tmp)

Submission status: Accepted

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

List = sorted(List)

tmp = 10**9
for i in range(N-K+1):
    diff = List[i+K-1]-List[i]
    if diff<tmp:
        tmp = diff
print(tmp)

Submission status: Accepted

Solution code:
# ABC115C - Christmas Eve
import sys
input = sys.stdin.readline

def main():
    n, k = list(map(int, input().rstrip().split()))
    lst = sorted(list(map(int, [input().rstrip() for _ in range(n)])))
    ans = lst[k - 1] - lst[0]
    for i, j in zip(lst, lst[k - 1 :]):
        ans = min(ans, j - i)
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# ABC115C - Christmas Eve
def main():
    n, k = list(map(int, input().rstrip().split()))
    lst = sorted(list(map(int, [input().rstrip() for _ in range(n)])))
    ans = lst[k - 1] - lst[0]
    for i, j in zip(lst, lst[k - 1 :]):
        ans = min(ans, j - i)
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

height_list = [0] * N

for n in range(N):
    height_list[n] = int(input())
height_list = sorted(height_list, reverse=True)

diff_list = [0] * (N - K + 1)

for i in range(N - K + 1):
    diff_list[i] = height_list[i] - height_list[i + K - 1]
print(min(diff_list))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

H.sort()
# print(H)

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

print(ans)

Submission status: Accepted

Solution code:
import numpy as np
N,K = map(int,input().split())
H = np.array([input() for _ in range(N)], dtype=np.int32)

K -= 1

H.sort()
answer = (H[K:] - H[:-K]).min()
print(answer)

Submission status: Accepted

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

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

print(min(ans))

Submission status: Accepted

Solution code:
h,k = [int(x) for x in input().split()]
a = [int(input()) for _ in range(h)]
a.sort()
i = 0
b = []
while i <= h-k:
  b.append(a[i+k-1] - a[i])
  i += 1
print(min(b))

Submission status: Accepted

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

Submission status: Accepted

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

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

print(min(difference))

Submission status: Accepted

Solution code:
n,k=[int(i) for i in input().split()]
h=sorted([int(input()) for _ in range(n)])
ans=10000000000
for i in range(0,n-k+1):
    hight=h[i+k-1]-h[i]
    if ans>hight:
        ans=hight
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

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

h.sort(reverse=True)
ans = []

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

print(min(ans))

Submission status: Accepted

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

print(min(p))

Submission status: Accepted

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

Submission status: Accepted

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


H = sorted([int(input()) for i in range(N)],reverse=True)


#print(H)

L = []

for k in range(N-K+1):
    #print(k,k+1,k+2,k+K)
    
    L.append(H[k]-H[k+K-1])

print(min(L))

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

difference = []

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

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
H = [int(input()) for i in range(N)]
H.sort()
res = 10**10
for i in range(N-K+1):
    res = min(H[i+K-1]-H[i],res)
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()
ans = int(1e18)
for i in range(N-(K-1)):
    tmp = h[i+K-1] - h[i]
    ans = min(ans, tmp)
print(ans)

Submission status: Accepted

Solution code:

import sys
INPUT = sys.stdin.readline

def SINGLE_INT(): return int(INPUT())
def MULTIPLE_INT_LIST(): return list(map(int, INPUT().split()))
def MULTIPLE_INT_MAP(): return map(int, INPUT().split())
def SINGLE_STRING(): return INPUT()
def MULTIPLE_STRING(): return INPUT().split()

N, K = MULTIPLE_INT_MAP()
trees = [int(input()) for _ in range(N)]
trees.sort()
ans = float('inf')



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

Submission status: Accepted

Solution code:

import sys
INPUT = sys.stdin.readline

def SINGLE_INT(): return int(INPUT())
def MULTIPLE_INT_LIST(): return list(map(int, INPUT().split()))
def MULTIPLE_INT_MAP(): return map(int, INPUT().split())
def SINGLE_STRING(): return INPUT()
def MULTIPLE_STRING(): return INPUT().split()

N, K = MULTIPLE_INT_MAP()
trees = [int(input()) for _ in range(N)]
trees.sort()
ans = float('inf')

for i in range(0, N-K+1):
    ans = min(ans, trees[i+K-1] - trees[i])
    if ans == 0:
        break

print(ans)

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:
# input 高速化
import sys
input = sys.stdin.readline

N, K = map(int, input().split())
h = [int(input()) for _ in range(N)]
h.sort()
m = 10 ** 10
for i in range(N-K+1):
    tmp = h[i+K-1] - h[i]
    if tmp < m:
        m = tmp
print(m)

Submission status: Accepted

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

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:
def resolve():
    (N, K) = list(map(int, input().split(" ")))
    Hs = [int(input()) for i in range(N)]
    Hs.sort()
    diffmin = float('inf')
    for i in range(0, N-K+1):
        diff = Hs[i+K-1] - Hs[i]
        if diffmin > diff:
            diffmin = diff
    print(diffmin)

if '__main__' == __name__:
    resolve()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def quick_sort(arr):
    left = []
    right = []
    if len(arr) <= 1:
        return arr

    # データの状態に左右されないためにrandom.choice()を用いることもある。
    # ref = random.choice(arr)
    ref = arr[0]
    ref_count = 0

    for ele in arr:
        if ele < ref:
            left.append(ele)
        elif ele > ref:
            right.append(ele)
        else:
            ref_count += 1
    left = quick_sort(left)
    right = quick_sort(right)
    return left + [ref] * ref_count + right

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

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

print(answer)

Submission status: Accepted

Solution code:
###template###
import sys
def input(): return sys.stdin.readline().rstrip()
def mi(): return map(int, input().split())
###template###

N, K = mi()
Hs = sorted([int(input()) for _ in range(N)])
gaps = [0] * (N-1)
for i in range(N-1):
  gaps[i] = Hs[i+1] - Hs[i]

mingapsum = 10**9

#最初のgapは元々計算しておく
nowgapsum = sum(gaps[0:K-1])
mingapsum = min(nowgapsum, mingapsum)

prevgap = gaps[0]
for i in range(1, N-K+1):
  nowgapsum = nowgapsum - prevgap + gaps[K-2+i]
  prevgap = gaps[i]
  mingapsum = min(nowgapsum, mingapsum)

print(mingapsum)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
h = [int(input()) for i in range(N)]
h.sort()
ans=[]
for i in range(len(h)-K+1):
  b=h[K+i-1]-h[i]
  ans.append(b)
print(min(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:
def resolve():
    N,K=map(int,input().split())
    l=[]
    mx=10**9

    for i in range(N):
        l.append(int(input()))
    l.sort()
    for i in range(N-K+1):
        mx=min(mx,l[i+K-1]-l[i])
    print(mx)
resolve()

Submission status: Accepted

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

h.sort()
ans = 1e9


for i in range(0,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())

h_array = [int(input()) for i in range(N)]
h_array.sort()
ans = 10**10
for i in range(N-K+1):
    ans = min(ans, h_array[i+K-1] - h_array[i])

print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
C - Christmas Eve
https://atcoder.jp/contests/abc115/tasks/abc115_c

"""
import sys


def solve(heights, K):
    return min([r - l for l, r in zip(heights, heights[K - 1:])])


def main(args):
    N, K = map(int, input().split())
    heights = sorted([int(input()) for _ in range(N)])
    ans = solve(heights, K)
    print(ans)


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Accepted

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

h_list.sort(reverse=True)

#print(h_list)

min = h_list[0] - h_list[N-1]

for i in range(N-K+1):
  diff = h_list[i] - h_list[i+K-1]
  #print(h_list[i],",",h_list[i+K-1])
  if min>diff:
    min = diff
    
print(min)

Submission status: Accepted

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

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

tree = h[:K]
height = []
height.append(tree[-1]-tree[0])

for n in range(K,N):
    tree.pop(0)
    tree.append(h[n])
    height.append(tree[-1]-tree[0])  

print(min(height))

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:
in_1 = input().split()
n = int(in_1[0])
k = int(in_1[1])

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

trees_height.sort()


def sub_minmax(list):
    return max(list)-min(list)

subs = []

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


print(min(subs))

Submission status: Accepted

Solution code:
x = input().split()
height = [int(input()) for n in range(int(x[0]))]
height.sort()
check_new = 0
check_old = max(height)
for n in range(int(x[0]) - int(x[1]) + 1):
    check_new = height[int(x[1]) - 1 + n] - height[n]
    if check_new <= check_old:
        check_old = check_new
    
print(check_old)

Submission status: Accepted

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

Submission status: Accepted

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

hs.sort()
mini = 10 ** 10
for i in range(n-k+1):
    sample = abs(hs[i] - hs[i+k-1])
    if mini > sample:
        mini = sample

print(mini)

Submission status: Accepted

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

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7

def LI(): return list(map(int,input().split()))
def I(): return int(input())
def LS(): return input().split()
def S(): return input()

def main():
  n,k=LI()

  l=[]
  for _ in range(n):
    l.append(I())

  l.sort()

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

  return mn

print(main())

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
# abc 115 C
# input
n, k = map(int, input().split())
a = []
o = 10000000000
for i in range(n):
    a.append(int(input()))
a.sort()

# calc
for i in range(n-k+1):
    t = a[i+k-1]-a[i]
    if o > t:
        o = t
# output
print(o)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
h=sorted([int(input()) for _ in range(n)])
gap=[h[i]-h[i-1] for i in range(1,n)]
tmp=0
lst=[gap[0]]
for i in range(1,len(gap)):
    lst.append(gap[i]+lst[i-1])
#print(gap,lst)
ans=[lst[k-2]]
for i in range(k,n):
    ans.append(lst[i-1]-lst[i-k])
#print(ans)
print(min(ans))

Submission status: Accepted

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

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

arr.sort()

print(min(abs(y - x) for x, y in zip(arr, arr[k-1:])))

Submission status: Accepted

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

res = 1000000000000
for i in range(N-K+1):
    res = min(res, inl[i+K-1] - inl[i])

print(res)

Submission status: Accepted

Solution code:
import heapq

n, k = map(int, input().split())
h = []
d = [0 for _ in range(n)]
ans = 1e9

for _ in range(n):
    # h.append(int(input()))
    heapq.heappush(h, int(input()))

h = [heapq.heappop(h) for _ in range(n)]

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

print(ans)

Submission status: Accepted

Solution code:
import heapq

n, k = map(int, input().split())
h = []
d = [0 for _ in range(n)]
ans = 100000000000

for _ in range(n):
    # h.append(int(input()))
    heapq.heappush(h, int(input()))

h = [heapq.heappop(h) for _ in range(n)]

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

print(ans)

Submission status: Accepted

Solution code:
import heapq

n, k = map(int, input().split())
h = []
d = [0 for _ in range(n)]
ans = float('inf')

for _ in range(n):
    # h.append(int(input()))
    heapq.heappush(h, int(input()))

h = [heapq.heappop(h) for _ in range(n)]

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

print(ans)

Submission status: Accepted

Solution code:
from collections import deque
N, K = map(int, input().split())
H = []
for i in range(N):
    H.append(int(input()))

H = sorted(H)
que = deque()
mi = float('inf')
for i in range(N):
    que.append(H[i])
    if len(que) >= K:
        mi = min(mi, que[-1] - que[0])
        que.popleft()

print(mi)

Submission status: Accepted

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

import sys

input = sys.stdin.readline

def main():
    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)]))

main()

Submission status: Accepted

Solution code:

n, k = [ int(v) for v in input().split() ]
tree_list = [0]*n

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

tree_list = sorted(tree_list)


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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
n, k = [ int(v) for v in input().split() ]
tree_list = [0]*n

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

tree_list = sorted(tree_list)


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

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
h=[int(input()) for i in range(N)]
h.sort()
h.reverse()
S=100000000000
for j in range(N-K+1):
    s=h[j]-h[j+K-1]
    if s<S:
        S=s
print(S)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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


print(min(l))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# ABC115C - Christmas Eve
import sys
input = sys.stdin.readline

n, k = list(map(int, input().rstrip().split()))
lst = sorted(list(map(int, [input().rstrip() for _ in range(n)])))
ans = lst[k - 1] - lst[0]
for i, j in zip(lst, lst[k - 1 :]):
    ans = min(ans, j - i)
print(ans)

Submission status: Accepted

Solution code:
# ABC115C - Christmas Eve
n, k = list(map(int, input().rstrip().split()))
lst = sorted(list(map(int, [input().rstrip() for _ in range(n)])))
ans = lst[k - 1] - lst[0]
for i, j in zip(lst, lst[k - 1 :]):
    ans = min(ans, j - i)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from sys import stdin
N, K = [int(x) for x in stdin.readline().rstrip().split()]
h = [int(stdin.readline().rstrip()) for _ in range(N)]
h.sort()
diff = [h[k + K - 1] - h[k] for k in range(N - K + 1)]
diff.sort()
print(diff[0])

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

print(min(ans))

Submission status: Accepted

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

ans = 10**9
for i in range(K-1, N):
    diff = h[i] - h[i-(K-1)]
    if diff < ans:
        ans = diff

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

print(min(ans))

Submission status: Accepted

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

print(h_min)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
import heapq

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

h = []
for _ in range(n):
    heapq.heappush(h, int(input()))

q = [heapq.heappop(h) for _ in range(k-1)]

ans = 1e9
for i in range(len(h)):
    p = i % (k-1)
    s = q[p]
    e = heapq.heappop(h)
    q[p] = e
    ans = min(ans, e - s)

print(ans)

Submission status: Accepted

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

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

h = sorted(h)

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

print(ans)

Submission status: Accepted

Solution code:
import heapq

n, k = map(int, input().split())
h = []
d = [0 for _ in range(n)]
ans = float('inf')

for _ in range(n):
    heapq.heappush(h, int(input()))

h = [heapq.heappop(h) for _ in range(n)]


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

print(ans)

Submission status: Accepted

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

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

print(min(lst))

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
h = []
ans = float('inf')

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

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

print(ans)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
h = []
d = [0 for _ in range(n)]
ans = float('inf')

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

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

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

print(ans)

Submission status: Accepted

Solution code:
N, K = [int(hk) for hk in input().split()]
H = []
for n in range(N):
  H.append(int(input()))
H.sort()
dh_mn = 10**9
for i in range(N-K+1):
  dh = H[K+i-1]-H[i]
  if dh < dh_mn:
    dh_mn = dh
print(dh_mn)

Submission status: Accepted

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


def solve(N: int, K: int, H: "List[int]"):
    H.sort()

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

    print(min_hdif)


# 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
    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 = sorted([int(input()) for _ in range(N)])

ans = 10 ** 9 - 1

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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


print(ans)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
h = [int(input()) for _ in range(N)]
h.sort(reverse=True)
cnt = 10**9
for i in range(N-K+1):
  rng = h[i]-h[i+K-1]
  if rng < cnt:
    cnt = rng
print(cnt)

Submission status: Accepted

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

dif = 10**10
for i in range(n-k+1):
    dif = H[i+k-1]-H[i] if H[i+k-1]-H[i] < dif else dif
print(dif)

Submission status: Accepted

Solution code:
# coding: utf-8

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

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

print(x)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
h = []
min_d = 10 ** 10

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

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

Submission status: Accepted

Solution code:
a=list(map(int,input().split()))
b=list(sorted([int(input()) for i in range(a[0])]))
ans=[]
for i in range(a[0]-a[1]+1):
  ans+=[abs(b[a[1]-1+i]-b[i])]
print(min(ans))