Problem:


Score : 300 points

Problem Statement

In some other world, today is Christmas Eve.

There are N trees planted in Mr. Takaha's garden. The height of the i-th tree (1 \leq i \leq N) is h_i meters.

He decides to choose K trees from these trees and decorate them with electric lights. To make the scenery more beautiful, the heights of the decorated trees should be as close to each other as possible.

More specifically, let the height of the tallest decorated tree be h_{max} meters, and the height of the shortest decorated tree be h_{min} meters. The smaller the value h_{max} - h_{min} is, the better. What is the minimum possible value of h_{max} - h_{min}?

Constraints

2 \leq K < N \leq 10^5

1 \leq h_i \leq 10^9

h_i is an integer.

Input

Input is given from Standard Input in the following format:

N K
h_1
h_2
:
h_N

Output

Print the minimum possible value of h_{max} - h_{min}.

Sample Input 1

5 3
10
15
11
14
12

Sample Output 1

2

If we decorate the first, third and fifth trees, h_{max} = 12, h_{min} = 10 so h_{max} - h_{min} = 2. This is optimal.

Sample Input 2

5 3
5
7
5
7
7

Sample Output 2

0

If we decorate the second, fourth and fifth trees, h_{max} = 7, h_{min} = 7 so h_{max} - h_{min} = 0. This is optimal.

There are not too many trees in these sample inputs, but note that there can be at most one hundred thousand trees (we just can't put a sample with a hundred thousand lines here).



Sample input:
5 3
10
15
11
14
12


Sample output:
2

Submission status: Accepted

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

Submission status: Accepted

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

p.sort()

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

print(m)

Submission status: Accepted

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

    hmin = h[K+i-1] - h[i]
    if c_hmin > hmin:
        c_hmin = hmin

print(c_hmin)

Submission status: Accepted

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

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

    if sabun == None:
        sabun = bbb
        continue

    if sabun > bbb:
        sabun = bbb

    if i+b == len(new_list):
        break
    
print(sabun)

Submission status: Accepted

Solution code:

l = [int(x) for x in input().split(' ')]
n,k = l
h = []

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

i,j = 0,k-1

r = float('inf')
while j < len(h):
	mn,mx=h[i],h[j]
	r = min(r, mx-mn)
	i,j=i+1,j+1

print(r)

Submission status: Accepted

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

hmax=0
hmin=99999999999999

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

n_list=[]
while True:
    try:
        n_list.append(int(input()))
    except EOFError:
        break

n_list.sort()

for i in range(0, n-k+1):
    diff = n_list[i+k-1] - n_list[ i ]
    if hmin > diff:
        hmin = diff

print( hmin )

Submission status: Accepted

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([h[x+k-1] - h[x] for x in range(n-k+1)]))

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
tree_list = [int(input()) for _ in range(N)]
tree_list = sorted(tree_list)
ans = 10**9
for i in range(N-K+1):
    compare = tree_list[i+K-1]-tree_list[i]
    if ans > compare:
        ans = compare
print(ans)

Submission status: Accepted

Solution code:

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

if __name__ == '__main__':
  solve()

Submission status: Accepted

Solution code:
a=[]
a=input().split(" ")
N=int(a[0])
K=int(a[1])
b=[]
b=[int(input()) for i in range(N)]
b.sort()
c=[]
for i in range(0,N-K+1) :
    b1=b[K-1+i]
    b2=b[i]
    d=b1-b2
    c.append(d)
c.sort()
p=c[0]
print(p)

Submission status: Accepted

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

p = []
#min = 10**9+1
for i in range(n) :
    t = int(input())
    p.append(t)
    #if t < min :
    #   min = t

p.sort()

ans = 10**9+1
for i in range(n-k+1) :
    max = p[i+k-1]
    min = p[i]
    t = max - min
    if ans > t :
        ans = t
print(ans)

Submission status: Accepted

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):
    if abs(dl[j+k-1] - dl[j]) <= ans:
        ans = abs(dl[j+k-1] - dl[j])
print(int(ans))

Submission status: Accepted

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

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

print(min_l)

Submission status: Accepted

Solution code:
import collections

H, W = map(int, input().split())
S = [int(input()) for _ in range(H)]
sa = 1000000000000
c = collections.Counter(S)
B = c.most_common()
if B[0][1] >= W:
  print("0")

else:
  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 == 1:
      break

  print(sa)

Submission status: Accepted

Solution code:
import numpy as np
N, K = map(int,input().split())
p = []
for i in range(N):
    p.append(int(input()))
p.sort()
p = np.array(p)
before = p[:N-K+1]
after = p[K-1:]
print(np.min(after-before))

Submission status: Accepted

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]

minimum = a[N-1]
for i in range(N-K+1):
    offset = a[i+K-1] - a[i]
    if minimum > offset:
        minimum = offset

print(minimum)

Submission status: Accepted

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

Submission status: Accepted

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

h.sort()
mn = 1e+10
for i in range(N - K + 1):
    diff = h[i+K-1] - h[i]
    mn = min(mn, diff)
print(mn)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
 
tmp=input()
n = int(tmp.split(" ")[0])
k = int(tmp.split(" ")[1])
 
h=[]
 
for i in range(n):
    h.append(int(input()))
 
h.sort()
 
x=int(0)
y=int(h[k-1]-h[0])
 
for i in range(n-k+1):
    x=h[i+k-1]-h[i]
    if x < y:
        y=x
 
print(y)

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
list1 = []
for i in range(n):
    x = int(input())
    list1.append(x)
y = 10000000000
a = -1
list1.sort()
for i in range(n-k+1):
    z = list1[k+i-1]-list1[0+i]
    if y > z:
        y = z
print(y)

Submission status: Accepted

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 = h_list[i]-h_list[j]
    ans = min(ans,diff)
    i += 1
    j += 1
print(ans)

Submission status: Accepted

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

t = 0

for i in range(n - k + 1):
    _t = h[i] - h[i + k - 1]
    if _t == 0:
        print(0)
        exit()
    else:
        if t == 0 or _t < t:
            t = _t

print(t)

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
l = []
for i in range(n):
    l.append(int(input()))
l.sort()
#print(l)
m = 1000000000
for i in range(n-k+1):
#    print(str(l[i+k-1])+":"+str(l[i]))
    a = l[i+k-1]-l[i]
    if m > a:
        m = a
print(m)

Submission status: Accepted

Solution code:
N, X = [int(i) for i in input().split(' ')]
from collections import deque

import sys
hs = []
for line in sys.stdin:
    h, = line.strip().split(" ")
    hs.append(int(h))
hs = deque(sorted(hs))
counter = 0

minh = None
for i in range(N):
    if (i + (X-1)) > N-1:
        break
    ch = hs[i+(X-1)] - hs[i]
    if minh == None:
        minh = ch
    elif ch < minh:
        minh = ch
print(minh)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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_diff = 10000000000
while i < N - K + 1:
  diff = h[i+K-1] - h[i]
  if min_diff > diff:
    min_diff = diff
  i += 1
print(min_diff)

Submission status: Accepted

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

tree = sorted(tree)

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

print(minimum)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np
from sys import stdin
N,K=[int(x) for x in stdin.readline().rstrip().split()]
data=[]
for i in range(N):
    data.append(int(input()))
data.sort()
data.reverse()
ans=float("inf")
for i in range(N-K+1):
    tmp=data[i]-data[i+K-1]
    if ans > tmp:
        ans=tmp
print(ans)

Submission status: Accepted

Solution code:

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

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

sort_h = sorted(h)
diff_h = 0
ans = []
for i in range(len(h)-K+1):
    diff_h = sort_h[i+(K-1)] - sort_h[i] 
    ans.append(diff_h)

print(min(ans))

Submission status: Accepted

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

trees.sort()

result = 10000000000
for i in range(total-select+1):  
  result = min(result, trees[i+select-1] - trees[i])

print(result)

Submission status: Accepted

Solution code:
N, K = map(int,input().split())
H = [int(input()) for k in range(N)]
H = sorted(H)
ans = float("inf")
for k in range(N-K+1):
    ans = min(ans, H[k+K-1]-H[k])
print(ans)

Submission status: Accepted

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 = arr2[y+k-1]
	mn = arr2[y]
	ans = min(ans,mx-mn)

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(reverse = True)
ans = 10000000000
maxh = h[:k]
maxn = 0
for i in range(k, n):
    if ans > maxh[maxn] - maxh[(maxn-1)%k]:
        ans = maxh[maxn] - maxh[(maxn-1)%k]
    maxh[maxn] = h[i]
    maxn = (maxn+1) % k
if ans > maxh[maxn] - maxh[(maxn-1)%k]:
    ans = maxh[maxn] - maxh[(maxn-1)%k]
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n_k = list(map(int,input().split()))
h = []
max_a = 1000000000

for i in range(n_k[0]):
    h.append(int(input()))

h.sort(reverse = True)

for j in range(n_k[0] - (n_k[1]-1)):
    a = h[j] - h[j + (n_k[1]-1)]
    if a <= max_a:
        max_a = a

print(int(max_a))

Submission status: Accepted

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+1):
    num = hlist[_]-hlist[_+(K-1)]
    sa.append(num)
sa.sort()
print(sa[0])

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
hei_list = []
for i in range(n):
    tmp = int(input())
    hei_list.append(tmp)
hei_list.sort()
for i in range(n-k+1):
    tmp_dist = hei_list[i + k - 1] - hei_list[i]
    if i == 0:
        min_dist = tmp_dist
    elif tmp_dist < min_dist:
        min_dist = tmp_dist
print(min_dist)

Submission status: Accepted

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

s = sorted(h)
candidate = 10 ** 9

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

print(candidate)

Submission status: Accepted

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

sa.sort()
print(sa[0])

Submission status: Accepted

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(min(Q))

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
list = []
m = -1
for i in range(N):
    list.append(int(input()))
List = sorted(list)
for i in range(N-K+1):
    mt = List[i+K-1]-List[i]
    if m == -1 or m > mt:
        m = mt
print(m)

Submission status: Accepted

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

shlist = sorted(hlist)
result = 1000000000
for i in range(n-k+1): # 5-3 = 2 = 0,1,2
  sa = abs(shlist[i] - shlist[i+k-1])
  if result > sa:
    result = sa

print(result)

Submission status: Accepted

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 <= ans:
        ans = SS
    S = SS
print(ans)

Submission status: Accepted

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

H=[]
for i in range(N):
	h=int( sys.stdin.readline() ) 
	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: Accepted

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

Submission status: Accepted

Solution code:
#coding:utf-8

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

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

h = sorted(h)

min_val = 100000000000
for i in range(N-K+1):
    min_val = min(abs(h[i+K-1] - h[i]), min_val)
print(min_val)

Submission status: Accepted

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

ans = 100000000000000000000000000000
for i in range(N-K+1):
	if(ans>H[i+K-1]-H[i]):
		ans = int(H[i+K-1]-H[i])

print(ans)

Submission status: Accepted

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)

min_dif = 1000000000
for i in range(N-K+1):
    start = i
    end = K - 1 + i

    end_v = sorted_h_list[end]
    start_v = sorted_h_list[start]
    dif = end_v - start_v

    if min_dif > dif:
        min_dif = dif

print(int(min_dif))

Submission status: Accepted

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=hs[i+k-1] - hs[i]
    if m==-1:
        m=cnd
    else:
        if cnd < m:
            m=cnd
    i+=1
print(int(m))

Submission status: Accepted

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

Submission status: Accepted

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

a = [int(input()) for _ in range(n)]
a.sort()
b = 0
ans = []

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

Submission status: Accepted

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

h = []

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

sortedh = sorted(h)

min_ = 1e9

for i in range(N-K+1):
    diff = sortedh[i+K-1] - sortedh[i]
    min_ = min(min_, diff)

print(min_)

Submission status: Accepted

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

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

print(min)

Submission status: Accepted

Solution code:
import numpy as np
(N,K) = map(int,input().split())
h = np.array([int(input()) for _ in range(N)])
h.sort()
d = np.array([h[i+K-1]-h[i] for i in range(N-K+1)])
print(d.min())

Submission status: Accepted

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

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

hs.sort()

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

Submission status: Accepted

Solution code:
N,K = list(map(int,input().split()))
H = [int(input()) for _ in range(N)]
H.sort()
min=10**9
for i in range(N+1-K):
    if(min>H[i+K-1]-H[i]):
        min=H[i+K-1]-H[i]
print(min)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
h = [int(input()) for _ in range(N)]
h.sort(reverse=True)
mini = 10 ** 9

for i in range(N-K+1):
  a = h[i] - h[i+K-1]
  if a < mini:
    mini = a
    
print(mini)

Submission status: Accepted

Solution code:
# coding:utf-8

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

an.sort()

lmin = an[-1] - an[0]

for i in range(N-K+1):
    bn = an[i+K-1] - an[i]
    if lmin >= bn:
        lmin = bn
    if lmin == 0:
        break

print(lmin)

Submission status: Accepted

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

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

#%%
# n, k = 5, 3
# h = [10, 15, 11, 14, 12]


#%%
h.sort()

#%%

#%%
picked = h[:k]


#%%
ans = h[k-1] -h[0]


#%%
dif = ans

for i in range(n - k):
    dif += h[i] - h[i+1]
    dif += h[i + k] - h[i + k - 1]
    if ans > dif:
        ans = dif
    # print(dif)

#%%
print(ans)

#%%

Submission status: Accepted

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

#昇順ソート
sorted_h = sorted(h_list)

best_score = 1000000000000
for idx, h in enumerate(sorted_h[:-K+1]):
    highest_idx = idx + K - 1
    score = sorted_h[highest_idx] - h
    
    if score < best_score:
        best_score = score
print(best_score)

Submission status: Accepted

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

Submission status: Accepted

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))
		height_distance.append(a[i+k-1]-a[i])
		#print(height_distance)

print(min(height_distance))

Submission status: Accepted

Solution code:
from sys import stdin

N, K = [int(x) for x in stdin.readline().rstrip().split()]

ls = []

for i in range(N):
    h = int(stdin.readline().rstrip())
    ls.append(h)

ls = sorted(ls)

minval = ls[K-1] - ls[0]

for i in range(1, N-K+1):
    tmp = ls[i+K-1] - ls[i]
    minval = min(minval, tmp)

print(minval)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy
N,K = map(int,input().split())
H = numpy.sort([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:
def main():
    N, k = (int(i) for i in input().split())
    h = []
    for _ in range(N):
        h.append(int(input()))
    h.sort()
    hdiffmin= 10**10
    for i in range(N - k + 1):
        diff = abs(h[i] - h[i+k-1])
        if diff < hdiffmin:
            hdiffmin = diff
    print(hdiffmin)


main()

Submission status: Accepted

Solution code:
from math import fabs


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

    h.sort()
    min = fabs(h[0] - h[N-1])
    for i in range(N - K + 1):
        min_temp = fabs(h[i] - h[i + K - 1])
        if min > min_temp:
            min = min_temp
    print(int(min))


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

for i in range(N):
    list.append(int(input()))
list.sort()
#print(list)

DIF = []
for i in range(N - K + 1):
    DIF.append(abs(list[i] - list[i + K - 1]))
print(min(DIF))

Submission status: Accepted

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) :
	max_v = trees[i+K-1]
	min_v = trees[i]
	diff = max_v - min_v
	if (min_diff > diff) :
		min_diff = diff
	if (min_diff == 0) :
		break



print(min_diff)

Submission status: Accepted

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

h.sort()
#print(h)
for i in range(n - k + 1):
  sa.append(h[i+k-1] - h[i])

#print(sa)
print(min(sa))

Submission status: Accepted

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

Submission status: Accepted

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

dif = 1000000000
h_sort = sorted(h)
for i in range(n-k+1):
    dif = min(dif,h_sort[i+k-1]-h_sort[i])

print(dif)

Submission status: Accepted

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

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

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

print(d)

Submission status: Accepted

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

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

heights = sorted(heights)

diffs = [heights[j + k - 1] - heights[j] for j in range(n - k + 1)]

print(min(diffs))

Submission status: Accepted

Solution code:
# coding: utf8

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

if __name__ == "__main__":
    n, k = read_int_n()
    num = []
    for _ in range(n):
        num.append(int(input()))
    num.sort()
    ans = 10 ** 9
    for i in range(n-k+1):
        if ans > num[i+k-1] - num[i]:
            ans = num[i+k-1] - num[i]
    print(ans)

Submission status: Accepted

Solution code:
N, K = map(int, raw_input().split())
h = [input() for _ in range(N)]
h.sort(reverse=True)
print min([h[i] - h[i + K - 1] 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()
minimum = 1e10
for i in range(N-K+1):
	if minimum > h[i+K-1] - h[i]:
		minimum = h[i+K-1] - h[i]
print(minimum)

Submission status: Accepted

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

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

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

gosa.sort()
print(gosa[0])

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:

N, K = input().split()
N, K = int(N), int(K)



li = [int(input()) for i in range(N)]
li.sort()
min_val = 10e10
for i in range(N-K+1):
    val = li[K + i - 1] - li[i]
    if val < min_val:
        min_val = val
print(int(min_val))

Submission status: Accepted

Solution code:
import pdb


def main():
    tmp = input().split()
    n = int(tmp[0])
    k = int(tmp[1])

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

    tree.sort()
    #print(tree)
    min_num = -1
    
    for i in range(0, n-k+1):
        #print(tree[i], tree[i+k-1])
        tmp = tree[i+k-1] - tree[i]
        if min_num == -1:
            min_num = tmp
        elif min_num > tmp:
            min_num = tmp

    print("%d" % min_num)

        
if __name__ == '__main__':
    main()

Submission status: Accepted

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

H.sort()

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

Submission status: Accepted

Solution code:
import sys
stdin = sys.stdin
 
sys.setrecursionlimit(10**5) 
 
def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

n,k = li()
h = [ni() for _ in range(n)]

h.sort()

ans = 10**18
for st in range(n-k+1):
    ans = min(ans, h[st+k-1] - h[st])
    
print(ans)

Submission status: Accepted

Solution code:
n,k=map(int, input().split())
l=[]
p=[]
for o in range(0,n):
 l.append(int(input()))
l.sort()
for o in range(0,n-k+1):
 p.append(l[o+k-1]-l[o])
print(min(p))

Submission status: Accepted

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):
    l.append(h[i]-h[i-k+1])
print(min(l))

Submission status: Accepted

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 = l2[i] - l2[i+d]
    if (d_min > tmp ):
        d_min = tmp
print(d_min)

Submission status: Accepted

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

Submission status: Accepted

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

min_diff = 10 ** 9
for i in range(n-k+1):
    h_max = h_list[i]
    h_min = h_list[i+k-1]
    h_diff = h_max - h_min
    if min_diff > h_diff:
        min_diff = h_diff
    if min_diff == 0:
        print(0)
        exit()
print(min_diff)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,k = map(int, input().split())
h_list = []
for i in range(n):
    h = int(input())
    h_list.append(h)
h_list.sort()
ans = 10000000000000000
for i in range(n-k+1):
    if ans > h_list[i+k-1] - h_list[i]:
        ans = h_list[i+k-1] - h_list[i]  
print(ans)

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
L=[int(input()) for _ in range(N)]
L=sorted(L)
P=[]
#print(L)
for i in range(N-K+1):
    P.append( L[i+K-1] - L[i])
print(min(P))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np
def main():
 
	buf = input().split()
	n = int(buf[0])
	k = int(buf[1])
	l = np.zeros(n)
	for i in range(n):
		l[i] = int(input())
 
	l = np.sort(l)
	min = -1
	for i in range(n-k+1):
		bufmin = l[i+k-1] - l[i]
		if (min == -1):
			min = bufmin
		elif (bufmin < min):
			min = bufmin
 
	print(int(min))
 
main()

Submission status: Accepted

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

h.sort()

m = -1
for i in range(N-K+1):
    d = h[i+K-1] - h[i]
    if d < m or m == -1:
        m = d
        
print(m)

Submission status: Accepted

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)
	ans = H[i + K - 1] - H[i]
	ansl.append(ans)

print(min(ansl))