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 = [int(input()) for _ in range(N)]
h.sort()
ans = 11**9
for i in range(N - K + 1):
    min_tree = h[i]
    max_tree = h[i + K - 1]
    ans = min(ans, max_tree - min_tree)
print(ans)

Submission status: Accepted

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

MIN=h[-1]-h[0]

for n in range(K-1,N):
    MIN=min(MIN,h[n]-h[n-K+1])
print(MIN)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def solve():
  N, K = map(int, input().split())
  H = [int(input()) for _ in range(N)]
  H.sort()
  ans = float('inf')
  for i in range(N):
    if i+K>N:
      break
    ans = min(H[i+K-1]-H[i],ans)
  return ans
print(solve())

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
def resolve():
    n, k = map(int, input().split())
    H_sort = list(sorted([int(input()) for _ in range(n)],reverse=True))
    ans = 10**9
    for i in range(n-k+1):
        ans = min(ans, H_sort[i]-H_sort[i+k-1])
    print(ans)
resolve()

Submission status: Accepted

Solution code:
def resolve():
    n, k = map(int, input().split())
    H = []
    for _ in range(n):
        H.append(int(input()))
    H_sort = list(sorted(H, reverse=True))
    ans = 10**9
    for i in range(n-k+1):
        ans = min(ans, H_sort[i]-H_sort[i+k-1])
    print(ans)
resolve()

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,K = list(map(int,input().split()))
h = []
for i in range(N):
    h.append(int(input()))
sorted_h = sorted(h)
minDelta = 10 ** 10
for i in range(N-K+1):
    delta = sorted_h[i+K-1] - sorted_h[i]
    if minDelta > delta:
        minDelta = delta
print(minDelta)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
num, sel = map(int, input().split(" "))
a = sorted(list((int)(input()) for i in range(num)))
mini = 10 ** 9
for i in range(num - sel + 1):
  if mini > a[i + sel - 1] - a[i]:
    mini = a[i + sel - 1] - a[i]
print(mini)

Submission status: Accepted

Solution code:
def main():
    n, k = map(int, input().split())
    inlis = list()
    for _ in range(n):
        h = int(input())
        inlis.append(h)
    inlis.sort()
    ans = 10 ** 10
    for i in range(n-k+1):
        tmp = inlis[i+k-1] - inlis[i]
        if tmp < ans:
            ans = tmp
    print(ans)
            



if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

h=sorted(h)

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

Submission status: Accepted

Solution code:
import bisect, collections, copy, heapq, itertools, math, string, sys
input = lambda: sys.stdin.readline().rstrip() 
sys.setrecursionlimit(10**7)
INF = float('inf')
def I(): return int(input())
def F(): return float(input())
def SS(): return input()
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LSS(): return input().split()

def resolve():
    N, K = LI()
    h = [I() for _ in range(N)]
    h.sort()

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

    print(ans)

if __name__ == '__main__':
    resolve()

Submission status: Accepted

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

tree_heights = []

for i in range(N):
    height = int(input())
    tree_heights.append(height)
    
tree_heights = sorted(tree_heights)

ans = 10**9

for i in range(N-K+1):
   diff = tree_heights[i+K-1] - tree_heights[i]
   
   if diff < ans:
       ans = diff
      
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    n, k = map(int, input().split())
    H = [int(input()) for _ in range(n)]
    H.sort()
    ans = 10 ** 9 + 2
    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 = map(int, input().split())
trees = [int(input()) for _ in range(n)]
trees.sort()
diff_trees_height = []

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

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

Submission status: Accepted

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

for n in range(N-K+1):
  A+=[H[n+K-1]-H[n]]

print(min(A))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# abc115_c.py
# https://atcoder.jp/contests/abc115/tasks/abc115_c

# C - Christmas Eve /
# 実行時間制限: 2 sec / メモリ制限: 1024 MB
# 配点 : 300点

# 問題文
# とある世界では、今日はクリスマスイブです。
# 高羽氏の庭には 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
# 5 3
# 10
# 15
# 11
# 14
# 12

# 出力例 1
# 2

# 1,3,5本目の木を飾り付けると hmax=12,hmin=10 となり hmax−hmin=2で、これが最適です。

# 入力例 2
# 5 3
# 5
# 7
# 5
# 7
# 7

# 出力例 2
# 0

# 2,4,5本目の木を飾り付けると hmax=7,hmin=7 となり hmax−hmin=0で、これが最適です。

# これらの入力例では木の数がそれほど多くありませんが、最大で 10万本の木がある可能性に注意してください 
# (ここに 10 万行の入力例を貼るわけにはいかないのです)。


def calculation(lines):
    # X = lines[0]
    # N = int(lines[0])
    N, K = list(map(int, lines[0].split()))
    values = list()
    for i in range(N):
        values.append(int(lines[i+1]))
    
    # 並び替え
    values.sort()

    dif = values[K-1] - values[0]

    for i in range(1, N-K+1):
        tmp = values[K-1+i] - values[i]
        if dif > tmp:
            dif = tmp
    return [dif]


# 引数を取得
def get_input_lines():
    line = input()
    N, M = list(map(int, line.split()))
    lines = list()
    lines.append(line)
    for _ in range(N):
        lines.append(input())
    return lines


# テストデータ
def get_testdata(pattern):
    if pattern == 1:
        lines_input = ['5 3', '10', '15', '11', '14', '12']
        lines_export = [2]
    if pattern == 2:
        lines_input = ['5 3', '5', '7', '5', '7', '7']
        lines_export = [0]
    return lines_input, lines_export


# 動作モード判別
def get_mode():
    import sys
    args = sys.argv
    if len(args) == 1:
        mode = 0
    else:
        mode = int(args[1])
    return mode


# 主処理
def main():
    import time
    started = time.time()
    mode = get_mode()
    if mode == 0:
        lines_input = get_input_lines()
    else:
        lines_input, lines_export = get_testdata(mode)

    lines_result = calculation(lines_input)

    for line_result in lines_result:
        print(line_result)

    # if mode > 0:
    #     print(f'lines_input=[{lines_input}]')
    #     print(f'lines_export=[{lines_export}]')
    #     print(f'lines_result=[{lines_result}]')
    #     if lines_result == lines_export:
    #         print('OK')
    #     else:
    #         print('NG')
    # finished = time.time()
    # duration = finished - started
    # print(f'duration=[{duration}]')


# 起動処理
if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

n, k = map(int, input().split(" "))
hs = sorted([int(input()) for _ in range(n)], reverse=True)

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

print(ans)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
h = []
for i in range(n):
  h.append(int(input()))
h.sort()
ans = 10 ** 10
#print(h)
for i in range(n-k+1):
  d = h[i+k-1] - h[i]
  ans = min(ans, d)
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())
  
h = sorted(h)
hmin = max(h)
for i in range(N-K+1):
  hmin = min([hmin, h[i+K-1]-h[i]])
  
print(hmin)

Submission status: Accepted

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

for i in range(N):
    num = int(input())
    l.append(num)

ans = max(l) - min(l)
l.sort()

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

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

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,*hh = map(int, open(0).read().split())

hh.sort()

ans = hh[k-1]-hh[0]

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

print(ans)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
h=[]
for i in range(n):
    h.append(int(input()))
h.sort()
m=float('inf')
for i in range(len(h)-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 = [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 = map(int,input().split())
h = []
for i in range(0,N):
    h.append(int(input()))
h.sort()
ans = 10**100
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 = []
for i in range(N):
    h.append(int(input()))

hd = []
h.sort(reverse=True)
for i in range(N-K+1):
    hd.append(h[i]-h[i+K-1])

print(min(hd))

Submission status: Accepted

Solution code:
import math

n, k = list(map(int, input().split()))
h = [int(input()) for _ in range(n)]
h.sort()
ans = math.inf
for i in range(n - k + 1):
    ans = min(ans, h[i + k - 1] - h[i])

print(ans)

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
h = [int(input()) for _ in range(N)]
h.sort(reverse = True)
ans = 10**9
for i in range(N-K+1):
    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 = sorted(H, reverse=True)

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

Submission status: Accepted

Solution code:
import sys
import itertools
# import numpy as np
import time
import math
import heapq
from collections import defaultdict
sys.setrecursionlimit(10 ** 7)
 
INF = 10 ** 18
MOD = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

# map(int, input().split())

N, X = map(int, input().split())
H = [0] * N
for i in range(N):
    H[i] = int(input())

H = sorted(H)

left = 0
right = X
ans = INF
while right <= N:
    mn = H[left]
    mx = H[right - 1]
    ans = min(ans, mx - mn)
    left += 1
    right += 1
print(ans)

Submission status: Accepted

Solution code:
#c
N,K = map(int,input().split())
H=[None]*N

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

H.sort()

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

Submission status: Accepted

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

ans = 1000000000
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)])
print(min(H[i+K-1]-H[i] for i in range(N-K+1)))

Submission status: Accepted

Solution code:
import sys
from collections import defaultdict

sys.setrecursionlimit(10**7)
input = sys.stdin.readline

n,k = map(int, input().split())
h = []
d = defaultdict(int)
cnt = []
for i in range(n):
    x = int(input())
    h.append(x)
    d[x] += 1

for i in d.items():
    if i[1] >= k:
        print(0)
        exit()

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
a=[]
for x in range(n):
  a.append(int(input()))
ans=1000000009
a=sorted(a)
for y in range(n-k+1):
  ans=min(a[y+k-1]-a[y],ans)
  
print(ans)

Submission status: Accepted

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

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

print(p)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# encoding:utf-8
import copy
import random
import bisect #bisect_left　これで二部探索の大小検索が行える
import fractions #最小公倍数などはこっち
import math
import sys
import collections
from decimal import Decimal # 10進数で考慮できる

mod = 10**9+7
sys.setrecursionlimit(mod) # 再帰回数上限はでdefault1000

d = collections.deque()
def LI(): return list(map(int, sys.stdin.readline().split()))

N, K = LI()
h = [0 for i in range(N)]
for i in range(N):
    h[i] = int(input())

h.sort()
ans = mod
for i in range(N - K + 1):
    # print(h[i+K - 1] , h[i])
    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()
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 = [int(input()) for _ in range(n)]
h.sort()

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

print(ans)

Submission status: Accepted

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

h.sort()
min1 = 10**9

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

print(min1)

Submission status: Accepted

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

Submission status: Accepted

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

h.sort()
inf = float('inf')
min_ = inf

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

Submission status: Accepted

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

Submission status: Accepted

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

solve()

Submission status: Accepted

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

nums = [None] * (N-K+1)
for i in range(N-K+1):
    nums[i] = H[i+K-1] - H[i]

print(min(nums))

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# coding:utf-8
n, k = map(int, input().split())
h = [int(input()) for _ in range(n)]
h.sort()
ans = h[0]

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

Submission status: Accepted

Solution code:
N,K = (int(X) for X in input().split())
H = sorted(int(input()) for X in range(0,N))
MIN = pow(10,9)
for T in range(0,N-K+1):
    D = H[T+K-1]-H[T]
    if D<MIN: MIN = D
print(MIN)

Submission status: Accepted

Solution code:
# 初期入力
import sys
input = sys.stdin.readline
N,K = (int(i) for i in input().split())
h =[0]*N
for i in range(N):
    h[i] = int(input())
h.sort()

#K本選んだうちの最高と最低の差をリスト
ans =[10**9+1]*N
for i in range(N-K+1):
    ans[i] =h[i+K-1] -h[i]
print(min(ans))

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
h = [int(input()) for _ in range(N)]
h.sort()
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:
import sys
input = sys.stdin.readline

N,K = list(map(int,input().split()))
H = sorted([int(input()) for _ in range(N)])
ans = float('inf')
for i in range(N-K+1):
    ans = min(H[i+K-1] - H[i],ans)
print(ans)

Submission status: Accepted

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

T = []

for i in range(N):
    t = int(input())
    T.append(t)

T.sort()

A = float("infinity") 
for i in range(N-K+1):
    a = T[i+K-1] - T[i]
    A = min(A,a)

print(A)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
n, k, *h = map(int, open(0).read().split())
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, m, l = map(int, input().split())

# list_n = list(map(int, input().split()))

# n = input()
# list = [input() for i in range(N)

# list = [[i for i in range(N)] for _ in range(M)]

import sys
input = sys.stdin.readline

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
from collections import deque, defaultdict
from math import sqrt, factorial
# def input(): return sys.stdin.readline()[:-1] # warning not \n
# def input(): return sys.stdin.buffer.readline().strip() # warning bytes
# def input(): return sys.stdin.buffer.readline().decode('utf-8')


def solve():
    n, k = [int(x) for x in input().split()]
    a = []
    for _ in range(n):
        a.append(int(input()))
    a.sort()
    ans = float('inf')
    for i in range(n-k+1):
        ans = min(ans, a[i+k-1]-a[i])
    print(ans)
t = 1
# t = int(input())
for case in range(1,t+1):
    ans = solve()


"""

a aa c ca c a b a ab a b c

"""

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
def s(): return input()
def k(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def X(): return list(input())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
def gcd(*numbers): reduce(math.gcd, numbers)
sys.setrecursionlimit(10 ** 9)
mod = 10**9+7
count = 0
ans = 0

N, K = I()
a = []
for i in range(N):
    x = k()
    a.append(x)

a.sort()

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())
h=[int(input()) for _ in range(n)]

h.sort()
ans=2000000000
for i in range(n):
    if i+k-1<n:
        hdif=h[i+k-1]-h[i]
        ans=min(ans,hdif)
print(ans)

Submission status: Accepted

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

ans = 10**10
for i in range(N-K+1):
    ans = min(ans, a[i+K-1]-a[i])
    if ans == 0:
        print(0)
        sys.exit()
print(ans)

Submission status: Accepted

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

ans = 0
t = float("inf")
for i in range(n-k+1):
  if abs(h[i+k-1] - h[i]) < t:
    t = abs(h[i+k-1] - h[i])
    ans = i

print(h[ans+k-1] - h[ans])

Submission status: Accepted

Solution code:
import collections
n,k = map(int, raw_input().split(' '))
heights = [int(raw_input()) for _ in range(n)]
heights.sort()
q = collections.deque([])
r =  float('inf') 
for h in heights:
	q.append(h)
	if len(q) == k + 1: q.popleft()
	if k and len(q) == k:
		r =min(r, q[-1] - q[0])
print r if k else 0

Submission status: Accepted

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

Submission status: Accepted

Solution code:

def solve():
    Height = 10**9
    for i in range(0,N-K+1):
        hmax = h[i+K-1]
        hmin = h[i]
        Height = min(Height, hmax-hmin)
#        print(h[i:i+K])
#        print(Height)

    print(Height)

if __name__=="__main__":
    N,K = list(map(int, input().split()))
    h = [int(input()) for _ in range(N)]
    h.sort()
    solve()

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()
ans=(H[K:]-H[:-K]).min()
print(ans)

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

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

M=10**10
H.sort()
for i in range(N-K+1):
    M=min(M,H[i+K-1]-H[i])
print(M)

Submission status: Accepted

Solution code:
INF = float("inf")

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

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

Submission status: Accepted

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

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

ans=10**10
for h in range(K-1,N):
    h_min=H[h-K+1]
    h_max=H[h]
    if ans > h_max-h_min:
        ans=h_max-h_min
print(ans)

Submission status: Accepted

Solution code:
# coding: utf-8

# https://atcoder.jp/contests/abc115


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

    h = sorted(h)

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

    return diff


print(main())

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())
T = [int(input()) for i in range(N)]

T.sort()
P = []
for i in range(N-K+1):
    P.append(T[i+K-1] - T[i])

print(min(P))

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline

in_n = lambda: int(readline())
in_nn = lambda: map(int, readline().split())
in_nl = lambda: list(map(int, readline().split()))
in_na = lambda: map(int, read().split())
in_s = lambda: readline().rstrip().decode('utf-8')


def main():

    N, K = in_nn()
    h = sorted(list(in_na()))

    ans = 10**9 + 7

    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 = 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:
import sys
input = sys.stdin.readline
maxa=10**10
L=list()
N,K=map(int,input().split())
for i in range(N):
  S=int(input())
  L.append(S)
L=sorted(L)
for i in range(N-K+1):
  maxa=min(maxa,L[i+K-1]-L[i])
print(maxa)

Submission status: Accepted

Solution code:
def resolve():
    N, K = [int(i) for i in input().split()]
    H = sorted([int(input()) for _ in range(N)])
    minA = float("inf")
    for i in range(N - K + 1):
        minA = min(minA, H[i + K - 1] - H[i])
    print(minA)


resolve()

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
H = []
for _ in range(N):
    H.append(int(input()))
H = list(sorted(H))
r_min = 10 ** 10
for i in range(N-K+1):
    n_min = H[i]
    n_max = H[i+K-1]
    r_min = min(r_min, n_max-n_min)
print(r_min)

Submission status: Accepted

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

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

Submission status: Accepted

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

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

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

print(ans)