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()
result = max(h) - min(h)
for i in range(N-K+1):
  tmp = h[i+K-1]-h[i]
  result = min(result, tmp)

print(result)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

n, k = map(int, input().split())
H = [int(input()) for i in range(n)]
H.sort()
ans = float('INF')

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

ans = float('inf')
h1 = sorted(h)

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

Submission status: Accepted

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

h = list(int(input()) for _ in range(N))
sh = sorted(h, reverse=True)
ans = sh[0] - sh[K - 1]
for i in range(1, N - K + 1):
    if N < (i + K):
        break
    ans = min(ans, sh[i] - sh[i + K - 1])

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

highs = [int(input()) for i in range(n)]
highs = sorted(highs)

minimum = min([highs[i + k - 1] - highs[i] for i in range(n - k + 1)])

print(minimum)

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()
ans = 1e+10
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:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate
import sys



def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LIM(): return list(map(lambda x:int(x) - 1, sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline()
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def LIRM(n): return [LIM() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
mod = 1000000007



n, k = LI()
trees = sorted(IR(n))
ret = float('inf')
for i in range(n - k + 1):
    ret = min(ret, trees[i + k - 1] - trees[i])



print(ret)

Submission status: Accepted

Solution code:
# -*-coding: utf-8 -*-
N, K = map(int, input().split())
h = []
for i in range(N):
    h.append(int(input()))

h = sorted(h)

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

print(nearest)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# C - Christmas Eve
N, K = map(int, input().split())
h = [int(input()) for i in range(N)]
H = list(h)
H.sort(reverse=True)
ans = H[0]-H[N-1]
tmp = 0
for i in range(len(H)-K+1):
    tmp = H[i]-H[i+K-1]
    if ans >= tmp:
        ans = tmp
    else:
        pass
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
# ABC115 C - Christmas Eve
import numpy as np
N,K = map(int,input().split())
H = [int(input()) for _ in range(N)]

ans = 10**10
H.sort()

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

Submission status: Accepted

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

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
from sys import stdin
import itertools



############################################

# read data for n sequences.
temp = stdin.readline().split()
N = int(temp[0])
X = int(temp[1])

data = []
for i in range(N):
    data.append(int(stdin.readline()))
    
d = sorted(data)

mindif = 1000000000000000
for i in range(N-X+1):
    dif = d[i+X-1] - d[i]
    if dif < mindif:
        mindif = dif
        
print(mindif)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
h=sorted([int(input())for _ in range(n)])
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:
def main():
    import sys
    input = sys.stdin.readline
    N, K = map(int, input().split())
    H = [int(input()) for i in range(N)]
    H = sorted(H)
    ans = float("inf")
    for i in range(N-K+1):
        tmp = H[i+K-1] - H[i]
        if tmp < ans:
            ans = tmp
    print(ans)
    

main()

Submission status: Accepted

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

h.sort()
  
ans = []
for i in range(n-k+1):
  ans.append(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)])
ans = 1000000000

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

Submission status: Accepted

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

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

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

print(diff)

Submission status: Accepted

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

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

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

print(min(diff))

Submission status: Accepted

Solution code:
N,A= [int(i) for i in input().split()]
n = [int(input()) for i in range(N)]
n.sort()
mn = 10**9
for i in range(N-A+1):
	m = n[i+A-1] - n[i]	
	if m == 0:
		print(0)
		exit()
	elif mn > m: mn=m 
print(mn)

Submission status: Accepted

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

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 = list(int(input()) for i in range(n))
h.sort()
ans = max(h)
i = 0
for i in range(n-k+1):
    if ans > h[i+k-1]-h[i]:
        ans = h[i+k-1]-h[i]
print(ans)

Submission status: Accepted

Solution code:
def christmas_eve(n,k,h):
    _h = sorted(h)
    opt = 0
    ans = 10**9 + 1
    _k = k - 1
    loop = n - _k
    for i in range(loop):
        opt = _h[i+_k] - _h[i]
        if ans >= opt:
            ans = opt

    return ans
if __name__ == '__main__':
    n,k = map(int,raw_input().split())
    h = [int(raw_input()) for _ in range(n)]

    print christmas_eve(n,k,h)

Submission status: Accepted

Solution code:
import math
import copy
import sys
import fractions
import numpy as np
from functools import reduce

# ===FUNCTION===

def getInputInt():
    inputNum = int(input())
    return inputNum

def getInputListInt():
    outputoData = []
    inputData = input().split()
    outputData = [int(n) for n in inputData]

    return outputData

def getSomeInputInt(n):
    outputDataList = []
    for i in range(n):
        inputData = int(input())
        outputDataList.append(inputData)

    return outputDataList

def getSomeInputListInt(n):
    inputDataList = []
    outputDataList = []
    for i in range(n):
        inputData = input().split()
        inputDataList = [int(n) for n in inputData]
        outputDataList.append(inputDataList)

    return outputDataList

# ===CODE===

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

data.sort()


ans = sys.maxsize

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

print(ans)

Submission status: Accepted

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

  if d<ans:
    ans=d
  


print(ans)

Submission status: Accepted

Solution code:
import sys
from collections import deque
stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

n, k = na()
li = [ni() for i in range(n)]
li.sort()
ans = 100000000000000000
for i in range(n - k + 1):
    dif = li[i + k - 1] - li[i]
    ans = min(ans, dif)
    if ans == 0:
        break
print(ans)

Submission status: Accepted

Solution code:

import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)
##############################

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(ans, (H[i+K-1] - H[i]))

print(ans)

Submission status: Accepted

Solution code:
import heapq


n, k = map(int, input().split())
hs = list(zip(sorted([int(input()) for _ in range(n)]),
              range(n)))
minus_hs = []
for i in range(n):
    minus_hs.append((-hs[i][0], hs[i][1]))

q = hs[:k - 1][:]
heapq.heapify(q)
minus_q = minus_hs[:k - 1][:]
heapq.heapify(minus_q)

min_diff = float('inf')
for i in range(k - 1, n):
    heapq.heappush(q, hs[i])
    heapq.heappush(minus_q, minus_hs[i])
    while q[0][1] < i - k + 1:
        heapq.heappop(q)
    while minus_q[0][1] < i - k + 1:
        heapq.heappop(minus_q)
    min_diff = min(min_diff, abs(q[0][0] + minus_q[0][0]))
print(min_diff)

Submission status: Accepted

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

sa = sorted(a)
v=10**9
for i in range(n-k+1):
	if v > sa[i+k-1]-sa[i]:
		v = sa[i+k-1]-sa[i]
print(v)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
# c
_, k, *h = map(int, open(0).read().split())
h.sort()
ans = 10**9
for hmin, hmax in zip(h, h[k-1:]):
    ans = min(ans, hmax-hmin)
print(ans)

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
HList = [ int(input()) for _ in range(N)]
HList.sort()
ans = 10**10
for n in range(N-K+1):
    num = abs( HList[n]-HList[n+K-1])
    if num < ans:
        ans = num
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
h = [int(input()) for i in range(n)]
h.sort()
result = float('inf')
for i in range(n - k + 1):
  if h[i + k - 1] - h[i] < result:
    result = h[i + k - 1] - h[i]
print(result)

Submission status: Accepted

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

    sort_h = sorted(h, reverse=True)

    ans = float('inf')
    for i in range(N-K+1):
        tmp = sort_h[i] - sort_h[i + K - 1]
        if ans > tmp:
            ans = tmp

    print(ans)

main()

Submission status: Accepted

Solution code:
N,M = [int(s) for s in input().split()]
ls=[int(input()) for i in range(N)]
ls.sort()
result=[ls[i+M-1]-ls[i] for i in range(N-M+1)]
print(min(result))

Submission status: Accepted

Solution code:
import random

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

def quick_sort(array):
    low_array = []
    high_array = []
    len_array = len(array)
    if len_array <= 1:
        return array
    pivot = random.choice(array)
    pivot_count = 0
    for i in range(len_array):
        if pivot > array[i]:
            low_array.append(array[i])
        elif pivot < array[i]:
            high_array.append(array[i])
        else:
            pivot_count += 1
    low_array = quick_sort(low_array)
    high_array = quick_sort(high_array)
    return low_array + [pivot]*pivot_count + high_array

H = quick_sort(H_input)

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

print(quick_sort(min)[0])

Submission status: Accepted

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

sl = sorted(l)
mn = 10 ** 9
for i in range(N):
    if i + K - 1< N:
        if mn > sl[i + K - 1] - sl[i]:
            mn = sl[i + K - 1] - sl[i];

print(mn)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
h=[int(input()) for i in range(n)]
h.sort()
d=[0]*n
for j in range(n-1):
  d[j+1]=d[j]+h[j+1]-h[j]
m=float('inf')
for l in range(n-k+1):
  m=min(m,d[l+k-1]-d[l])
print(m)

Submission status: Accepted

Solution code:
import sys
import math

#sys.setrecursionlimit(10000)

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

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

h.sort()
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())
lis = []
for i in range(n):
	h = int(input())
	lis.append(h)
lis.sort()
diff = []
for j in range(n-k+1):
	diff.append(lis[j+k-1] - lis[j])
print(min(diff))

Submission status: Accepted

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

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

psa=float('inf')
for i in range(n-k+1):
  sa=hs[k+i-1]-hs[i]
  if psa>sa:
    psa=sa

print(psa)

Submission status: Accepted

Solution code:
def main():
    N, K = map(int, input().split())
    H = []
    for _ in range(N):
        H.append(int(input()))
    H = sorted(H)
    min_d = float('inf')
    for i in range(N-K+1):
        min_d = min(min_d, H[i+K-1]-H[i])
    return min_d


if __name__ == '__main__':
    print(main())

Submission status: Accepted

Solution code:
#### import ####
import sys
import math
from collections import defaultdict

#### 設定 ####
sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

#### 定数 ####
mod = 10**9 + 7

#### 読み込み ####
def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N):
    read_all = [tuple(map(int, input().split())) for _ in range(N)]
    return map(list,zip(*read_all))

#################

N,K = II()
h = [I() for _ in range(N)]
h.sort()

ans = float('inf')

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

print(ans)

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/abc115/tasks/abc115_c
# C - Christmas Eve
import itertools

n, k = list(map(int, input().split()))
h_list = []

for _ in range(n):
    h_list.append(int(input().split()[0]))

h_list = sorted(h_list)

min_value = float('inf')

for i, v in enumerate(h_list[:-k+1]):
    diff_h = h_list[i+k-1] - v
    if diff_h < min_value:
        min_value = diff_h

print(min_value)

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
para = [int(input()) for i in range(N)]
para.sort(reverse=True)
val = float("inf")
for i in range(0,N-K+1):
    val = min(val,para[i]-para[i+K-1])
print(val)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

ans = H[-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:
# ABC 115, c
n, k = [int(el) for el in input().split(' ')]
H = sorted([int(input()) for i in range(n)])

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

print(min(b))

Submission status: Accepted

Solution code:
import sys
# from collections import Counter
input = sys.stdin.readline

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

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

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
H = [int(input()) for _ in range(n)]
H.sort()
d = []
for i in range(1,n):
  d.append(abs(H[i]-H[i-1]))
S = [0]
for i in d:
  S.append(S[-1]+i)
min_num = 10**9
for i in range(k-1,n):
  min_num = min(min_num, abs(S[i]-S[i-k+1]))
print(min_num)

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:
import sys
from operator import itemgetter as ig
from collections import defaultdict as dd

# お約束
args = None
INF = float("inf")
MOD = int(1e9 + 7)
def int1(n):
    return int(n) - 1
def input():
    return next(args)
def parse(*params):
    if len(params) == 1:
        return params[0](next(args))
    return tuple(p(v) for p, v in zip(params, next(args).split()))
def debug(*v):
    if __debug__:
        print(*v, file=sys.stderr)

# エントリーポイント
def main():
    N, K = parse(int, int)
    H = [parse(int) for _ in range(N)]

    h = sorted(H)

    diff_h = tuple(h[l + K - 1] - h[l] for l in range(N - K + 1))

    print(min(diff_h))

if __name__ == '__main__':
    args = iter(sys.stdin.read().split("\n"))
    main()

Submission status: Accepted

Solution code:
import sys
from operator import itemgetter as ig
from collections import defaultdict as dd

# お約束
args = None
INF = float("inf")
MOD = int(1e9 + 7)
def int1(n):
    return int(n) - 1
def input():
    return next(args)
def parse(*params):
    if len(params) == 1:
        return params[0](next(args))
    return tuple(p(v) for p, v in zip(params, next(args).split()))
def debug(*v):
    if __debug__:
        print(*v, file=sys.stderr)

# エントリーポイント
def main():
    N, K = parse(int, int)
    H = [parse(int) for _ in range(N)]

    h = sorted(H)
    l, r = 0, K

    sum_h = [0]
    for n in range(1, N):
        sum_h += [sum_h[-1] + (h[n] - h[n - 1])]
    debug(sum_h)

    diff_h = tuple(sum_h[l + K - 1] - sum_h[l] for l in range(N - K + 1))
    debug(diff_h)
    print(min(diff_h))

if __name__ == '__main__':
    args = iter(sys.stdin.read().split("\n"))
    main()

Submission status: Accepted

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


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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import math
import sys
from itertools import accumulate
from functools import reduce
import queue
from fractions import gcd

def lcm(a, b):
    return a * b // gcd(a, b)

def combination_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

def permutations_count(n, r):
    return math.factorial(n) // math.factorial(n - r)

sys.setrecursionlimit(10000)

mod = 1000000007

"""
# 0埋め, 小数点出力桁指定時のときの出力
a = 100
b = 0.987654321
print("{0:06d}-{1:6f}".format(a,b))
000100-0.987654

# 文字列をリストに格納
char_list = list("abcd") # ["a","b","c","d"]

# 標準入力取得
## 文字列
    = sys.stdin.readline().rstrip()
    = list(sys.stdin.readline().rstrip())

## 数値
    = int(sys.stdin.readline())
    = map(int, sys.stdin.readline().split())
    = list(map(int, sys.stdin.readline().split()))
    = list(map(int, sys.stdin.readlines()))
    = [list(map(int,list(sys.stdin.readline().split()))) for i in range(h)]  # 二次元配列入力　二次元マップみたいな入力のとき
"""

if __name__ == "__main__":
    n,k= map(int, sys.stdin.readline().split())
    h = list(map(int, sys.stdin.readlines()))
    h.sort()
    # print(h)
    ans=10**9
    for i in range(n-k+1):
        # print("i:",h[i])
        # print("i+k:",h[i+k-1])
        tmp=h[i+k-1]-h[i]
        ans=min(ans,tmp)
        # print("tmp:",ans)
    print(ans)

Submission status: Accepted

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

h.sort(reverse = True)
answer = h[0] - h[k-1]

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

print(answer)

Submission status: Accepted

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

Submission status: Accepted

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

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

h.sort()
dif = h[k-1] - h[0]

for i in range(k, n):
    temp = h[i] - h[i-k+1]
    if temp < dif:
        dif = temp

print(dif)

Submission status: Accepted

Solution code:
def resolve():
    N,k = map(int,input().split())
    K = k-1
    H = list(int(input()) for _ in range(N))
    H.sort()
    minh = 10**10
    for i in range(K,N):
        minh = min(minh,abs(H[i-K]-H[i]))
    print(minh)
resolve()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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


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

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

    return


# Generated by 1.1.3 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:
#!/usr/bin/env python3
import sys


def solve(N: int, K: int, h: "List[int]"):
    avg = sum(h)/N

    h.sort()

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

    return


# Generated by 1.1.3 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=[]
for i in range(n):
	h.append(int(input()))
h.sort()
ans=[]
for i in range(n-k+1):
	x=h[i+k-1]-h[i]
	ans.append(x)
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()
_min=1145141919
for i in range(N-K+1):
    _min = h[K+i-1]-h[i] if h[K+i-1]-h[i]<_min else _min
print(_min)

Submission status: Accepted

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

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

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

h.sort()

mi = 10 ** 9
temp = 10 ** 9

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

print(mi)

Submission status: Accepted

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

h.sort()

m = 10**9
for i in range(n - k + 1):
  m = min(h[i + k - 1] - h[i], m)

print(m)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import itertools
import collections


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

n,k=getints()
h=list(sorted([getint() for _ in range(n)]))

res = 1 << 30
dq = collections.deque()
for v in h:
    dq.append(v)
    if len(dq) == k:
        res = min(res, dq[-1] - dq[0])
        dq.popleft()

print(res)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import math
import sys
import itertools
import queue
from fractions import gcd

def lcm(a, b):
    return a * b // gcd(a, b)

def combination(n, r):
    return factorial(n) // (factorial(n - r) * factorial(r))

sys.setrecursionlimit(10000)

mod = 1000000007

"""
# 0埋め, 小数点出力桁指定時のときの出力
a = 100
b = 0.987654321
print("{0:06d}-{1:6f}".format(a,b))
000100-0.987654

# 文字列をリストに格納
char_list = list("abcd") # ["a","b","c","d"]

# 標準入力取得
## 文字列
    = sys.stdin.readline().rstrip()
    = list(sys.stdin.readline().rstrip())

## 数値
    = int(sys.stdin.readline())
    = map(int, sys.stdin.readline().split())
    = list(map(int, sys.stdin.readline().split()))
    = [list(map(int,list(sys.stdin.readline().split()))) for i in range(h)]  # 二次元配列入力　二次元マップみたいな入力のとき
"""

if __name__ == "__main__":
    N, M = map(int, sys.stdin.readline().split())
    L = sorted([ int(sys.stdin.readline()) for _ in range(N)])
    #print(L)

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

Submission status: Accepted

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

hList = sorted(hList)

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

for i in range(N-K+1):
    ansList[i] = hList[i+K-1] - hList[i]

print(min(ansList))