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: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N, K = map(int,input().split())
h = [int(input()) for i in range(N)]
h.sort()
for i in list(set(h)):
  if(h.count(i) >= K):
    print('0')
    exit()
trees = h[:K]
h_max = max(trees)
h_min = min(trees)
print(h_max - h_min)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n,k = input().split(' ')
n = int(n)
k = int(k)
h = []
for i in range(n):
    h.append(int(input()))
h.sort()
mindiff = 1000000000
for i in range(n-k+1):
    t = h[i:i+k]
    diff = max(t) - min(t)
    if diff < mindiff:
        mindiff = diff
print(mindiff)

Submission status: Time Limit Exceeded

Solution code:
N,K = map(int,input().split())
h = list(int(input()) for i in range(N))
h.sort(reverse=True)
cnt = 0
ans = []
dif = float('inf') 
for i in range(N):
    if K-1<=i:
        if dif>ans[0]-h[i]:
            dif = ans[0]-h[i]
        del ans[0]
    ans.append(h[i])
print(dif)

Submission status: Time Limit Exceeded

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

for i in range(n-k+1):
  list1 = [h[j] for j in range(i,i+k)]
#  print(list1)
  if max(list1)-min(list1) < ans:
      ans = max(list1)-min(list1)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N,K = list(map(int,input().strip().split()))
ans = int(10e10)

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

hlist.sort()

for i in range(N-K+1):
    low = min(hlist[i:i+K])
    high = max(hlist[i:i+K])
    ans = min(ans,(high-low))

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N,K = list(map(int,input().strip().split()))
hlist = []
ans = int(10e10)

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

hlist.sort()

for i in range(N-K+1):
    low = min(hlist[i:i+K])
    high = max(hlist[i:i+K])
    ans = min(ans,(high-low))

print(ans)

Submission status: Time Limit Exceeded

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

tree.sort()
m = float("inf")
for i in range(N-K+1):
    d = tree[i : i + K]
    dif = max(d) - min(d)
    m = dif if m > dif else m

print(int(m))

Submission status: Time Limit Exceeded

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

tree.sort()
m = float("inf")
for i in range(N-K+1):
    d = tree[i:i+K]
    dif = max(d) - min(d)
    m = dif if m > dif else m

print(int(m))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
h = {}
for i in range(n):
    x = int(input())
    if x in h.keys():
        h[x] += 1
    else:
        h[x] = 1

res = max(h.keys()) - min(h.keys())
ih = sorted(h.keys())

for i in range(len(h)):
    hi = ih[i]
    cnum = h[hi]
    ip = i+1
    if cnum >= k:
        d = 0
        res = 0
        break
    else:
        d = max(h.keys()) - min(h.keys())
    while cnum < k and ip < len(h):
        hi2 = ih[ip]
        cnum += h[hi]
        ip += 1
        if cnum >= k:
            d = abs(hi - hi2)
    res = min(res, d)

print(res)

Submission status: Time Limit Exceeded

Solution code:
n,k = map(int,input().split())
h=sorted(list(int(input()) for i in range(n)))
h+=h
mi=float('inf')
ansma=0
ansmi=0
for i in range(n-2):
    tmp=h[i:i+k]
    tmpma=max(tmp)
    tmpmi=min(tmp)
    if i == n:
        break
    if mi > tmpma - tmpmi:
        mi=tmpma-tmpmi
        ansma=tmpma
        ansmi=tmpmi
 
print(ansma-ansmi)

Submission status: Time Limit Exceeded

Solution code:
N,K = input().split(" ")
N = int(N)
K = int(K)
min = 1000000000
trees = []

for i in range(N):
  trees.append(int(input()))
  trees = sorted(trees)
  
list2 = [y-trees[x-K+1] for x,y in enumerate(trees) if x >= K-1]
print(sorted(list2)[0])

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
h = []
ans = 1000000001
for i in range(n):
    h.append(int(input()))
h.sort()
for j in range(n-(k-1)):
    x = []
    if ans == 0:
        break
    for l in range(j,j+k):
        x.append(h[l])
        if ans == 0:
            break
        elif len(x) == k:
            y = x[-1]-x[0]
            if ans == 0:
                break
            elif ans > y:
                ans = y
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
h = []
ans = 1000000001
for i in range(n):
    h.append(int(input()))
h.sort()
for j in range(n-(k-1)):
    x = []
    for l in range(j,j+k):
        x.append(h[l])
        if ans == 0:
            break
        elif len(x) == k:
            y = x[-1]-x[0]
            if ans == 0:
                break
            elif ans > y:
                ans = y
print(ans)

Submission status: Time Limit Exceeded

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

aa = h_sort[1:]
bb = h_sort[:-1]
diff = [a - b for a, b in zip(aa, bb)]

ans = []
for i in range(len(diff) - (K - 2)):
    tmp = 0
    for j in range(K - 1):
        tmp += diff[i + j]
    ans.append(tmp)
 
print(min(ans))

Submission status: Time Limit Exceeded

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

from collections import defaultdict, deque

def solve():
    N, K = map(int, input().split())
    H = defaultdict(int)
    for _ in range(N):
        H[int(input())] += 1
    if K in H.values():
        res = 0
    else:
        H = sorted(list(H.items()))
        res = float('inf')
        for i in range(len(H)):
            h = H[i][0]
            k = H[i][1]
            while k < K:
                i += 1
                if i < len(H):
                    k += H[i][1]
                else:
                     break
            else:
                h = H[i][0] - h
                res = min(res, h)
    return str(res)

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

Submission status: Time Limit Exceeded

Solution code:
N_K = input().split()
N = int(N_K[0])
K = int(N_K[1])
Hs = sorted([int(input()) for i in range(N)])
Hs_diff = [Hs[i+1] - Hs[i] for i in range(N) if i<(N-1)]
Hs_diff_K = [sum(Hs_diff[(i):(i+K-1)]) for i in range(len(Hs_diff)) if i < len(Hs_diff)-K+2]
print(min(Hs_diff_K))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
n,k=[int(i) for i in input().split()]
h=[int(input()) for i in range(n)]
h.sort()
sub=[]

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

Submission status: Time Limit Exceeded

Solution code:
N,K = input().split(" ")
N = int(N)
K = int(K)
min = 1000000000
trees = []

for i in range(N):
  trees.append(int(input()))
  trees = sorted(trees)
  
for x,y in enumerate(trees):
  if x +K <= len(trees):
    min2 = trees[x+K-1]-y
    if min > min2:
      min = min2
  else:
    break
print(min)

Submission status: Time Limit Exceeded

Solution code:
import itertools,math
n=input().split()
n=[int(i) for i in n]
h=[]
for i in range(n[0]):
    h.append(int(input()))
q=-1
 
for j in itertools.combinations(h,n[1]):
    if q>max(j)-min(j) or q==-1:
        q=max(j)-min(j)
    if q==0:
        break
 
print(q)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
n,k = map(int,input().split())
h=sorted(list(int(input()) for i in range(n)))
h+=h
mi=float('inf')
ansma=0
ansmi=0
for i in range(int(len(h)/2)):
    tmp=h[i:i+k]
    tmpma=max(tmp)
    tmpmi=min(tmp)
    if mi > tmpma - tmpmi:
        mi=tmpma-tmpmi
        ansma=tmpma
        ansmi=tmpmi

print(ansma-ansmi)

Submission status: Time Limit Exceeded

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

h = sorted(h)

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

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
n, k = map(int, input().split())
h = np.array([int(input()) for _ in range(n)],dtype=int)
h.sort()
print(min([np.subtract(np.max(h[x:x+k]),np.min(h[x:x+k])) for x in range(n-k+1)]))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
n, k = map(int, input().split())
h = np.array([int(input()) for _ in range(n)],dtype=int)
h.sort()
print(min([np.max(h[x:x+k]) - np.min(h[x:x+k]) for x in range(n-k+1)]))

Submission status: Time Limit Exceeded

Solution code:
import itertools,math
n=input().split()
n=[int(i) for i in n]
h=[]
for i in range(n[0]):
    h.append(int(input()))
q=-1

for j in itertools.combinations(h,n[1]):
    if q>max(j)-min(j) or q==-1:
        q=max(j)-min(j)

print(q)

Submission status: Time Limit Exceeded

Solution code:
n, k = input().strip().split(' ')
n, k = [int(n),int(k)]
l = []
for i in range(n):
    h = input()
    h = int(h)
    l.append(h)
dl = sorted(l)
ans = float('inf')
for j in range(n-k+1):
    ddl = []
    for a in range(k):
        ddl.append(dl[j+a])
    if max(ddl) - min(ddl) <= ans:
        ans = max(ddl) - min(ddl)
print(int(ans))

Submission status: Time Limit Exceeded

Solution code:
n,k = map(int,input().split())
h=sorted(list(int(input()) for i in range(n)))
h+=h
mi=float('inf')
a=0
for i in range(int(len(h)/2)):
    tmp=h[i:i+k]
    if mi > max(tmp)-min(tmp):
        mi=max(tmp)-min(tmp)
        a=i

print(max(h[a:a+k])-min(h[a:a+k]))

Submission status: Time Limit Exceeded

Solution code:
import itertools,math
n=input().split()
n=[int(i) for i in n]
h=[]
for i in range(n[0]):
    h.append(int(input()))
q=[]

for j in itertools.combinations(h,n[1]):
    q.append(max(j)-min(j))

print(min(q))

Submission status: Time Limit Exceeded

Solution code:
NK = list(map(int, input().split()))
a = [0]*NK[0]
for i in range(len(a)):
    a[i] = int(input())
a.sort()
N = NK[0]
K = NK[1]

b = [0]*K
offsets = [0]*(N-K+1)
for i in range(len(offsets)):
    for j in range(K):
        b[j] = a[i+j]
    offsets[i] = b[K-1] - b[0]

print(min(offsets))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

kumi = list(itertools.combinations(h, K))
for j in range(len(kumi)):
    dh.append(max(kumi[j])-min(kumi[j]))
print(min(dh))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
n, k = map(int, input().split())
h = np.array(sorted([int(input()) for _ in range(n)]))
print(min([max(h[x:x+k]) - min(h[x:x+k]) for x in range(n-k+1)]))

Submission status: Time Limit Exceeded

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

arr = []
for i in range(n):
	arr += [int(input())]
	
arr2 = sorted(arr)

ans = float('inf')

for y in range(n-k+1):
	mx = max(arr2[y:y+k])
	mn = min(arr2[y:y+k])
	ans = min(ans,mx-mn)

print(ans)

Submission status: Time Limit Exceeded

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

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

h.sort()

iter = len(h) - K

c_hmin = 99999999999999999999999999999999

for i in range(iter+1):
    cell = h[i: K + i]
    hmin = cell[-1] - cell[0]
    if c_hmin > hmin:
        c_hmin = hmin

print(c_hmin)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import itertools
N,K = map(int,input().split())
h = [input() for i in range(N)]
def f(x):
    return int(max(x)) - int(min(x))
l = list(map(f, itertools.combinations(h, 3)))
print(min(l))

Submission status: Time Limit Exceeded

Solution code:
import collections


N, K = map(int, input().split())
trees = [int(input()) for i in range(N)]
tree_min = min(trees)
tree_max = max(trees)
hights = collections.Counter(trees)

diffs = set()
for i in range(tree_min, tree_max + 1):
    num = 0
    diff = -1
    for h in range(i, tree_max + 1):
        diff += 1
        num += hights.get(h, 0)
        if num >= K:
            diffs.add(diff)
            break
    if diff == 0:
        break

print(min(diffs))

Submission status: Time Limit Exceeded

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)
ans = 10**15

for i in range(len(diff_h_sorted) - K + 2):
    ans = min(ans, sum(diff_h_sorted[i:i+K-1]))

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N,K=list(map(int,input().split()))
k=[]
for i in range(N):
    k.append(int(input()))
S=list(set(k))
temp=float('inf')
for s in S:
    a=[]
    for i in range(N):
        a.append([abs(s-k[i]),i])
    a.sort()
    b=[]
    for i in range(K):
        b.append(k[a[i][1]])
    m=max(b)-min(b)
    if(m<temp):
        temp=m
print(temp)

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations
import numpy as np
n, k = map(int, input().split())
hs = []
for i in range(n):
    hs.append(int(input()))
def get_diff(x):
    return max(x) - min(x)
print(min(np.apply_along_axis(get_diff, 1, list((combinations(hs, k))))))

Submission status: Time Limit Exceeded

Solution code:
import itertools

total, select = map(int, input().split(' '))

trees = []

for _ in range(total):
  trees.append(int(input()))

result = 10000000000
for t in list(itertools.combinations(trees, select)):
  result = min(result, max(t)-min(t))

print(result)

Submission status: Time Limit Exceeded

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

arr = []
for i in range(n):
	arr += [int(input())]
	
arr2 = sorted(arr)

ans = float('inf')

for i in range(n-k+1):
	mx = max(arr2[i:i+k])
	mn = min(arr2[i:i+k])
	ans = min(ans,mx-mn)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations
N, K = [int(i) for i in input().split()]
List = [int(input()) for _ in range(N)]
print(min([max(c) - min(c) for c in combinations(List, K)]))

Submission status: Time Limit Exceeded

Solution code:
NK = list(map(int, input().split()))
a = [0]*NK[0]
for i in range(len(a)):
    a[i] = int(input())
a.sort()

b = [0]*NK[1]
offsets = [100]*(NK[0]-NK[1]+1)
for i in range(len(offsets)):
    for j in range(NK[1]):
        b[j] = a[i+j]
    b.sort()
    offsets[i] = b[len(b)-1] - b[0]

print(min(offsets))

Submission status: Time Limit Exceeded

Solution code:
import sys, copy, math, collections, itertools

n, k = (int(i) for i in input().split())
hs = [int(input()) for i in range(n)]
hs.sort()
i=0
m=-1
l=len(hs)
while True:
    if l < (i+k):
        break
    tmp = hs[i:i+k]
    cnd=tmp[-1] - tmp[0]
    if m==-1:
        m=cnd
    else:
        if cnd < m:
            m=cnd
    i+=1
print(int(m))

Submission status: Time Limit Exceeded

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

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

h_sort = sorted(h)

h_sort_diff = []

for i in range(len(h_sort) - 1):
    tmp = h_sort[i + 1] - h_sort[i]
    h_sort_diff.append(tmp)

ans = []
for i in range(len(h_sort_diff) - (K - 2)):
    tmp = 0
    for j in range(K - 1):
        tmp += h_sort_diff[i + j]
    ans.append(tmp)

print(min(ans))

Submission status: Time Limit Exceeded

Solution code:
#!/env/usr/python3
n, k = input().split()
n = int(n)
k = int(k)

a = []
for i in range(n):
	a.append(int(input()))
	#print(a)

a.sort()
#print(a)


height_distance = []
for i, h in enumerate(a):
	if i <= n-k:
		chosen = a[i:i+k]
		height_distance.append(max(chosen)-min(chosen))
		#print(height_distance)

print(min(height_distance))

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
h = []
ans = 1000000001
for i in range(n):
    h.append(int(input()))
h.sort()
for j in range(n-(k-1)):
    x = []
    for l in range(j,j+k):
        x.append(h[l])
        if len(x) == k:
            y = x[-1]-x[0]
            if ans > y:
                ans = y
print(ans)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
# Christmas Eve

N = list(map(int, (input().strip().split(' '))))
K = N[1]
N = N[0]
i = 0
h = []
while i < N:
  hi = int(input().strip())
  h.append(hi)
  i += 1
h = sorted(h)

i = 0
min_i = i
min_diff = 10000000000
while i < N - K + 1:
  sub = h[i:i+K]
  diff = sub[K-1] - sub[0]
  if min_diff > diff:
    min_diff = diff
    min_i = i
  i += 1
print(min_diff)

"""
med = sum(h)/len(h)
print(list(map(lambda x:x-med, h)))
"""

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations
N, K = [int(i) for i in input().split()]
List = [int(input()) for _ in range(N)]
min0 = 1000000000
for c in combinations(List, K):
    mint = max(c) - min(c)
    if min0 > mint:
        min0 = mint
print(min0)

Submission status: Time Limit Exceeded

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

n, k = int(n), int(k)

h = [i for i in range(n)]

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

h.sort()

mm = []

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

print(min(mm))

Submission status: Time Limit Exceeded

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

diffs = set()
for i in range(N - K + 1):
    diff = sum([trees[i + j + 1] - trees[i + j] for j in range(K - 1)])
    diffs.add(diff)
    if diff == 0:
        break

print(min(diffs))

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations
import numpy as np
n, k = map(int, input().split())
hs = []
for i in range(n):
    hs.append(int(input()))
def get_diff(x):
    return max(x) - min(x)
print(min(np.array(list(map(get_diff, list((combinations(hs, k))))))))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

kumiawase = list(itertools.combinations(H,K))
ans = 100000000000000000000000000000
for ks in kumiawase:
	max_h = max(ks)
	min_h = min(ks)

	kouho = max_h - min_h
	if(ans > kouho):
		ans = int(kouho)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
import itertools

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

lst3 = []
for l in lst2:
    diff = max(l)-min(l)
    lst3.append(diff)

print(min(lst3))

Submission status: Time Limit Exceeded

Solution code:
n, k = tuple(map(int, input().split()))
arr = [0 for i in range(n)]
sums = 0
for i in range(n):
    inp = int(input())
    arr[i] = inp

arr.sort()

sa = 1000000000
for i in range(n-k+1):
    newsa = max(arr[i:i+k]) - min(arr[i:i+k])
    if newsa < sa:
        sa = newsa

print(sa)

Submission status: Time Limit Exceeded

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

H.sort()

#res1 = max(H[:K])-min(H[:K])
#res2 = max(H[N-K:])-min(H[N-K:])

res = 1000000000
for i in range(N-K+1):
    tmp = max(H[i:i+K])-min(H[i:i+K])
    if tmp < res:
        res = tmp
print(res)

Submission status: Time Limit Exceeded

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

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

trees.sort()
min_diff = 99999999999
for i in range(N-K+1) :
	arr = trees[i:i+K]
	max_v = max(arr)
	min_v = min(arr)
	diff = max_v - min_v
	if (min_diff > diff) :
		min_diff = diff
	if (min_diff == 0) :
		break


print(min_diff)

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations
import numpy as np
n, k = map(int, input().split())
hs = []
for i in range(n):
    hs.append(int(input()))
print(min(np.array(list(map(max, list((combinations(hs, k)))))) - np.array(list(map(min, list((combinations(hs, k))))))))

Submission status: Time Limit Exceeded

Solution code:
N,K = map(int,input().split(" "))
height = []
for i in range(N):
    height.append(int(input()))
height.sort()
min = 10**9
n = N-K+1
for j in range(n):
    light = height[j:j+K]
    hmin = light[0]
    hmax = light[K-1]
    hei = hmax-hmin
    if hei <= min:
        min = hei
print(min)

Submission status: Time Limit Exceeded

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

kumiawase = list(itertools.combinations(H,K))
Kouhos = []
for ks in kumiawase:
	max_h = max(ks)
	min_h = min(ks)

	kouho = max_h - min_h
	Kouhos.append(kouho)

print(min(Kouhos))

Submission status: Time Limit Exceeded

Solution code:
import itertools

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

m = -1
for l in itertools.combinations(h,K):
    d = max(l) - min(l)
    if d < m or m == -1:
        m = d
print(m)

Submission status: Time Limit Exceeded

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

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

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: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

hs = list(sorted(hs))
minh=-1

for i in range(N-K+1):
  hsb = hs[i:i+K]
  if minh==-1 or minh>hsb[-1]-hsb[0]:
    minh = hsb[-1]-hsb[0]

print(minh)

Submission status: Time Limit Exceeded

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

hs = list(sorted(hs))
minh=-1

for i in range(N-K+1):
  hsb = hs[i:i+K]
  if minh==-1 or minh>max(hsb)-min(hsb):
    minh = max(hsb)-min(hsb)

print(minh)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import itertools

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

com = list(itertools.combinations(h, k))

ans = max(com[0]) - min(com[0])
for c in com[1:]:
    ans = min(ans, max(c) - min(c))
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Accepted

Solution code:
# C

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

Submission status: Accepted

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

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

H.sort()

ans = 1000000009

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

print(ans)

Submission status: Accepted

Solution code:
#-*-coding:utf-8-*-
import sys
input=sys.stdin.readline
import numpy as np

def main():
    numbers=[]
    n,m = map(int,input().split())
    [numbers.append(int(input())) for _ in range(n)]
    n_numbers=np.array(numbers,dtype=int)
    n_numbers.sort()
    print(min(n_numbers[i+m-1] - n_numbers[i] for i in range(n-m+1)))

if __name__=="__main__":
    main()

Submission status: Accepted

Solution code:
#-*-coding:utf-8-*-
import sys
input=sys.stdin.readline

def ngram(numbers,n):
    return list([numbers[i:i+n] for i in range(len(numbers)-2)])

def main():
    numbers=[]
    n,m = map(int,input().split())
    [numbers.append(int(input())) for _ in range(n)]
    numbers.sort()
    print(min(numbers[i+m-1] - numbers[i] for i in range(n-m+1)))

if __name__=="__main__":
    main()

Submission status: Accepted

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

ans = float("inf")
for i in range(n-k+1):
    sa = h[i+k-1] - h[i]
    if ans > sa:
        ans = sa
        
print(ans)

Submission status: Accepted

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

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())
high = [int(input()) for _ in range(N)]
s_high = sorted(high)
ans = 10**9
#print(s_high)
for i in range(N-K+1):
    j = s_high[i+K-1] - s_high[i]
    ans = min(ans,j)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    N, K = map(int, input().split())
    h = []
    for _ in range(N):
        h.append(int(input()))
    h.sort()
    ans = pow(10, 10)
    for k in range(N-K+1):
        ans = min(ans, abs(h[k]-h[K-1+k]))
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# coding: utf-8


def main():
    N, K = map(int, input().split())
    H = sorted([int(input()) for _ in range(N)])
    ans = min([H[i + K - 1] - H[i] for i in range(N - K + 1)])

    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
import collections
from sys import exit
from itertools import product

ii = lambda : int(input())
mi = lambda : map(int,input().split())
li = lambda : list(map(int,input().split()))

n, k = mi()
h = [ii() for i in range(n)]
h.sort()
ans = 10**12
for i in range(n):
    last = i + k - 1
    if last >= n:
        break
    ans = min(ans, h[last]-h[i])
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

h.sort()

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

print(min(l))

Submission status: Accepted

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

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

print(result)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

H.sort()

for j in range(N-K+1):
  sa.append(H[K+j-1]-H[j])

print(min(sa))

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
L = sorted(list(int(input()) for _ in range(n)))
m = 10**9

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

print(m)