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: Runtime Error

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

def merge(array):                                   # line1
    mid = len(array)                                # line2
    if mid > 1:                                     # line3
        left = merge(array[:(mid//2)])               # line4
        right = merge(array[(mid//2):])              # line5
        array = []                                  # line6
        while len(left) != 0 and len(right) != 0:   # line7
            if left[0] < right[0]:                  # line8
                array.append(left.pop(0))           # line9
            else:                                   # line10
                array.append(right.pop(0))          # line11
        if len(left) != 0:                          # line12
            array.extend(left)                      # line13
        elif len(right) != 0:                       # line14
            array.extend(right)                     # line15
    return array



for i in range(n):
	a=int(input())
	S.append(a)
L=merge(S)
#print(L)
ans=-L[0]+L[n-1]
for i in range(k):
	if -L[i]+L[i+k-1]<ans:
		ans=-L[i]+L[i+k-1]
print(ans)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

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

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

print(ans)

Submission status: Runtime Error

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

# check
h.sort()
min = max(h) - min(h)
for i in range(0, N):
    if i + K - 1 >= N:
        break
    min = min(min, (h[i + K - 1] - h[i]))

print(min)

Submission status: Runtime Error

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

# check
h.sort()
min = 10 ** 9 - 1
for i in range(0, N):
    if N - 1 - i < i:
        break
    min = min(min, h[N - 1 - i] - h[i])

print(min)

Submission status: Runtime Error

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

# check
h.sort()
min = 10 ** 9 - 1
for i in zip(range(0, N)):
    if N - 1 - i < i:
        break
    min = min(min, h[N - 1 - i] - h[i])

print(min)

Submission status: Runtime Error

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

#############
# Libraries #
#############

import sys
input = sys.stdin.readline

import math
#from math import gcd
import bisect
from collections import defaultdict
from collections import deque
from functools import lru_cache


#############
# Constants #
#############

MOD = 10**9+7
INF = float('inf')

#############
# Functions #
#############

######INPUT######
def I(): return int(input().strip())
def S(): return input().strip()
def IL(): return list(map(int,input().split()))
def SL(): return list(map(str,input().split()))
def ILs(n): return list(int(input()) for _ in range(n))
def SLs(n): return list(input().strip() for _ in range(n))
def ILL(n): return [list(map(int, input().split())) for _ in range(n)]
def SLL(n): return [list(map(str, input().split())) for _ in range(n)]

######OUTPUT######
def P(arg): print(arg); return
def Y(): print("Yes"); return
def N(): print("No"); return
def E(): exit()
def PE(arg): print(arg); exit()
def YE(): print("Yes"); exit()
def NE(): print("No"); exit()

#####Shorten#####
def DD(arg): return defaultdict(arg)


#####Inverse#####
def inv(n): return pow(n, MOD-2, MOD)

######Combination######
kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret

######Factorization######
def factorization(n):
  arr = []
  temp = n
  for i in range(2, int(-(-n**0.5//1))+1):
    if temp%i==0:
      cnt=0
      while temp%i==0: 
        cnt+=1 
        temp //= i
      arr.append([i, cnt])
  if temp!=1:
    arr.append([temp, 1])
  if arr==[]:
    arr.append([n, 1])
  return arr

#####MakeDivisors######
def make_divisors(n):
  divisors = []
  for i in range(1, int(n**0.5)+1):
    if n % i == 0:
      divisors.append(i)
      if i != n // i: 
        divisors.append(n//i)
  return divisors

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

#####BitCount#####
def count_bit(n):
  count = 0
  while n:
    n &= n -1
    count += 1
  return count

#####ChangeBase#####
def base_10_to_n(X, n):
  if X//n:
    return base_10_to_n(X//n, n)+[X%n]
  return [X%n]

def base_n_to_10(X, n):
  return sum(int(str(X)[-i])*n**i for i in range(len(str(X))))

#####IntLog#####
def int_log(n, a):
  count = 0
  while n>=a:
    n //= a
    count += 1
  return count

#############
# Main Code #
#############

N,K = IL()
T = ILs(N)

T.sort()

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

Submission status: Runtime Error

Solution code:
def main():
    N, K = (int(i) for i in input().split())
    H = [int(i) for i in input().split()]
    H.sort()
    ans = 10**12
    for i in range(N-K+1):
        ans = min(ans, H[i+K-1] - H[i])

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10 ** 6)


def input():
    return sys.stdin.readline()[:-1]


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

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

Submission status: Runtime Error

Solution code:
q

Submission status: Runtime Error

Solution code:
import sys
import numpy as np
input = sys.stdin.readline


def readstr():
    return input().strip()


def readint():
    return int(input())


def readnums():
    return map(int, input().split())


def readstrs():
    return input().split()


def main():
    N, K = readnums()
    h = np.sort(np.array([readint() for _ in range(N)]))
    ans = 0
    for i in range(N - K + 1):
        if not i:
            ans = h[i + 2] - h[i]
        else:
            ans = min(ans, h[i + 2] - h[i])

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from heapq import heapify, heappop, heappush
 
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
 
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N, K = MAP()
H = [INT() for i in range(N)]
H.sort()
ans = INF
for i in range(N-K+1):
    mi = H[i:i+K][0]
    ma = H[i:i+K][2]
    tmp = ma - mi
    ans = min(tmp, ans)
print(ans)

Submission status: Runtime Error

Solution code:
#AtCorder Begginer Contest 161 Christmas Eve
N,K= map(int, input().split())
Tree_List= [int(input()) for i in range(N)]
Tree_List=np.array(sorted(Tree_List))
Answer=min(Tree_List[K-1:]-Tree_List[:-(K-1)])
print(Answer)

Submission status: Runtime Error

Solution code:
#AtCorder Begginer Contest 161 Christmas Eve
N,K= map(int, input().split())
Tree_List= [int(input()) for i in range(N)]
Tree_List=np.array(sorted(Tree_List))
min(Tree_List[K-1:]-Tree_List[:-(K-1)])

Submission status: Runtime Error

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

Submission status: Runtime Error

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

a.sort()

ans = a[-1]-a[0]
for i in range(n-k+1):
  if ans > a[i+k-1]-a[i]:
    ans = a[i+k]-a[i]

print(ans)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
N, K = list(map(int,input().split()))
h = [None for _ in range(N)]
MIN = float('inf')
for i in range(N):
  h[i] = int(input())
H = sorted(h)
for i in range(N-2):
  if MIN >= H[i+(K-1)] - H[i]:
    MIN = H[i+(K-1)] - H[i]
print(MIN)

Submission status: Runtime Error

Solution code:
import numpy as np
n, k = map(int, input().split())
h = [int(input()) for _ in range(n)]
h = np.array(sorted(h))
diff = np.diff(h)
diff = np.tile(diff, (n - k + 1, 1))
mask_x = [[i] * (k - 1) for i in range(n - k + 1)]
mask_y = [range(i, i + k - 1) for i in range(n - k + 1)]
cost = np.sum(diff[mask_x, mask_y], axis=1)
print(cost.min())

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
n,m= map(int,input().split())

B = []
C = [0]*n
ans = 0
pna = 0
for i in range(m):
    A = list(map(str,input().split()))
    if A[0] not in B:
        if A[1]=="AC" :
            B.append(A[0])
            ans += 1
            pna += C[int(A[0])-1]
    	else:
      	  C[int(A[0])-1] += 1 
print(ans,pna)

Submission status: Runtime Error

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])
print(ans)

Submission status: Runtime Error

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

Submission status: Runtime Error

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


h=list(int(input()) for in range(n))
h.sort()
result=10**9

i=0
while i+k-1<n:
  result=min(result,h[i+k-1]-h[i])
  
  i+=1
  
print(result)

Submission status: Runtime Error

Solution code:
inf = 10**9+7
mod = 10**9+7
n = int(input())
a = [int(input()) for i in range(n)]
h = max(a)
print(sum(a)-h+h//2)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
N , M = (map(int , input().split()))
tree = []
for i in range(N):
    tree.append(int(input()))
sort_tree = sorted(tree)
print(min([sort_tree[N-M+i] - sort_tree[i] for i in range(N-M+1)]))

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
import sys
input= lambda: sys.stdin.readline().rstrip()
def pin(type=int):return map(type,input().split())
def tup(t=int):return tuple(pin(t))
#%%code
def resolve():
    N,K=pin()
    H=[int(input()) for i in range(N)]
    H.sort()
    ans=1000000000
    for i in range(N-K+1):
        temp=[H[i],H[i+1],H[i+2]]
        xam=max(temp)
        nim=min(temp)
        ans=min(xam-nim,ans)
    print(ans)
#%%submit!
resolve()

Submission status: Runtime Error

Solution code:
#%% for atcoder uniittest use
import sys
input= lambda: sys.stdin.readline().rstrip()
def pin(type=int):return map(type,input().split())
def tup(t=int):return tuple(pin(t))
#%%code
def resolve():
    N,K=pin()
    H=[int(input()) for i in range(N)]
    H.sort()
    ans=1000000000
    for i in range(N-K+1):
        temp=[H[i],H[i+1],H[i+2]]
        xam=max(temp)
        nim=min(temp)
        ans=min(xam-nim,ans)
    print(ans)
#%%submit!
resolve()

Submission status: Runtime Error

Solution code:


def solve():

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

    # sortして走破？
    hlist.sort()
    l = []

    for i in range(N-K+1):
        l.append(hlist(i+K-1)-hlist(i))

    print(min(l))


if __name__ == "__main__":
    solve()

Submission status: Runtime Error

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: Runtime Error

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: Runtime Error

Solution code:
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):
    ans = min(ans, H[i+N-K]-H[i])

print(ans)

Submission status: Runtime Error

Solution code:
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):
    ans = min(ans, H[i+N-K-1]-H[i])

print(ans)

Submission status: Runtime Error

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

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

print(ans)

Submission status: Runtime Error

Solution code:
def main():
	N, K = [int(v) for v in input().split()]
    
    H = sorted([-int(input() for _ in range(N)])
    
    print(-H[0] + H[K - 1])
    
main()

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(1000000000)
INF=10**9
n,a,b,c=map(int,input().split())
l=[int(input()) for i in range(n)]
def dfs(i,x,y,z):
    if i==n:
        if min(x,y,z)>0:
            return abs(x-a)+abs(y-b)+abs(z-c)-30
        else:
            return INF
    ans=dfs(i+1,x,y,z)
    ant=dfs(i+1,x+l[i],y,z)+10
    anu=dfs(i+1,x,y+l[i],z)+10
    anv=dfs(i+1,x,y,z+l[i])+10
    return min(ans,ant,anu,anv)
print(dfs(0,0,0,0))

Submission status: Runtime Error

Solution code:
n, k, H = map(int, open(0).read().split())
H.sort()
ans = 10 ** 10
for h0, h1 in zip(H, H[k-1:]):
  ans = min(ans, h1-h0)
print(ans)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
import sys, math, itertools, collections, bisect
n,k=map(int,input().split())
h=[]
for i in range(n):
    tmp=int(input())
    h.append(tmp)
h.sort()
min=10000000000
for i in range(len(h)-2):
    tmp2=h[i+k-1]-h[i]
    if min>tmp2:
        min=tmp2
print(min)

Submission status: Runtime Error

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

mindiff = float('inf')
for i in range(N-K+1):
    diff = selected[i] - selected[i+K-1]
    if diff <= mindiff:
        mindiff = diff
print(mindiff)

Submission status: Runtime Error

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

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

print(ans)

Submission status: Runtime Error

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

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

print(ans)

Submission status: Runtime Error

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

    for i in range(1, N-K+1):
        ans = min(ans, max(hh[i:i+K])-min(hh[i:i+K]))
    print(ans)
    
main()

Submission status: Runtime Error

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

H = sorted(H)
ans = float('Inf')
for i in range(N - K + 1):
    ans = min(ans, H[i + 2] - H[i])
print(ans)

Submission status: Runtime Error

Solution code:
n,k= map(int, input().split())
 
#print(n,k)
Ar = [0]*1000
 
for i in range(n):
#	print("i=",i)
	dat = int(input())
	Ar[i] = dat
#	print(Ar)

ans = 10000

for i in range(n-k+1):
	h = Ar[i]
	l = Ar[i]
#	print("Start = ",i)
	for j in range(i,i+k+1):
#		print("check st",j)
		if h < Ar[j]:
			h = Ar[j]
		if l > Ar[j]:
			l = Ar[j]
	if ans > h-l:
		ans = h-l
#	print("diff=",ans,h,l)

print (ans)

Submission status: Runtime Error

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

#print(n,k)
Ar = [0]*10000

for i in range(n):
#	print("i=",i)
	dat = int(input())
	Ar[i] = dat
#	print(Ar[i])

ans = 100000
for i in range(n-k):
	h = 0
	l = 0
	for j in range(k)
    	if h < Ar[i]:
        	h = Ar[i]
		if l > Ar[i]:
			l = Ar[i]
		if ans > h-l:
			ans = h-l
            
print (ans)

Submission status: Runtime Error

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

#print(n,k)
Ar = [0]*10000

for i in range(n):
#	print("i=",i)
	dat = int(input())
	Ar[i] = dat
#	print(Ar[i])

sum = 0
for i in range(k):
	sum = sum + Ar[i]

tmp = 0
for i in range(k,n):
#	print(i)
	if sum > sum - Ar[i-k] + Ar[k]:
		sum = sum - Ar[i-k] + Ar[k]

print (sum)

Submission status: Runtime Error

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

#print(n,k)
Ar = [0]*100

for i in range(n):
#	print("i=",i)
	dat = int(input())
	Ar[i] = dat
#	print(Ar[i])

sum = 0
for i in range(k):
	sum = sum + Ar[i]

tmp = 0
for i in range(k,n):
#	print(i)
	if sum > sum - Ar[i-k] + Ar[k]:
		sum = sum - Ar[i-k] + Ar[k]

print (sum)

Submission status: Runtime Error

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

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

Submission status: Runtime Error

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

tree = []
for _ in range(n):
    tree.append(int(input()))

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

Submission status: Runtime Error

Solution code:
5 3
5
7
5
7
7

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

list_H.sort()
ans = []

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

print(min(ans))

Submission status: Runtime Error

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,N+1):
    m = min(h[i-K:i])
    M = max(h[i-K:i])
    ans = min(ans,M-m)
print(ans)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
from collections import Counter

N, K = list(map(int,input().split()))
H = []
for i in range(N):
    H.append(int(input()))
H = sorted(H,reverse = False)
C = Counter(H)
CH = list(C.values()) 

cnt = 0
for i in range(len(CH)):
    if CH[i] >= K:
        cnt += 1
        break
if cnt != 0:
    print('0')
else:
    #print(H[K-1] - H[0])
    print(min(H[K-1] - H[0],H[K] - H[1],H[K+1] - H[2]))

Submission status: Runtime Error

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

Submission status: Runtime Error

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

h.sort()

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

print(m)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
H = sorted([int(input()) for _ in range(N)])
cum = [0] * (N-K+1)
for i in range(N-K+1):
    cum[i] = abs(H[i] - H[i+2])
print(sorted(cum)[0])

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
H = sorted([int(input()) for _ in range(N)])
cum = [0] * K
for i in range(N-K+1):
    cum[i] = abs(H[i] - H[i+2])
print(sorted(cum)[0])

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
n, k = gets.chomp.split(" ").map(&:to_i)
h = []
ans = 1 << 30
n.times do
    h << gets.chomp.to_i
end
h.sort!
(n-k+1).times do |i|
    ans = [ans, h[i+k-1]-h[i]].min
end
puts ans

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
N,K=map(int,input().split())
H=list(map(int,input().split()))
H.sort()
ans=10**6

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

print(ans)

Submission status: Runtime Error

Solution code:
from itertools import combinations

N, K = [int(n) for n in input().split()]
h = sorted([int(input()) for _ in range(N)])
ans = []
for c in combinations(h, K):
    ans.append(c[2]-c[0])
print(min(ans))

Submission status: Runtime Error

Solution code:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
#define all(x) (x).begin(), (x).end()
typedef long long ll;

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> h(N);
  for (int i = 0; i < N; i++) cin >> h[i];
  sort(all(h));
  ll ans = 1000000000;
  for (int i = 0; i + K <= N; i++) {
    ll hmax = *max_element(&h[i], &h[i + K]);
    ll hmin = *min_element(&h[i], &h[i + K]);
    ans = min(ans, hmax - hmin);
  }

  cout << ans << endl;
}

Submission status: Runtime Error

Solution code:
#include <iostream>
#include <vector>
using namespace std;
#define all(x) (x).begin(), (x).end()
typedef long long ll;

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> h(N);
  for (int i = 0; i < N; i++) cin >> h[i];
  sort(all(h));
  ll ans = 1000000000;
  for (int i = 0; i + K <= N; i++) {
    ll hmax = *max_element(&h[i], &h[i + K]);
    ll hmin = *min_element(&h[i], &h[i + K]);
    ans = min(ans, hmax - hmin);
  }

  cout << ans << endl;
}

Submission status: Runtime Error

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

Submission status: Runtime Error

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

A=sorted(A)
t=10**9
for i in range(K):
    t=min(t,abs(A[K-1+i]-A[i]))
print(t)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
hl = [int(input()) for _ in range(n)]
delta = 10*10**9

hl_s = sorted(hl, reverse=True)

if n > 1000:
    if hl_s[n//2] > (hl_s[0] + hl_s[n])//2: #中央値＞平均値
        hl_s = hl_s[:n//2]
    else:
        hl_s = hl_s[n//2:]

for i in range(len(hl_s)-k+1):
    h_max = max(hl_s[i:i+k])
    h_min = min(hl_s[i:i+k])
    if delta > (h_max - h_min):
        delta = h_max - h_min
    if delta == 0:
        break

print(delta)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
hl = [int(input()) for _ in range(n)]
delta = 10*10**9

hl_s = sorted(hl, reverse=True)

if n > 10000:
    if hl_s[n//2] > (hl_s[0] + hl_s[n])//2: #中央値＞平均値
        hl_s = hl_s[:n//2]
    else:
        hl_s = hl_s[n//2:]

for i in range(len(hl_s)-k+1):
    h_max = max(hl_s[i:i+k])
    h_min = min(hl_s[i:i+k])
    if delta > (h_max - h_min):
        delta = h_max - h_min
    if delta == 0:
        break

print(delta)

Submission status: Runtime Error

Solution code:
#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>

using namespace std;

int main(){
  int N, K;
  cin >> N >> K;

  vector<int> H(N);
  for(int i=0; i<N; i++){
    cin >> H[i];
  }
  sort(H.begin(), H.end());

  int min = int(1e9);
  for(int i=0; i<=N-K; i++){
    if (H[i+K-1] - H[i] < min){
      min = H[i+K-1] - H[i];
    }
  }
  cout << min << endl;
  return 0;
}

Submission status: Runtime Error

Solution code:
n,s=map(int, input().split())
N=[]
for _ in range(n):
    N.append(int(input()))
N=sorted(N)
for i in range(s-1,len(N)):
    diff_list.append(N[i]-N[i-s+1])
print(min(diff_list))

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
lst=[int(input()) for _ in range(n)]
lst=sorted(lst)
ab=lst[k-1]-lst[0]
cd=lst[-1]-lst[-k]
print(ans=min(ab,cd))

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
N, K = list(map(int, input().split()))
h_l = [0]*100000
i = 0
for i in range(N):
    x = int(input())
    h_l[i] = x
    x = 0
j = 0
min = 1000000000
h_list = [j for j in h_l if h_l[j] == 0]
h_list.sort()
y = 0
q = 0
while q < N-K+1:
    y = h_list[q + K - 1] - h_list[q]
    if y < min:
        min = y
        y = 0
    else:
        y = 0
    q += 1
print(min)

Submission status: Runtime Error

Solution code:
N, K = list(map(int, input().split()))
# print(N)
# print(K)
h_l = [0]*100000
i = 0
for i in range(N):
    x = int(input())
    h_l[i] = x
    x = 0
j = 0
min = 1000000000
h_list = [j for j in h_l if h_l[j] == 0]
h_list.sort()
# print(h_list)
y = 0
q = 0
while q < N-K+1:
    y = h_list[q + K - 1] - h_list[q]
#    print(q)
#    print(h_list[q+K-1], h_list[q])
    if y < min:
        min = y
        y = 0
    else:
        y = 0
    q += 1
print(min)

Submission status: Runtime Error

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

an.sort()
dn = [-1 for _ in range(n - k + 1)]
for i in range(n - k + 1):
  dn[i] = an[i+k] - an[i]

print(min(dn))

Submission status: Runtime Error

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