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:
#-*-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=[]
    answers=[]
    dp=[]
    n,m = map(int,input().split())
    [numbers.append(int(input())) for _ in range(n)]
    numbers.sort()
#    [answers.append(numbers[i+m-1] - numbers[i]) for i in range(n-m+1)]
    answers=ngram(numbers,3)
    for answer in answers:
        tmp = max(answer)-min(answer)
        dp.append(tmp)
#    print(answers)
    print(min(dp))

if __name__=="__main__":
    main()

Submission status: Wrong Answer

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=[]
    s_numbers=[]
    dp=[]
    n,m = map(int,input().split())
    [numbers.append(int(input())) for _ in range(n)]
    s_numbers=sorted(numbers)
    n_list=ngram(s_numbers,3)

    for data in n_list:
        if len(data)==3:
            min_data=min(data)
            max_data=max(data)
            dp.append(max_data-min_data)
        else:
            break
    print(min(dp))

if __name__=="__main__":
    main()

Submission status: Wrong Answer

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=[]
    s_numbers=[]
    dp=[]
    n,m = map(int,input().split())
    [numbers.append(int(input())) for _ in range(n)]
    s_numbers=sorted(numbers)
    n_list=ngram(s_numbers,3)
    print(n_list)

    for data in n_list:
        if len(data)==3:
            min_data=min(data)
            max_data=max(data)
            dp.append(max_data-min_data)
        else:
            break
    print(min(dp))

if __name__=="__main__":
    main()

Submission status: Wrong Answer

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

def main():
    numbers=[]
    s_numbers=[]
    dp=[]
    n,m = map(int,input().split())
    [numbers.append(int(input())) for _ in range(n)]
    s_numbers=sorted(numbers)
    n_list=ngram(s_numbers,3)

    for data in n_list:
        if len(data)==3:
            min_data=min(data)
            max_data=max(data)
            dp.append(max_data-min_data)
        else:
            break
    print(min(dp))

if __name__=="__main__":
    main()

Submission status: Wrong Answer

Solution code:
def main():
    N, K = map(int, input().split())
    h = []
    for _ in range(N):
        h.append(int(input()))
    h.sort()
    ans = N
    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: Wrong Answer

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])
    print(ref, ans)
    ans = min(ans, ref)

print(ans)

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 = H[k-1] - H[0]
b = H[-1] - H[- k]

print(min(a,b))

Submission status: Wrong Answer

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-2):
    L.append(l[j+2]-l[j])
print(min(L))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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):
    ans=min(h[k+i]-h[i],ans)
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 = 100000000000000000000000000000000000000
for i in range(n-k+1):
    ans = min(ans, h[i+k-1] - h[i])
    print(i)
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 = 1000000
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 = map(int, input().split())
hl = []
for i in range(n):
    hl.append(int(input()))

hl.sort()
ans = 100000001

for j in range(n-k+1):
    s = hl[j+k-1] - hl[j]
    if s < ans:
        ans = s
    else:
        pass
print(ans)

Submission status: Wrong Answer

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

hl.sort(reverse=True)

ans = 100000001

for j in range(n-k+1):
    s = hl[j] - hl[j+k-1]
    if s < ans:
        ans = s
    else:
        pass
print(ans)

Submission status: Wrong Answer

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

m = float('inf')
for i in range(n-k+1):
  m = min(m, h[i]-h[i-k])

print(m)

Submission status: Wrong Answer

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

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

print(m)

Submission status: Wrong Answer

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

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

print(m)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
L=sorted([int(input()) for i in range(N)])
ans=10**6
for i in range(K-1,N):
  ans=min(ans,L[i]-L[i-(K-1)])
print(ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
h = [0]*N
for i in range(N):
    h[i] = int(input())
h = sorted(h)
ans = 10**10
for i in range(N-2):
    tmp = h[i+2] - h[i]
    ans = min(tmp,ans)
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=10**5
for i in range(n-k+1):
    ans=min(ans,h[i+k-1]-h[i])
    if ans==0:break
print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
h=[int(input()) for i in range(n)]
h.sort()
print(h)
ans=10**5
for i in range(n-k+1):
    print(h[i+k-1],h[i])
    ans=min(ans,h[i+k-1]-h[i])
    if ans==0:break
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=10**5
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 = map(int, input().split())
A=[0]*N
for i in range(N):
    a=int(input())
    A[i]=a
A=sorted(A)

ans=[]
for i in range(N-K):
    ans.append(A[i+K-1]-A[i])

print(min(ans))

Submission status: Wrong Answer

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

ans=[]
for i in range(N-2):
    ans.append(A[i+2]-A[i])

print(min(ans))

Submission status: Wrong Answer

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

for n in range(N):
  h = int(input())
  H.append(h)

H.sort()

for i in range(N-K+1):
  dif = H[i+K-1] - H[i]
  if dif < min_dif:
    min_dif = dif
    
print(min_dif)

Submission status: Wrong Answer

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

hmax = 0
hmin = 10**18

for i in range(K):
    hmax = max(h[i],hmax)
    hmin = min(h[i],hmin)
print(hmax-hmin)

Submission status: Wrong Answer

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

diff = 10**9
for i in range(N-K+1):
    a = h[i]-h[i+K-1]
    if diff > a:
        diff = a
    if diff == 0:
        break
print(diff)

Submission status: Wrong Answer

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

for i in range(n-k+1):
  sa=h[n-k+1]-h[i]
  if sa<ma:
    ma=sa
    
print(ma)

Submission status: Wrong Answer

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

for i in range(n-k+1):
  b=h[i:n-k+i+1]
  sa=b[-1]-b[0]
  if sa<ma:
    ma=sa
    
print(ma)

Submission status: Wrong Answer

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

Hd=[]
for i in range(1,n):
    Hd.append(H[i]-H[i-1])
Hd.sort()
ans = sum(Hd[:k-1])
print(ans)

Submission status: Wrong Answer

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

Hd=[]
for i in range(1,n):
    Hd.append(H[i]-H[i-1])
Hd.sort()
ans = sum(Hd[::k-1])
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
A = [int(input()) for _ in range(N)]
B = sorted(set(A))
ans = []
flag = 0
for i in range(max(0, len(B)-K+1)):
  ans.append(B[i+K-1]-B[i])
  flag = 1
print(min(ans) if flag == 1 else "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()
ans=h[k-1]-h[0]
for i in range(1,n-k):
  ans=min(ans,h[i+k-1]-h[i])
print(ans)

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

Submission status: Wrong Answer

Solution code:
import sys


# \n
def input():
    return sys.stdin.readline().rstrip()


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

    H.sort()
    toll =10**8
    for i in range(N-K+1):
        toll = min(H[K+i-1] -H[i],toll)

    print(int(toll))




if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys


# \n
def input():
    return sys.stdin.readline().rstrip()


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

    H.sort()
    toll =10**8
    for i in range(N-K+1):
        toll = min(H[K+i-1] -H[i],toll)

    print(toll)




if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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)[::-1]
for i in range(N-K):
    ans = min(h[i]-h[i+K-1],ans)
print(str(ans))

Submission status: Wrong Answer

Solution code:
n,k=list(map(int,input().split()))
array=[]
for _ in range(n):
	h=int(input())
	array.append(h)
sarray=sorted(array)
ansarray=[]
for i in range(n-1):
	ansarray.append(abs(sarray[i]-sarray[i+1]))
sansarray=sorted(ansarray)
print(sum(sansarray[:k-1]))

Submission status: Wrong Answer

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**6

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

h.sort()

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

Solution code:
n,m=[int(i) for i in input().split()]
l=[]
c=[int(input()) for i in range(n)]
l=sorted(c)
print(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])
    f=(max(b)-min(b))
    if(f<maxs):
        maxs=f
print(maxs)

Submission status: Wrong Answer

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

hmax = 0
hmin = 0
ans = 10**5
for i in range(N - K + 1):
    hmax = h[i + K - 1]
    hmin = h[i]
    if hmax - hmin < ans:
        ans = hmax - hmin
print(ans)

Submission status: Wrong Answer

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

hmax = 0
hmin = 0
ans = 10**5
for i in range(N - K + 1):
    hmax = max(h[i: i + K])
    hmin = min(h[i: i + K])
    if hmax - hmin < ans:
        ans = hmax - hmin
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
if __name__ == '__main__':

	n,k = map(int,input().split())

	A = []
	for i in range(n):
		cmd = int(input())
		A.append(cmd)
	
	B = sorted(A,reverse=True)

	ans = 10**5
	for j in range(n-k+1):
		C = B[j:j+k]
		tmp = max(C)-min(C)
		ans = min(ans,tmp)
	print(ans)

Submission status: Wrong Answer

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

H.sort()

res = 10 ** 7

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

print(res)

Submission status: Wrong Answer

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

H.sort()

hmax1 = H[-1]
hmin1 = H[-K]

hmax2 = H[K - 1]
hmin2 = H[0]

print(min(hmax1 - hmin1, hmax2 - hmin2))

Submission status: Wrong Answer

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-2):
  h2 = h[i:i+3]
  ans = min(max(h2) - min(h2),ans)
print(str(ans))

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
h = [int(input()) for _ in range(N)]
h.sort()
# 小さい方から選ぶ
ans1 = h[K - 1] - h[0]
# 大きい方から選ぶ
ans2 = h[-1] - h[-K]
print(min(ans1, ans2))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def main():
    n, k = map(int, input().split())
    inlis = list()
    for _ in range(n):
        h = int(input())
        inlis.append(h)
    inlis.sort()
    ans = 10 * 100
    for i in range(n-k+1):
        tmp = inlis[i+k-1] - inlis[i]
        if tmp < ans:
            ans = tmp
    print(ans)
            



if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def main():
    n, k = map(int, input().split())
    inlis = list()
    for _ in range(0, n):
        h = int(input())
        inlis.append(h)
    inlis.sort()
    ans = 10 * 10
    for i in range(n-k+1):
        tmp = inlis[i+k-1] - inlis[i]
        if tmp < ans:
            ans = tmp
    print(ans)
            



if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import math

n, k = list(map(int, input().split()))
h = [int(input()) for _ in range(n)]
h.sort()
ans = math.inf
for i in range(n - k):
    ans = min(ans, h[i + k - 1] - h[i])

print(ans)

Submission status: Wrong Answer

Solution code:
import math

n, k = list(map(int, input().split()))
h = [int(input()) for _ in range(n)]
h.sort()
ans = math.inf
for i in range(n - k):
    ans = min(ans, max(h[i : i + k]) - min(h[i : i + k]))

print(ans)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
import sys
from collections import defaultdict

sys.setrecursionlimit(10**7)
input = sys.stdin.readline

n,k = map(int, input().split())
h = []
d = defaultdict(int)
cnt = []
for i in range(n):
    x = int(input())
    h.append(x)
    d[x] += 1

for i in d.items():
    if i[1] >= k:
        print(0)
        exit()

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

Submission status: Wrong Answer

Solution code:
import sys
from collections import defaultdict

sys.setrecursionlimit(10**7)
input = sys.stdin.readline

n,k = map(int, input().split())
h = []
d = defaultdict(int)
cnt = []
for i in range(n):
    x = int(input())
    h.append(x)
    d[x] += 1

for i in d.items():
    if i[1] >= k:
        print(0)
        exit()

sh = sorted(h)
print(sh[k - 1] - sh[0])

Submission status: Wrong Answer

Solution code:
import sys
from collections import defaultdict

sys.setrecursionlimit(10**7)
input = sys.stdin.readline

n,k = map(int, input().split())
h = []
d = defaultdict(int)
cnt = []
for i in range(n):
    x = int(input())
    h.append(x)
    d[x] += 1

sh = sorted(h)
if d[sh[0]] == k or d[sh[k - 1]] == k:
    print(0)
else:
    print(sh[k - 1] - sh[0])

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

print(ans)

Submission status: Wrong Answer

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

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

print(ans)

Submission status: Wrong Answer

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

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

print(ans)

Submission status: Wrong Answer

Solution code:
N,K = [int(x) for x in input().split()]
h = [0 for x in range(N)]
for i in range(N):
    h[i] = int(input())
h = sorted(h)
dis = []
for i in range(N-K+1):
    dis.append(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()
inf = float('inf')
min_ = inf

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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 - 3):
    _h = min(H[i:i+3])
    h = max(H[i:i+3])
    res = min(res, h - _h)
    if res == 0:
        break
print(res)

Submission status: Wrong Answer

Solution code:
# coding:utf-8
n, k = map(int, input().split())
h = [int(input()) for _ in range(n)]
h.sort()
ans = h[0]

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

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
from collections import deque, defaultdict
from math import sqrt, factorial
# def input(): return sys.stdin.readline()[:-1] # warning not \n
# def input(): return sys.stdin.buffer.readline().strip() # warning bytes
# def input(): return sys.stdin.buffer.readline().decode('utf-8')


def solve():
    n, k = [int(x) for x in input().split()]
    a = []
    for _ in range(n):
        a.append(int(input()))
    a.sort()
    ans = float('inf')
    for i in range(n-k):
        ans = min(ans, a[i+k-1]-a[i])
    print(ans)
t = 1
# t = int(input())
for case in range(1,t+1):
    ans = solve()


"""

a aa c ca c a b a ab a b c

"""

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
h=[int(input()) for _ in range(N)]
h.sort()
a=max(h[:K])
b=min(h[:K])
c=max(h[-K:])
d=min(h[-K:])
print(min(a-b,c-d))

Submission status: Wrong Answer

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

for n in range(N):
    H.append(int(input()))
    
h_max=max(H[:K])
h_min=min(H[:K])

for h in H[K:]:
    if h < h_max:
        h_max = h
    elif h > h_min:
        h_min = h
    
print(h_max-h_min)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
H = [int(input()) for _ in range(N)]
H.sort()
df = 0
min_h = H[K-1]-H[0]

for i in range(1, N-K-1):
    new_h = H[i+K-1]-H[i]
    if new_h < min_h:
        min_h = new_h

print(min_h)

Submission status: Wrong Answer

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

h.sort()

print(min(h[:K][-1] - h[:K][0], h[K-1:][-1] - h[K-1:][0]))

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
hh = []
minn = 10**12
for i in range(n):
    h = int(input())
    hh.append(h)
h = sorted(hh)
for i in range(n - k - 1):
    if h[i + k - 1] - h[i] < minn:
        minn = h[i + k - 1] - h[i]
print(minn)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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


resolve()

Submission status: Wrong Answer

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

print(sum(dif[:k-1]))

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)
Q=abs(h[0]-h[K-1])
h.sort()
QQ=abs(h[0]-h[K-1])
print(min(Q,QQ))

Submission status: Wrong Answer

Solution code:
def mi():
    return map(int, input().split())

def ii():
    return int(input())

def main():
    N, K = mi()
    h = [ii() for _ in range(N)]
    h.sort()
    m = 10*100
    for i in range(N-K+1):
        m = min(m, h[K+i-1]-h[i])
    print(m)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

print(ans)

Submission status: Wrong Answer

Solution code:
# 20200612_yorukatsu_c.py
N, K = map(int,input().split())
M = [int(input()) for i in range(N)]
M.sort()

print(min(abs(M[0]-M[K-1]),abs(M[N-1]-M[N-K])))

Submission status: Wrong Answer

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):
  list(s).sort()
  ans = min(ans, H[K-1] - H[0])
  
print(ans)

Submission status: Wrong Answer

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

print(H[K-1] - H[0])

Submission status: Wrong Answer

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

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

print(ans)

Submission status: Wrong Answer

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 = H[N-1] - H[N-K]
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
apple = []
for _ in range(n):
    apple.append(int(input()))
m = sorted(apple)
a = m[k-1] - m[0]
b = m[-1] - m[-k]
print(min(a, b))

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

for i in range(N):
  a = int(input())
  arr.append(a)
  
arr.sort()
ans = 10**10

for i in range(N-K):
  a = arr[i]
  b = arr[i+K-1]
  c = b-a
  ans = min(ans,c)
  
print(ans)

Submission status: Wrong Answer

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

for i in range(N):
  a = int(input())
  arr.append(a)
  
arr.sort()
ans = 10**10

for i in range(N-K):
  a = arr[i]
  b = arr[i+K]
  c = b-a
  ans = min(ans,c)
  
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
A = sorted([int(input()) for i in range(N)])
B = [A[i]-A[i-K] for i in range(K, N)]
print(min(B))

Submission status: Wrong Answer

Solution code:
## C - Christmas Eve
N, K = map(int, input().split())
H = sorted([int(input()) for i in range(N)])
rt = 10**9
for i in range(0,N-K):
    rt = H[K - 1 + i] - H[i] if rt > H[K - 1 + i] - H[i] else rt
print(rt)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n,k =map(int,input().split())
h=[]
for i in range(n):
    ki=int(input())
    h.append(ki)
h_l=sorted(h)
dis=[]
for i in range(n-1):
    dis.append(h_l[i+1]-h_l[i])
dis_l=sorted(dis)
print(sum(dis_l[0:k-1]))