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: Wrong Answer

Solution code:
n,k = map(int,input().split())
h_list = [int(input()) for _ in range(n)]
h_list.sort(reverse=True)
i = 0
j = k-1
ans = 10**9

while j < n:
    diff = abs(h_list[j]-h_list[i])
    ans = min(ans,diff)
    i  = j
    j += k-1
print(ans)

Submission status: Wrong Answer

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

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


h.sort()
print(h)
smallest = 10**9

for i in range(N-K+1):
    print(h[i+K-1],h[i])
    diff = h[i+K-1] - h[i]
    if diff < smallest:
        smallest = diff

print(smallest)

Submission status: Wrong Answer

Solution code:
l = input().split(" ")
l[0]=int(l[0])
l[1]= int(l[1])
tl = []
for i in range(l[0]):
    tl.append(int(input()))
tl = sorted(tl,reverse=True)
h=0
for i in range(l[0]-l[1]):
    h = min(tl[i]-tl[i+l[1]-1],tl[i+1]-tl[i+l[1]])
print(h)

Submission status: Wrong Answer

Solution code:
a, b = map(int, input().split())
p = list(map(int, [input() for i in range(a)]))
x = 0
m = 100000

for i in range(a):
  for i in range(a-1):
    if p[i] < p[i+1]:
      x = p[i]
      p[i] = p[i+1]
      p[i+1] = x

for i in range(a-b+1):
  if p[i] - p[i+b-1] < m:
    m = p[i] - p[i+b-1]

print(m)

Submission status: Wrong Answer

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=9999
for j in itertools.combinations(h,n[1]):
    if max(j)-min(j)<q:
        q=max(j)-min(j)
print(q)

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split())
S = [int(input()) for _ in range(H)]
sa = 100000
A = sorted(S, reverse=True)

for i in range(len(A)-W+1):
  if sa > A[i]-A[i+W-1]:
    sa = A[i]-A[i+W-1]
  
  if sa == 0:
    break

print(sa)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
h_list = [int(input()) for _ in range(n)]
h_list.sort()
i = 0
j = k-1
ans = 10**9

while j < n:
    diff = abs(h_list[i]-h_list[j])
    ans = min(ans,diff)
    i  = j
    j += k-1
print(ans)

Submission status: Wrong Answer

Solution code:

def solve():
  n,k = map(int,input().split())
  t = []
  hl = []
  mi = 200000000
  for i in range(n):
    hl.append(int(input()))
  hl.sort()
  for i in range(n):
    h = hl[i]
    if i + 1 > k:
       t.pop(0)
    t.append(h)
    if len(t) == k:
      sm = t[k-1] - t[0]
      if mi > sm:
        mi = sm
  print(mi)

if __name__ == '__main__':
  solve()

Submission status: Wrong Answer

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_maxK_diff = Hs[-1] - Hs[-K]
Hs_minK_diff = Hs[(K-1)] - Hs[0]
print(min(Hs_maxK_diff, Hs_minK_diff))

Submission status: Wrong Answer

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


N = [int(input()) for i in range(N)]
N = sorted(N)
Q = []
for i in range(len(N)-n+1):
	max1 = int(*N[i+n-1:i+n])
	min1 = int(*N[i:i+1])
	Q.append(max1 - min1)
print(max(Q))

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
hlist = [int(input()) for _ in range(N)]
hlist.sort(reverse=True)
sa = []
for _ in range(N-K):
    num = hlist[_]-hlist[K-1]
    sa.append(num)
sa.sort()
print(sa[0])

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

a = [int(input()) for _ in range(n)]
a.sort()
b = 0
ans = []
print(a)
for i in range(n - k + 1):
    b = a[i + k - 1] - a[i]
    ans.append(b)
ans.sort()
print(ans[0])

Submission status: Wrong Answer

Solution code:
n_k = input()
n_k_list =n_k.split(' ')

N = int(n_k_list[0])
K = int(n_k_list[1])

h_list = [0] * N
for i in range(N):
    h_list[i] = int(input())

sorted_h_list = sorted(h_list)
print(sorted_h_list)

# print(h_list)
min_dif = 1000000000
for i in range(N-K+1):
    start = i
    end = K - 1 + i
    # print('start,end', start, end)
    end_v = sorted_h_list[end]
    start_v = sorted_h_list[start]
    dif = end_v - start_v
    # print('dif', dif)
    # print('min_dif', min_dif)
    if min_dif > dif:
        min_dif = dif

print(int(min_dif))

Submission status: Wrong Answer

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

hs.sort()
print(hs)

min_diff = hs[k - 1] - hs[0]
for i in range(n - k + 1):
    diff = hs[i + k - 1] - hs[i]
    if diff == 0:
        min_diff = 0
        break

    if diff < min_diff:
        min_diff = diff

print(min_diff)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
n,k=map(int,input().split())
h=sorted([int(input()) for i in range(n)])
mn=999999999999999
for i in range(n-k):
    mn=min(mn,h[i+k-1]-h[i])
print(mn)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
h = []
for i in range(n):
    h.append(int(input()))
h.sort()
d = [h[i+1]-h[i] for i in range(0, len(h)-1)]
S = 0
for i in range(k-1):
    S += d[i]
ans = S
for i in range(0, n-k):
    SS = S + d[i+k-1] - d[i]
    if SS <= S:
        ans = SS
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
#coding:utf-8
s=[int(i) for i in input().split()]
n=[]
for i in range(s[0]):
  n.append(int(input()))
n.sort()
sa=n[s[0]-1]-n[s[0]-s[1]]
for i in range(s[0]-2,s[0]-s[1]-1,-1):
  if(sa>n[i]-n[i-s[1]+1]):
    sa=n[i]-n[i-s[1]+1]
print(sa)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
h = []
for i in range(n):
    h.append(int(input()))
h.sort()
d = [h[i+1]-h[i] for i in range(0, len(h)-1)]
S = 0
for i in range(k-1):
    S += d[i]
ans = 1000000000
for i in range(0, n-k):
    SS = S + d[i+k-1] - d[i]
    if SS <= S:
        ans = SS
print(ans)

Submission status: Wrong Answer

Solution code:
h,k = [int(x) for x in input().split()]
a = [int(input()) for _ in range(h)]
a.sort()
b = a[:k]
print(max(b) - min(b))

Submission status: Wrong Answer

Solution code:
import numpy as np

N, K = map(int, input().split())
A = []
for i in range(N):
    N = int(input())
    A.append(N)
    
def getNearestValue(A, num):
    idx = np.abs(np.asarray(A) - num).argmin()
    return A[idx]
    
avr = sum(A) / len(A)
L = []
for i in range(K):
    near = getNearestValue(A, N)
    L.append(near)
    A.remove(near)
    
print(max(L) - min(L))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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
a,b,c = 0,trees[0], trees[1]
for i in range(N-K+1) :
	a,b,c = b,c,trees[i+K-1]

	max_v = c
	min_v = a
	diff = max_v - min_v
	if (min_diff > diff) :
		min_diff = diff
	if (min_diff == 0) :
		break



print(min_diff)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N,K=list(map(int,input().split()))
a=[]
for i in range(N):
  a.append(int(input()))
a.sort(reverse=True)
print(a[0]-a[K-1])

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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(n-1, x-1, -1):
	if lst[i]-lst[i-x+1] <= minn:
		minn = lst[i] - lst[i-x+1]
	if minn == 0:
		break
print(minn)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat Dec  8 20:51:13 2018

@author: Owner
"""

import collections
import scipy.misc
import sys
import numpy as np
import math
from operator import itemgetter
import itertools

def prime_decomposition(n):
  i = 2
  table = []
  while i * i <= n:
    while n % i == 0:
      n /= i
      table.append(int(i))
    i += 1
  if n > 1:
    table.append(int(n))
  return table   

def digit(i):
    if i > 0:
        return digit(i//10) + [i%10]
    else:
        return []

"""
N, X = map(int, input().split())

x = [0]*N
x[:] = map(int, input().split())

P = [0]*M
Y = [0]*M
for m in range(M):
    P[m], Y[m] = map(int, input().split())
    
all(nstr.count(c) for c in '753')
"""
N, K = map(int, input().split())


h = [0]*N
for n in range(N):
    h[n] = int(input())

diff_min = 10^(9)
for l in list(itertools.combinations(h,K)):
    temp_diff = l[np.argmax(l)] - l[np.argmin(l)]
    if diff_min > temp_diff:
        diff_min = temp_diff
    if diff_min == 0:
        break
print(diff_min)

Submission status: Wrong Answer

Solution code:
n,k = (int(i) for i in input().split())
l = list([])
for i in range(n):
    h = int(input())
    l.append(h)
l2 = sorted(l, reverse=True)

d = k-1
d_min = 1000000000
for i in range(n-d):
    tmp = abs(l2[i] - l2[i+d])
    if (d_min > tmp ):
        d_min = tmp
print(tmp)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n_k = input()
n_k_list =n_k.split(' ')

N = int(n_k_list[0])
K = int(n_k_list[1])

h_list = [0] * N
for i in range(N):
    h_list[i] = int(input())

h_list = sorted(h_list)

# print(h_list)
min_dif = 100000000
for i in range(N-K+1):
    start = i
    end = K - 1 + i
    # print('start,end', start, end)
    end_v = h_list[end]
    start_v = h_list[start]
    dif = end_v - start_v
    # print('dif', dif)
    # print('min_dif', min_dif)
    if min_dif > dif:
        min_dif = dif

print(int(min_dif))

Submission status: Wrong Answer

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

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

h_list.sort()

r1 = h_list[k-1] - h_list[0]

h_list.reverse()
r2 = h_list[0] - h_list[k-1]

if r1 <= r2:
    print(r1)
else:
    print(r2)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

D = [(H[i+K-1] - H[i]) for i in range(N-K)]
print(min(D))

Submission status: Wrong Answer

Solution code:
def inpl(): return [int(i) for i in input().split()]
N, K = inpl()
h = [0] + sorted([int(input()) for i in range(N)]) 
ans = float('inf')
for i in range(N-K+2):
    ans = min(ans, h[i+K-1]-h[i])
print(ans)

Submission status: Wrong Answer

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

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


h.sort()
smallest = 10**5

for i in range(N-K+1):
    #print(h[i+K-1],h[i])
    diff = h[i+K-1] - h[i]
    if diff < smallest:
        smallest = diff

print(smallest)

Submission status: Wrong Answer

Solution code:
m = input()
n = int(m.split()[0])
k = int(m.split()[1])
t = []
for i in range(n):
    t.append(int(input()))
t.sort()
a = None
for i in range(len(t)-k+1):
    w = abs(-1*t[i] + t[i-k])
    if i == 0:
        a = w
    elif a > w:
        a = w
print(a)

Submission status: Wrong Answer

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(n-1, x, -1):
	if lst[i]-lst[i-x+1] <= minn:
		minn = lst[i] - lst[i-x+1]
	if minn == 0:
		break
print(minn)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
l=[]
for i in range(n):
  l.append(int(input()))
l.sort()
m=l[2]-l[0]
for i in range(1,n-2):
  a=l[i+2]-l[i]
  if a<m:
    m=a
print(m)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
m = input()
n = int(m.split()[0])
k = int(m.split()[1])
t = []
for i in range(n):
    t.append(int(input()))
t.sort()
a = None
for i in range(len(t)-k+1):
    w = abs(-1*t[i] + t[i-3])
    if i == 0:
        a = w
    elif a > w:
        a = w
print(a)

Submission status: Wrong Answer

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(n-1, x, -1):
	if lst[i]-lst[i-x] <= minn:
		minn = lst[i] - lst[i-x]
	if minn == 0:
		break
print(minn)

Submission status: Wrong Answer

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

def sort(l):
	m = sum(l)/len(l)
	a = [x for x in l if x < m]
	b = [x for x in l if m <= x]
	if a == []:
		return b
	if b == []:
		return a
	return sort(a)+sort(b)

sh = sort(h)
ans = max(h)
for i in range(0,N-2):
	s = (sh[i+2] - sh[i])
	if s < ans:
		ans = s
print(ans)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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 = 999999
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: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

h.sort()
ans = 1000000
for i in range(N-K+1):
    d = abs(h[i]-h[i+K-1])
    if d < ans:
        ans = d

print(ans)

Submission status: Wrong Answer

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

h.sort()

ans = float("inf")

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

print(ans)

Submission status: Wrong Answer

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

print(min(res1,res2))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

Hs = sorted(hs)
min = 10000000

for i in range(N - K + 1):
    diff = Hs[i + K -1] - Hs[i]
    if diff < min:
        min = diff

print(min)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

H = sorted(H)

tmp = 0
ans = H[-1]
for i in range(N-K+1):
    tmp = H[i+K-1] - H[i]
#    print(H[i:i+K])

    ans = min(ans, tmp)

print(ans)

Submission status: Wrong Answer

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(n-1, 0, -1):
	if lst[i]-lst[i-1] < minn:
		minn = lst[i] - lst[i-1]
	if minn == 0:
		break
print(minn)

Submission status: Wrong Answer

Solution code:
m = input()
n = int(m.split()[0])
k = int(m.split()[1])
t = []
for i in range(n):
    t.append(int(input()))
t.sort()
a = None
for i in range(len(t)-k+1):
    w = -t[i] + t[i-3]
    if i == 0:
        a = w
    elif a > w:
        a = w
print(a)

Submission status: Wrong Answer

Solution code:
import itertools
n,k=map(int,input().split())
H=[int(input()) for i in range(n)]
dif=10**9

for i in itertools.combinations(H,3):
  dif=min(dif,abs(max(i)-min(i)))
print(dif)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
n,k=map(int,input().split())
h=sorted([int(input()) for i in range(n)])
mn=99999999
for i in range(n-k+1):
    mn=min(mn,h[i+k-1]-h[i])
print(mn)

Submission status: Time Limit Exceeded

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

H = [0]*(N-1)
for i in range(N-1):
    H[i] = h[i+1] - h[i]

ans = float("inf")
for j in range(N-(K-1)):
    ref = sum(H[j:j+K-1])
    ans = min(ans, ref)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
def mapt(fn, *args):
    return tuple(map(fn, *args))


def Input():
    return mapt(int, input().split(" "))


def main():
    n, k = Input()
    data = sorted(int(input()) for _ in range(n))
    ans = 1e9
    for i in range(n-k+1):
        x = data[i:i+k]
        ans = min(ans, max(x) - min(x))
    print(ans)
main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

import itertools
result = 10**9
h_sorted = sorted(h)
for i in range(n-k+1):
  k_arr = h_sorted[i:i+k]
  k_arr = list(itertools.chain.from_iterable(k_arr))
  result = min(result, k_arr[k-1]-k_arr[0])

print(result)

Submission status: Time Limit Exceeded

Solution code:
import itertools

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

hmin =[]

c = itertools.combinations(h,K)

for v in c:
    hmin.append(max(v)-min(v))
print(min(hmin))

Submission status: Time Limit Exceeded

Solution code:
import itertools

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

hmin = 10**18

c = itertools.combinations(h,K)

for v in c:
    hmin = min(max(v)-min(v),hmin)
print(hmin)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
import itertools
n,k = map(int, input().split())
h = [int(input()) for i in range(n)]
num = 10**9
for j in itertools.combinations(h,k):
    t = max(j)-min(j)
    if t < num:
        num = t
print(num)

Submission status: Time Limit Exceeded

Solution code:
import itertools
n,k = map(int, input().split())
h = [int(input()) for i in range(n)]
l = []
for j in itertools.combinations(h,k):
    l.append(max(j)-min(j))
print(min(l))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import sys


inint = lambda: int(sys.stdin.readline())
inintm = lambda: map(int, sys.stdin.readline().split())
inintl = lambda: list(inintm())
instrm = lambda: map(str, sys.stdin.readline().split())
instrl = lambda: list(instrm())

n, k = inintm()

h = []
ans = 10**10

for _ in range(n):
    h.append(inint())

h.sort()

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n,m=[int(i) for i in input().split()]
l=[]
for i in range(n):
    a=int(input())
    l.append(a)
l=sorted(l)
b=l[0:m]
maxs=max(b)-min(b)
for i in range(0,(n-m)):
    b.pop(0)
    b.append(l[i+m])
    c=(max(b)-min(b))
    if(c<maxs):
        maxs=c
print(maxs)

Submission status: Time Limit Exceeded

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

ans = 10**9
l.sort()
for i in range(n-k+1):
    tmp = max(l[i:k+i]) - min(l[i:k+i])
    if tmp < ans:
        ans = tmp

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations

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

ans=10**9
for i in combinations(H, K):
    ans=min(ans,max(i)-min(i))
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
h = [int(input()) for i in range(n)]
h.sort()
ans = 10 ** 10
for i in range(n-k+1):
  ans = min(ans, h[i:i+k][-1] - h[i:i+k][0])
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()
ans = 10 ** 10
for i in range(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:
MAX_H = 1_000_000_000
N, K = list(map(int, input().split()))
H = [int(input()) for _ in range(N)]
H.sort()
res = MAX_H
for i in range(N - K + 1):
    _h = min(H[i:i+K])
    h = max(H[i:i+K])
    res = min(res, h - _h)
    if res == 0:
        break
print(res)

Submission status: Time Limit Exceeded

Solution code:
MAX_H = 1_000_000_000
N, K = list(map(int, input().split()))
H = [int(input()) for _ in range(N)]
H.sort()
res = MAX_H
for i in range(N - K + 1):
    _h = MAX_H
    h = 0
    for j in range(i, i + K):
        _h = min(_h, H[j])
        h = max(h, H[j])
    res = min(res, h - _h)
    if res == 0:
        break
print(res)

Submission status: Time Limit Exceeded

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

l = [0]*(n-1)
for i in range(n-1): l[i] = h[i+1]-h[i]

s = 0
t = float("inf")
for i in range(n-k+1):
  p = sum(l[i:i+k-1])
  if p < t:
    t = p
    s = i
    
print(h[s+k-1] - h[s])

Submission status: Time Limit Exceeded

Solution code:

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

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

Submission status: Time Limit Exceeded

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

print(min(t))

Submission status: Time Limit Exceeded

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

ans=10**10
for i in range(n-k+1):
    p=sum(s[i:i+k-1])
    ans=min(ans,p)

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()
ans=float("inf")
for i in range(len(h)-k+1):
	ma = max(h[i:i+k])
	mi = min(h[i:i+k])
	ans = min(ans,ma-mi)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
def resolve():
    N, K = list(map(int, input().split()))
    h = []
    for _ in range(N):
        h.append(int(input()))
    ans = 10 ** 18
    h.sort()

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


resolve()

Submission status: Time Limit Exceeded

Solution code:
def resolve():
    N, K = list(map(int, input().split()))
    h = [int(input()) for _ in range(N)]
    ans = 10 ** 18
    h.sort()

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


resolve()

Submission status: Time Limit Exceeded

Solution code:
def resolve():
    N, K = list(map(int, input().split()))
    h = [int(input()) for _ in range(N)]
    ans = 10 ** 18
    h.sort()

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


resolve()

Submission status: Time Limit Exceeded

Solution code:
def resolve():
    N, K = list(map(int, input().split()))
    h = [int(input()) for _ in range(N)]
    ans = 10 ** 18
    h.sort()

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

resolve()

Submission status: Time Limit Exceeded

Solution code:
import sys
import math
import itertools as it
def I():return int(sys.stdin.readline().replace("\n",""))
def I2():return map(int,sys.stdin.readline().replace("\n","").split())
def S():return str(sys.stdin.readline().replace("\n",""))
def L():return list(sys.stdin.readline().replace("\n",""))
def Intl():return [int(k) for k in sys.stdin.readline().replace("\n","").split()]
def Lx(k):return list(map(lambda x:int(x)*-k,sys.stdin.readline().replace("\n","").split()))

if __name__ == "__main__":
    n ,k = I2()
    h = [I() for _ in range(n)]
    h.sort()
    ans = float("inf")
    for i in range(n-k+1):
        l = h[i:i+k]
        #print(l)
        ans = min(ans,l[-1]-l[0])
    print(ans)

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

Submission status: Time Limit Exceeded

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

h.sort()
hh = [0]*(n-1)
for i in range(n-1):
  hh[i] = h[i+1] - h[i]

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
def main():
    N, K = map(int, input().split())
    h = [int(input()) for i in range(N)]
    H = sorted(h)
    ans = 10**9 +1
    for i in range(N-K+1):
        high = max(H[i:i+K]) - min(H[i:i+K])
        ans = min(ans, high)
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

tmp = 10000000000

for i in range(n-k+1):
    ma = max(lis[i:i+k])
    mi = min(lis[i:i+k])
    
    tmp = min(tmp, ma-mi)
    
print(tmp)

Submission status: Time Limit Exceeded

Solution code:
import itertools

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

ans = float("inf")

for s in itertools.permutations(H, K):
  s = list(s)
  s.sort()
  ans = min(ans, s[K-1] - s[0])
  
print(ans)

Submission status: Time Limit Exceeded

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

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

print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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):
    hs = h[i:i+k]
    ans = min(ans,(hs[-1] - hs[0]))
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()
ans = float("inf")
for i in range(len(H)-K+1):
	ans = min(ans, max(H[i:i+K])- min(H[i:i+K]))

print(ans)

Submission status: Time Limit Exceeded

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

h = list(sorted([int(input()) for i in range(N)]))
ans = float('inf')

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

print(ans)