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 = list(map(int, input().split()))


def check(k, hs, s):
    if k == 0:
        return max(s) - min(s)
    r = []
    for i, h in enumerate(hs):
        s.append(h)
        hs.remove(h)
        r.append(check(k - 1, hs, s[::]))
        s.pop(-1)
        hs.insert(i, h)
    return min(r)


def main():
    hs = [int(input()) for i in range(N)]
    print(check(K, hs, []))


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
#coding:utf-8
n,k=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: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

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

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


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

print(min(cand))

Submission status: Runtime Error

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

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

Submission status: Runtime Error

Solution code:
n, k = [int(i) for i in input().split()]
h = [int(input()) for i in range(n)]
INF = 10 ** 9 + 1

h.sort()
ans = INF
for ki in range(k):
    d = h[k + ki - 1] - h[ki]
    if ans > d:
        ans = d

print(ans)

Submission status: Runtime Error

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

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

h_array.sort()

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

print(result)

Submission status: Runtime Error

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

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

h_array.sort()

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

print(result)

Submission status: Runtime Error

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

Solution code:
n, k = 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: Runtime Error

Solution code:
n, x = map(int, input().split(' '))
lst = []
for i in range(n):
  lst.append(int(input()))
  lst.sort()
  minn = 999999999999
  for i in range(0, n-x+1):
    if lst[i+x]-lst[i] < minn:
      minn = lst[i+x] - lst[i]
  print(minn)

Submission status: Runtime Error

Solution code:
N,K=map(int,raw_input().split())
P=[]
minus=1000000
save=0

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

for i in range(N):
  if i+K>N:
    break
  save=int(P[i+K])-int(P[i])
  if minus>save:
    minus=save
  
print minus

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
import numpy as np
N = int(input())
K = int(input())

e = [0]*N

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

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

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

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

Submission status: Runtime Error

Solution code:
import numpy as np
N = int(input())
K = int(input())

e = [0]*N

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

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

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

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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


list2=list()

h.sort()

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

Submission status: Runtime Error

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

l = []
for i in range(N):
    a = []
    for j in range(N):
        if i != j and h[i]-h[j]>=0:
            a.append(h[i]-h[j])
    if len(a) >= K-1:
        l.append(a)

l2 = []
for i in range(len(l)):
    a = min(l[i])
    l[i].remove(a)
    l2.append(min(l[i]))
print(min(l2))

Submission status: Runtime Error

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

l = []
for i in range(N):
    a = []
    for j in range(N):
        if i != j and h[i]-h[j]>=0:
            a.append(h[i]-h[j])
    if len(a) >= K-1:
        l.append(a)

l2 = []
for i in range(len(l)):
    a = min(l[i])
    l[i].remove(a)
    l2.append(min(l[i]))
print(min(l2))

Submission status: Runtime Error

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

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

A.sort(reverse=True)
    
for i in range(k):
    diff = A[i] - A[i + k -1]
    D.append(diff)
    
print(min(D))

Submission status: Runtime Error

Solution code:
S = input()
S = S.split()
N = int(S[0])
K = int(S[1])
h = [int(input()) for i in range(N)]
h.sort()
ans = 10**9 + 1
for i in range(N - K + 1):
    tmp = h[i + 3 - 1] - h[i]
    if(tmp < ans):
        ans = tmp
print(ans)

Submission status: Runtime Error

Solution code:
NK = input().split(' ')
N, K = int(NK[0]), int(NK[1])
trees = []
for i in range(N):
  trees.append(int(input()))
trees.sort(reversed=True)
min = 0
for i in range(N-K):
  h = trees[i+K]-trees[i]
  if h < min:
    min = h
print(min)

Submission status: Runtime Error

Solution code:
NK = input().split(' ')
N, K = int(NK[0]), int(NK[1])
trees = []
current = 0
for i in range(N):
  tree = int(input())
  if len(trees) < K:
    trees.append(tree)
  elif tree > min(trees) and tree < max(trees):
    trees.append(tree)
    tmp1 = trees.remove(min(trees))
    tmp2 = trees.remove(max(trees))
    if max(tmp1)-min(tmp1) > max(tmp2)-min(tmp2):
      trees = tmp2
    else:
      trees = tmp1
print(max(trees)-min(trees))

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
n = nnk[0]
k = nnk[1]

hs = [int(input()) for l in range(n)]
hs.sort()

num = n - k + 1

hmmm = []
for l in range(num):
    hmmm.append(hs[k+l-1] - hs[l])

hmmm.sort()
print(hmm[0])

Submission status: Runtime Error

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


nnk = list(map(lambda x: int(x), input().split(" ")))
n = nnk[0]
k = nnk[1]

hs = [int(input()) for l in range(n)]
hs.sort()

num = n - k + 1

hmmm = []
for l in range(num):
    hmmm.append(hs[k+l-1] - hs[l])

hmmm.sort()
print(hmm[0])

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
N, K = map(int, input().split())
heights = sorted([int(input() for _ in range(N)])
ans = float('inf')
for i in range(N - K + 1):
    if abs(heights[i] - heights[i + K - 1]) < ans:
        ans = abs(heights[i] - heights[i + K - 1])
print(ans)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
from sys import stdin
import numpy as np
import sys

N, K = [int(x) for x in stdin.readline().rstrip().split()]
h = np.array(map(int, [stdin.readline().rstrip() for _ in range(N)]))

hs = h.sort()
hmin = sys.maxint

for i in range(0, N-K):
    hdiff = hs[i+K] - hs[i]
    if hdiff < hmin:
        hmin = hdiff

print(hmin)

Submission status: Runtime Error

Solution code:
from sys import stdin
a = stdin.readline().split(" ")
num=int(a[0])
find=int(a[1])
j=1
w2=[]
while j<=num:
    z = stdin.readline().rstrip('\n')
    w2.append(int(z))
    j+=1
w2.sort()
minnum=w2[find-1]-w2[0]
for i in range(len(w2)-2):
    minnum=min(minnum,w2[i+find-1]-w2[i])
print(minnum)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

tree_list = []
sort_list = []
for x in range(N):
    tree_list.append(int(input()))

sort_list = sorted(tree_list)

min_heigt = sort_list[2] - sort_list[0]

for x in range(N-M+1):
    nagasa =sort_list[x+2] - sort_list[x] 
    if min_heigt > nagasa:
        min_heigt = nagasa

print(min_heigt)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
N,K = map(int,input().split())
c = [int(input()) for i in range(N)]
c.sort()
output = c[K-1] - c[0]
for i in range(N-K):
    output = c[K+i] - c[i+1] if output > c[K+i] - c[i+1]
print(output)

Submission status: Runtime Error

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

hi = []
i = 0
while i + (K - 1) < N:
    hi.append(int(h[i + 2] - h[i]))
    i += 1

print(min(hi))

Submission status: Runtime Error

Solution code:
ef solve(string):
    n, k = map(int, string.split("\n")[0].split(" "))
    ins = list(map(int, string.split("\n")[1:]))
    ins.sort()
    return str(min([ins[i + k - 1] - ins[i] for i in range(len(ins) - k + 1)]))


if __name__ == '__main__':
    n, k = map(int, input().split(" "))
    ins = [input() for _ in range(n)]
    print(solve("{} {}\n{}".format(n, k, "\n".join(ins))))

Submission status: Runtime Error

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

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

Submission status: Runtime Error

Solution code:
NK = input()
NK = N.split(" ")
N = int(NK[0])
K = int(NK[1])

h = []
for i in range(N):
  h.append(int(input()))
  
  
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: Runtime Error

Solution code:
NK = input()
NK = N.split(" ")
N = int(NK[0])
K = int(NK[1])


h = []
for i in range(N):
  h.append(int(input()))
  
  
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: Runtime Error

Solution code:
NK = input()
NK = N.split(" ")
N = int(NK[0])
K = int(NK[1])

h = []
for i in range(N):
  h.append(int(input()))
    
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: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
h = []
for i in range(N) : 
    s = input()
    h.append(int(s))
    
h = sorted(h)
minDiff = h[len(h) - 1] - h[0]
for i in range(N) : 
    try : 
        diff = h[len(h) - K + i] - h[i]
        print(f"diff={h[len(h) - K + i]} - {h[i]} = {diff}")
        minDiff = min(minDiff , diff)
        
    except : 
        if i == 0 : minDiff = 0
        continue
print(minDiff)

Submission status: Runtime Error

Solution code:
s = input().split(" ")
N = int(s[0])
K = int(s[1])

h = []
for i in range(N) : 
    s = input()
    h.append(int(s))
    
h = sorted(h)
minDiff = h[len(h) - 1] - h[0]
for i in range(N) : 
    try : 
        diff = h[len(h) - K + i] - h[i]
        print(f"diff={h[len(h) - K + i]} - {h[i]} = {diff}")
        minDiff = min(minDiff , diff)
        
    except : 
        if i == 0 : minDiff = 0
        continue
print(minDiff)

Submission status: Runtime Error

Solution code:
N,K = list(map(int, input().split()))
h = [int(input()) for i in range(N)]
candidate = None
h.sort()
for i in range(N-K+1):
    tmp = h[i+K+1] - h[i]
    if candidate == None or tmp < candidate:
        candidate = tmp
print(candidate)

Submission status: Runtime Error

Solution code:
def main():
  N, K = map(int, input().split(' '))
  # N本の木があって，i本目の木はh_i[m]
  # K本選んで電飾する.
  # 木の内一番高いのをh_max[m], 低いものをh_min[m]
  # min(h_max - h_min)をしたい.
  trees = [int(input()) for _ in range(N)]
  trees.sort()
  trees = [n - trees[0] for n in trees]
  # print(trees[K-1] - trees[0]) -> これだと，最初の方はかなり分散が大きい配列で，後ろの方が分散が小さい配列だとダメ
  # 例[0, 100, 101, 102] から2個選ぶ時, 上記だと100が帰ってくる.
  MIN = 100000000000000
  # print(trees)
  for n in range(K):
    t = trees[n+K-1] - trees[n]
    MIN = min(t, MIN)
  print(MIN)
main()

Submission status: Runtime Error

Solution code:
def main():
  N, K = map(int, input().split(' '))
  # N本の木があって，i本目の木はh_i[m]
  # K本選んで電飾する.
  # 木の内一番高いのをh_max[m], 低いものをh_min[m]
  # min(h_max - h_min)をしたい.
  trees = [int(input()) for _ in range(N)]
  trees.sort()
  trees = [n - trees[0] for n in trees]
  # print(trees[K-1] - trees[0]) -> これだと，最初の方はかなり分散が大きい配列で，後ろの方が分散が小さい配列だとダメ
  # 例[0, 100, 101, 102] から2個選ぶ時, 上記だと100が帰ってくる.
  MIN = 100000000000000
  # print(trees)
  for n in range(K):
    t = trees[n+K-1] - trees[n]
    print(trees[n], trees[n+K-1])
    MIN = min(t, MIN)
  print(MIN)
main()

Submission status: Runtime Error

Solution code:
N,K = list(map(int, input().split()))
h = [int(input()) for i in range(N)]
first_flg = True
h.sort()
for i in range(N-K-1):
    tmp = h[i+K-1] - h[i]
    if first_flg or tmp < candidate:
        first_flg = False
        candidate = tmp
print(candidate)

Submission status: Runtime Error

Solution code:
from statistics import mean, median,variance,stdev
import sys
import math

x = input().split()
#y = input().split()


#y = int(input())
#x = int(input())
a = []
n = int(x[0])
k = int(x[1])
for i in range(k):
    a.append(int(input()))
a = sorted(a)
p = []
for i in range(n-k-1):
    p.append(a[i+k-1]-a[i])
print(min(p))

Submission status: Runtime Error

Solution code:
from operator import sub

n, k = map(int, input().split())
hhh = sorted(int(input()) for _ in range(n))
print(min(map(sub, zip(hhh[k - 1:], hhh))))

Submission status: Runtime Error

Solution code:
a,b=map(int,input().split())
c=[]
for i in range(a):
	c.append(int(input()))
c=sorted(c)
f=[]
for k in range(a-b+1):
	e=[]
	f.append(e)
	for i in range(b):
		e.append(c[i+k])
g=[]
for i in range(b):
	g.append(max(f[i])-min(f[i]))
print(min(g))

Submission status: Runtime Error

Solution code:
a,b=map(int,input().split())
c=[]
for i in range(a):
	c.append(int(input()))
c=sorted(c)
f=[]
for k in range(len(c)-2):
	e=[]
	f.append(e)
	for i in range(b):
		e.append(c[i+k])
g=[]
for i in range(b):
	g.append(max(f[i])-min(f[i]))
print(min(g))

Submission status: Runtime Error

Solution code:
import numpy as np
N,K = map(int,input().split())
list = []
for i in range(N):
 list.append(int(input()))
list = sorted(list);
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())
list = [int(input()) for i in range(N)]
max(list())
list.sort()
print(min(list[i+K-1] - list[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(h)
mi=float('inf')
 
for i in range(n-k+1):
    mi = min(mi, h[i+k-1]-h[i])
 
print(mi)

Submission status: Runtime Error

Solution code:
import numpy as  np
import copy
for i in range(N-K+1):
    
    diff = 0
    print("h[i] is {}".format(h[i]))
    for j in range(K-1):
        if(h[i] >= h[j]):
            target = [a for a in h if a <= h[i] and a >= h[j]]
            print("0")
            exit()
        else :
            target = [a for a in h if a > h[i] and a < h[j]]
        media = (a.pop(np.abs(np.asarray(a) - h[i]).argmin()))
        choose_h =[h[i],h[j],media]
        diff = max(choose_h)-min(choose_h)
        if(diff < candidate):
            candidate = diff
print(candidate)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
import collections

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

h = h.sort(reverse=True)
c = collections.Counter(h)
flg = False
for i in c:
    if c[i] >= 3:
        print(0)
        flg = True
        break

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

Submission status: Runtime Error

Solution code:
in_t = input().split()
n = int(in_t[0])
k = int(in_t[1])
list = []
for i in range(k):
 list.append(int(input()))
for i in range(n - k):
 new = int(input())
 if (list[k-1] > new) | (new > list[2]):
  list.append(new)
  list.sort()
  diff_max = list[k-1] - list[k-2]
  diff_min = list[1] - list[2]
  if (diff_max > diff_min):
   list.remove(list[0])
  else:
    list.remove(list[k-1])
print(list[k-1] - list[0])

Submission status: Runtime Error

Solution code:
import math

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

crr_min = 10^9

for i in range(0,math.ceil((n-k+1)/2)):
    tmp1 = abs(h[n-k+i] - h[i])
    tmp2 = abs(h[n-1-i] - h[n-k-i])
    if tmp1==0 or tmp2==0:
        crr_min=0
        break

    if tmp1<crr_min:
        crr_min=tmp1

    if tmp2<crr_min:
        crr_min=tmp2


print(crr_min)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(1000000000)

n, k = map(int, input().split())
h_li = [int(input()) for i in range(n)]
a = max(h_li)-min(h_li)
dp = [[-1]*(a+5)]*100010

def dfs(i, choosed):
    if i == n:
        if len(choosed) != k:
            dif = 10000000010
        else:
            dif = max(choosed) - min(choosed)

    elif len(choosed) >= 2 and dp[len(choosed)][max(choosed)-min(choosed)] != -1:
        dif = dp[len(choosed)][max(choosed)-min(choosed)]
            
    elif len(choosed) == k:
        dif = max(choosed) - min(choosed)
        
    else:
        dif = min(
            dfs(i+1, choosed + [h_li[i]]),
            dfs(i+1, choosed)
        )

    return dif

print(dfs(0, []))

Submission status: Runtime Error

Solution code:
n=input().split()
n=[int(i) for i in n]
h=[0]*n[0]
for i in range(n[0]):
    #h.append(int(input()))
    h[i]=int(input())
h.sort()

q=-1
for i in range(len(h)):
    a=h[i+n[1]-1]-h[i]
    if q==-1 or q>a:
        q=a
    if q==0:
        break
print(q)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
h_li = [int(input()) for i in range(n)]
a = max(h_li)-min(h_li)
dp = [[-1]*(a+5)]*100010



def dfs(i, choosed):
    if i == n:
        if len(choosed) != k:
            dif = 10000000010
        else:
            dif = max(choosed) - min(choosed)

    elif len(choosed) >= 2 and dp[len(choosed)][max(choosed)-min(choosed)] != -1:
        dif = dp[len(choosed)][max(choosed)-min(choosed)]
            
    elif len(choosed) == k:
        dif = max(choosed) - min(choosed)
    else:
        dif = min(
            dfs(i+1, choosed + [h_li[i]]),
            dfs(i+1, choosed)
        )

    return dif

print(dfs(0, []))

Submission status: Runtime Error

Solution code:
import statistics
import numpy as np

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

h.sort()
median = statistics.median(h)
idx = np.abs(np.asarray(h) - median).argmin()
result = 10 ** 9

if abs(h[idx] - h[idx-(K-1)]) < abs(h[idx+(K-1)] - h[idx]):
    result = abs(h[idx] - h[idx-(K-1)])
else:
    result = abs(h[idx+(K-1)] - h[idx])
print(result)

Submission status: Runtime Error

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

for i in range(N):
        li.append(int(input()))

li.sort()

co = K - 1
hmin = 1000000
for i in range(N-K+2):
        if hmin > li[i+co] - li[i]:
                hmin = li[i+co] - li[i]
                if hmin == 0:
                        break
print(hmin)

Submission status: Runtime Error

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

if list(np.diff(hs)) == list(np.zeros(n-1, dtype=int)):
    print('0')
else:
    sorted_hs = np.sort(hs)
    print(min([min(np.diff(sorted_hs[i::k-1])) for i in range(k)]))

Submission status: Runtime Error

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

for i in range(N):
        li.append(int(input()))

li.sort()

co = K - 1
hmin = 1000000
for i in range(N):
        if hmin > li[i+co] - li[i]:
                hmin = li[i+co] - li[i]
                if hmin == 0:
                        break
print(hmin)

Submission status: Runtime Error

Solution code:
=input().split()
n=[int(i) for i in n]
h=[n[1]]
for i in range(n[0]):
    h[i]=int(input())
h.sort()

q=-1
for i in range(len(h)):
    a=h[i+n[1]-1]-h[i]
    if q==-1 or q>a:
        q=a
    if q==0:
        break
print(q)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
h_li = [int(input()) for i in range(n)]
dp = [[-1]*1000000010]*100010

def dfs(i, choosed):
    if i == n:
        if len(choosed) != k:
            dif = 10000000010
        else:
            dif = max(choosed) - min(choosed)

    elif len(choosed) >= 2 and dp[len(choosed)][max(choosed)-min(choosed)] != -1:
        dif = dp[len(choosed)][max(choosed)-min(choosed)]
            
    elif len(choosed) == k:
        dif = max(choosed) - min(choosed)
    else:
        dif = min(
            dfs(i+1, choosed + [h_li[i]]),
            dfs(i+1, choosed)
        )

    return dif

print(dfs(0, []))

Submission status: Runtime Error

Solution code:
import collections

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

h = sorted(h)
c = collections.Counter(h)
flg = False
for i in c:
    if c[i] >= 3:
        print(0)
        flg = True
        break

if flg == False:
    print(abs(h[n] - h[n-k]))

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
h1, h2 = map(int, input().split())
l = []
l.append(h1)
l.append(h2)
for i in range(N-2):
    h = int(input())
    global l
    if len(l) < K:
        l.append(h)
    elif len(l) >= K:
        if h > min(l):
            l[l.index(min(l))] = h
        if h < max(l):
            l[l.index(max(l))] = h
print(max(l) - min(l))

Submission status: Runtime Error

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

for i in range(n):
    h.append(int(input()))
min = 10 ** 10
#  print(h)
h = sorted(h)
#  print(h)
for i in range(k):
    #  print(h[i:i+k-1])
    div = h[i+k-1] - h[i]
    if div < min:
        min = div
    if min == 0:
        print(min)
        exit()
print(min)

Submission status: Runtime Error

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

if list(np.diff(hs)) == list(np.zeros(n-1, dtype=int)):
    print(0)
elif k == 1:
    print(0)
else:
    sorted_hs = np.sort(hs)
    print(min([min(np.diff(sorted_hs[i::k-1])) for i in range(k)]))

Submission status: Runtime Error

Solution code:
# coding: utf-8
import itertools
import sys

N, K = map(int, input().split())
# lst = [int(input()) for _ in range(N)]
lst = [int(i) for i in sys.stdin]
# lst2 = list(itertools.combinations(lst, K))
# print(lst2)

lst3 = []
# for l in lst2:
for l in itertools.combinations(lst, K):
    print(l)
    diff = max(l)-min(l)
    lst3.append(diff)

print(min(lst3))

Submission status: Runtime Error

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

q=-1
for i in range(len(h)):
    a=h[i+n[1]-1]-h[i]
    if q==-1 or q>a:
        q=a
    if q==0:
        break
print(q)

Submission status: Runtime Error

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

if list(np.diff(hs)) == list(np.zeros(n-1, dtype=int)):
    print(0)
else:
    sorted_hs = np.sort(hs)
    print(min([min(np.diff(sorted_hs[i::k-1])) for i in range(k)]))

Submission status: Runtime Error

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

q=-1
for i in range(len(h)):
    if q==-1 or q>h[i+n[1]-1]-h[i]:
        q=h[i+n[1]-1]-h[i]
    if q==0:
        break
print(q)

Submission status: Runtime Error

Solution code:
import math

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

crr_min = 10^9
crr_min += 1

for i in range(0,math.ceil((n-k+1)/2)):
    tmp1 = h[n-k+i] - h[i]
    tmp2 = h[n-1-i] - h[n-k-i]
    if tmp1<crr_min:
        crr_min=tmp1

    if tmp2<crr_min:
        crr_min=tmp2

print(crr_min)

Submission status: Runtime Error

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

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

h_sorted = sorted(h, reverse=False)
diff_h_sorted = np.diff(h_sorted, n=1, axis=-1)

def min_sum(N, a):
    dp = [0]*(N+1)
    for i in range(N):
        dp[i+i] = min(dp[i], dp[i]+a[i])
    return dp[N]

print(min_sum(K-1,diff_h_sorted))

Submission status: Runtime Error

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

def dfs(i, choosed):
    if i == n:
        if len(choosed) != k:
            dif = 10000000000
        else:
            dif = max(choosed) - min(choosed)
            
    elif len(choosed) == k:
        dif = max(choosed) - min(choosed)
    else:
        dif = min(
            dfs(i+1, choosed + [h_li[i]]),
            dfs(i+1, choosed)
        )

    return dif

print(dfs(0, []))

Submission status: Runtime Error

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

if n == k:
    print(max(hs) - min(hs))
else:
    sorted_hs = np.sort(hs)
    print(min([min(np.diff(sorted_hs[i::k-1])) for i in range(k)]))

Submission status: Runtime Error

Solution code:
a, b = (int(i) for i in input().split()) 

list=[]
 
for i in range(a):
    list.append(int(input()))

new_list = sorted(list)

sabun = None

for i in range(len(new_list)-1):

    if b - i < 1:
        break

#    if i+b-1 > len(new_list):
#        break

    bbb = new_list[i+b-1] - new_list[i]

    if sabun == None:
        sabun = bbb
        continue

    if sabun > bbb:
        sabun = bbb
    
print(sabun)