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())
hl = [int(input()) for _ in range(n)]

hl_s = sorted(hl, reverse=True)
delta = 10*10**9

if n > 10000:
    while n > 10000:
        if hl_s[n//2] < (hl_s[0] + hl_s[n-1])//2:
            hl_s = hl_s[:n//2]
        else:
            hl_s = hl_s[n//2:]
        n = len(hl_s)

for i in range(len(hl_s)-k+1):
    h_max = max(hl_s[i:i+k])
    h_min = min(hl_s[i:i+k])
    if delta > (h_max - h_min):
        delta = h_max - h_min
    if delta == 0:
        break

print(delta)

Submission status: Wrong Answer

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

hl_s = sorted(hl, reverse=True)
delta = 10*10**9

if n > 1000:
    while n > 1000:
        if hl_s[n//2] < (hl_s[0] + hl_s[n-1])//2:
            hl_s = hl_s[:n//2]
        else:
            hl_s = hl_s[n//2:]
        n = len(hl_s)

for i in range(len(hl_s)-k+1):
    h_max = max(hl_s[i:i+k])
    h_min = min(hl_s[i:i+k])
    if delta > (h_max - h_min):
        delta = h_max - h_min
    if delta == 0:
        break

print(delta)

Submission status: Wrong Answer

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

hl_s = sorted(hl, reverse=True)
delta = 10*10**9

if n > 1000:
    if hl_s[n//2] > (hl_s[0] + hl_s[n-1])//2: #中央値＞平均値
        hl_s = hl_s[:n//2]
    else:
        hl_s = hl_s[n//2:]

for i in range(len(hl_s)-k+1):
    h_max = max(hl_s[i:i+k])
    h_min = min(hl_s[i:i+k])
    if delta > (h_max - h_min):
        delta = h_max - h_min
    if delta == 0:
        break

print(delta)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

def main():
    N,K = map(int,input().split())
    h = [int(input()) for _ in range(N)]
    h.sort()
    print(h)
    
    ans = 10**10
    for i in range(N-K+1):
        ans = min(ans,h[K+i-1]-h[i])
        
    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.sort()

ans = 10**9

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 i in range(N)]

H.sort()

ans = 999999999999999999999
for i in range(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 = [int(input())  for i in range(N)]

ans = []

for h in sorted(H):
  if len(ans) < K:
    ans.append(h)
  else:
    m = min(ans)
    tmp = ans[:]
    tmp.remove(m)
    tmp.append(h)
    if max(ans) - m >= max(tmp) - min(tmp):
      ans = tmp

print(max(ans) - min(ans))

Submission status: Wrong Answer

Solution code:

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

ans = []

for h in sorted(H):
  if len(ans) < K:
    ans.append(h)
  else:
    m = min(ans)
    tmp = ans[:]
    tmp.remove(m)
    tmp.append(h)
    if max(ans) - m >= max(tmp) - min(tmp):
      ans = tmp

print(max(ans) - min(ans), ans)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
N,K=map(int,input().split())
H=[0]*N
for i in range(N):
    H[i]=int(input())
H.sort()
mae=H[K-1]-H[0]
usr=H[N-1]-H[N-K]
print(H,mae,usr)
print(min(mae,usr))

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
N,K=map(int,input().split())
H=[0]*N
for i in range(N):
    H[i]=int(input())
H.sort()
mae=H[K-1]-H[0]
usr=H[N-1]-H[N-1-K+1]
#print(H)
print(min(mae,usr))

Submission status: Wrong Answer

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

h.sort()
dh = [h[i+1]-h[i] for i in range(0,n-1)]

dhsum = sum(dh[:k-1])
for i in range(1,n-k+1):
    dhsum = min(dhsum,dhsum-dh[i-1]+dh[i+k-2])

ans = dhsum
print(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[n-1] - h[n-k]

print(min(ans1,ans2))

Submission status: Wrong Answer

Solution code:

# ABC１１５　C

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

trees = []

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


trees.sort(reverse=True)

diff_list =[]

for i in range(N-1):
   diff_list.append(trees[i]-trees[i+1])

diff_list.sort()


print(sum(diff_list[:K-1]))

Submission status: Wrong Answer

Solution code:

# ABC１１５　C

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

trees = []

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


trees.sort(reverse=True)

diff_list =[]

for i in range(N-1):
   diff_list.append(trees[i]-trees[i+1])

diff_list.sort()


print(min(diff_list[:K]))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
lst=[int(input()) for _ in range(n)]
lst=sorted(lst)
ab=lst[k-1]-lst[0]
cd=lst[-1]-lst[-k]
print(min(ab,cd))

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
treelist = []
difflist = []
append = treelist.append
#print(treelist)
append_diff = difflist.append
for i in range(N):
    tree = int(input())
    #print(tree)
    append(tree)
treelist.sort()
print(treelist)
for i in range(K-1,N):
    diff = treelist[i] - treelist[i-K+1]
    #print(diff)
    append_diff(diff)
print(min(difflist))

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
H = sorted([int(input()) for _ in range(n)])
ans = float("INF")
for i in range(0,n - k + 1):
  print(i)
  selected = H[i:k + i]
  ans = min(ans,max(selected) - min(selected))
print(ans)

Submission status: Wrong Answer

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

    ans = 10 ** 5
    H_sorted = sorted(H)
    for i in range(n-k+1):
        ans = min(ans,H_sorted[i+k-1]-H_sorted[i])
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
# coding: utf-8
import sys

n,k = map(int, input().split())
H = sorted(map(int, sys.stdin.readlines()))
print(min(H[k-1] - H[0], H[-1] - H[-k]))

Submission status: Wrong Answer

Solution code:
A,B = map(int,input().split())
C = [int(input()) for _ in range(A)]
c = []
p = 0
C = sorted(C)
for i in range(B,A+1):
  c.append(max(C[i-(B):i]) - min(C[i-(B):i]))
  print(C[i-(B):i])
print(min(c))

Submission status: Wrong Answer

Solution code:
A,B = map(int,input().split())
C = [int(input()) for _ in range(A)]
c = []
p = 0
C = sorted(C)
for i in range(B-1,A):
  c.append(max(C[i-(B-1):i]) - min(C[i-(B-1):i]))
print(min(c))

Submission status: Wrong Answer

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

h = [input() for i in range(N)]
H = [int(s) for s in h]
H.sort()
d = 10000000000
for i in range(N-K):
    if d > (H[i+K-1] - H[i]):
        d = H[i+K-1] - H[i]

print(d)

Submission status: Wrong Answer

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

h.sort()
m=abs(h[0]-h[K-1])
for i in range(1,N-K):
    if m>abs(h[i]-h[i+K-1]):
        m=abs(h[i]-h[i+K-1])
print(m)

Submission status: Wrong Answer

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

h.sort()
m=abs(h[0]-h[K-1])
for i in range(1,N-K):
    if m>abs(h[i]-h[K-1]):
        m=abs(h[i]-h[K-1])
print(m)

Submission status: Wrong Answer

Solution code:
import sys

N,K=(int(x) for x in input().split())

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

minimum=10**10
tmp=N-K
for i in range(N-K+1):
	#print("i/minimun",i,minimum)
	if h[i+K-1]-h[i] < minimum:
		minimum=h[i+K-1]-h[i]

#print(minimum)
#print(h)

Submission status: Wrong Answer

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

h.sort(reverse=True)

m=int(h[0])-int(h[K-1])
print(m)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Diff=sorted(Diff)

print(sum(Diff[:K-1]))

Submission status: Wrong Answer

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

index = bisect.bisect_left(h, sum(h)/n)
t = h[:index] if len(h[:index]) == k else h[index:]
print(t[-1] - t[0])

Submission status: Wrong Answer

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

index = bisect.bisect_left(h, sum(h)/n)
left = h[:index]
right = h[index:]

diff_r = right[-1]-right[0]
diff_l = left[-1]-left[0]

if len(left) < len(right):
    print(diff_r)
elif len(left) > len(right):
    print(diff_l)
else:
    print(diff_r if diff_l <= diff_r else diff_l)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
h = list(int(input()) for _ in range(N))
fin = h[K-1] - h[0]
for i in range(1, N-K+1):
  t = h[i+K-1] - h[i]
  if fin > t :
    fin = t
print(fin)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
h = sorted(list(int(input()) for _ in range(N)))
fin = h[K-1] - h[0]
for i in range(1, N-K):
  t = h[i+K-1] - h[i]
  if fin > t :
    fin = t
print(fin)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
h = sorted(list(int(input()) for _ in range(N)))
fin = h[K-1] - h[0]
for i in range(1, N-K):
  k = h[i+K-1] - h[i]
  if fin > k :
    fin = k
print(fin)

Submission status: Wrong Answer

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

h_min = h[0]
h_max = h[k-1]
for i in range(1, n-k+1):
    if h[k-1 + i] - h[i] < h_max - h_min:
        h_max = h[k-1 + 1]
        h_min =h[i]
        
print(h_max - h_min)

Submission status: Wrong Answer

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

h_min = h[0]
h_max = h[k-1]
for i in range(1, n-k):
    if h[k-1 + i] - h[i] < h_max - h_min:
        h_max = h[k-1 + 1]
        h_min =h[i]
        
print(h_max - h_min)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

L = sorted(L)
ans_L = []
for i in range(n-k+1):
    ans = L[i+k-1] - L[i]
    ans_L.append(ans)
print(min(ans_L))

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
hlist = [int(input()) for _ in range(N)]
h_sorted = sorted(hlist)
mae_K = h_sorted[K-1] - h_sorted[0]
usiro_K = h_sorted[-1] - h_sorted[-K]
print(mae_K if mae_K < usiro_K else usiro_K)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
hlist = [int(input()) for _ in range(N)]
h_sorted = sorted(hlist)
mae_K = h_sorted[K-1] - h_sorted[0]
usiro_K = h_sorted[-1] - h_sorted[-K-1]
print(mae_K if mae_K < usiro_K else usiro_K)

Submission status: Wrong Answer

Solution code:
n, k = [int(i) for i in input().split()]
stock = []
for i in range(n):
	stock.append(int(input()))
stock = sorted(stock)
ret = 100000
for i in range(n - k + 1):
	rangelist = stock[i:i+k]
	length = stock[i+k-1] - stock[i]
	ret = min(ret, length)
print (ret)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys


def solve(N: int, K: int, h: "List[int]"):
    h_sort = sorted(h, reverse=True)
    ans = 10*9
    for i,h_top in enumerate(h_sort[:N-K+1]):
        h_bottom = h_sort[i+K-1]
        ans = min(ans, h_top - h_bottom)
    print(int(ans))


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    h = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, K, h)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys


def solve(N: int, K: int, h: "List[int]"):
    h_sort = sorted(h, reverse=True)
    ans = 10*9
    for i,h_top in enumerate(h_sort[:N-K+1]):
        h_bottom = h_sort[i+K-1]
        ans = min(ans, h_top - h_bottom)
    print(ans)


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    h = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, K, h)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys


def solve(N: int, K: int, h: "List[int]"):
    h_sort = sorted(h, reverse=True)
    ans = 10*9
    for i,h_top in enumerate(h_sort[:-K+1]):
        h_bottom = h_sort[i+K-1]
        ans = min(ans, h_top - h_bottom)
    print(ans)


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    h = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, K, h)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

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)
print(h)
dh_max = h[0]

for i in range(N - K + 1):
    dh = max(h[i:K+i]) - min(h[i:K+i])
    if dh < dh_max:
        dh_max = dh

print(dh_max)

Submission status: Wrong Answer

Solution code:
N, K = [int(x) for x in input().split()]
H = sorted([int(input()) for _ in range(N)])
D = [-1] * (N-1)
for i in range(N-1):
    D[i] = H[i+1] - H[i] 
print(sum(sorted(D)[:(K-1)]))

Submission status: Wrong Answer

Solution code:
import itertools


N, K = map(int, input().split())
ans = 0

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

for x in itertools.combinations(set(tree), K):
    if all(x[i] == x[i + 1] for i in range(len(x) - 1)) or all(x[i] == x[i + 1] + 1 for i in range(len(x) - 1)) or all(x[i] == x[i + 1]-1 for i in range(len(x) - 1)):
        height = max(x) - min(x)
        ans = max(ans, height)

print(ans)

Submission status: Wrong Answer

Solution code:
import itertools


N, K = map(int, input().split())
ans = 0

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

for x in itertools.combinations(tree, K):
    if all(x[i] == x[i + 1] for i in range(len(x) - 1)) or all(x[i] == x[i + 1] + 1 for i in range(len(x) - 1)) or all(x[i] == x[i + 1]-1 for i in range(len(x) - 1)):
        height = max(x) - min(x)
        ans = max(ans, height)

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

diff_min = H[2] - H[0]

for i in range(0,N - K + 1):

  diff = H[i + K - 1] - H[i]
  if diff <= diff_min:
    diff_min = diff
    
print(diff_min)

Submission status: Wrong Answer

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

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

diff_min = H[2] - H[0]

for i in range(0,N - K + 1):

  diff = H[i + K - 1] - H[i]
  if diff < diff_min:
    diff_min = diff
    
print(diff_min)

Submission status: Wrong Answer

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

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

H = sorted(H)

diff_min = abs(H[2] - H[0])



for i in range(0,N - K + 1):

  diff = abs(H[i + K - 1] - H[i])
  if diff < diff_min:
    diff_min = diff
    
print(diff_min)

Submission status: Wrong Answer

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

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

H = sorted(H)

diff_min = abs(H[2] - H[0])

for i in range(0,N - 2):
  diff = abs(H[i + 2] - H[i])
  if diff < diff_min:
    diff_min = diff
    
print(diff_min)

Submission status: Wrong Answer

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


cnt = 0
for num, value in h_cnt.most_common():
  if(cnt == 0):
    h_max = num
    remain = k - value
  else:
    remain -= value
  if(remain <= 0):
    print(abs(h_max - num))
    exit()
  cnt += 1

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

minh = 10000000000
for i in range(n-2):
	if h[i+2] - h[i] < minh:
		minh = h[i+2] - h[i]

print (minh)

Submission status: Wrong Answer

Solution code:
from functools import reduce
n, k = map(int, input().split())

heights = []
for _ in range(n):
	heights.append(int(input()))

heights.sort()

diff = [0]*n

for i in range(k,len(heights)):
	diff[i] = heights[i]-heights[i-k]
	
print(reduce(min, diff[k:], 1000000002))

Submission status: Wrong Answer

Solution code:
from functools import reduce
n, k = map(int, input().split())

heights = []
for _ in range(n):
	heights.append(int(input()))

heights.sort()

diff = [0]*n

for i in range(2,len(heights)):
	diff[i] = heights[i]-heights[i-2]
	
print(reduce(min, diff[2:], 1000000002))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n, k = map(int,input().split())
h = [int(input()) for _ in range(n)]
h.sort()
between = [h[i+1]-h[i] for i in range(n-1)]
answer = 10**9
for i in range(1,n-1):
  between[i] += between[i-1]

for i in range(n-k):
  answer = min(answer, between[i+k-1]-between[i])
  
print(answer)

Submission status: Wrong Answer

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

for i in range(n-k+1):
  answer = min(answer, sum(between[i: i+k-1]))
  
print(answer)

Submission status: Wrong Answer

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

h.sort()
h = h[:k]
print(h[-1]-h[0])

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
h = sorted([int(input()) for _ in range(n)], reverse=True)
ans = h[0]
for i in range(n-2):
  if (h[i]-h[i+2])<ans:
    ans=h[i]-h[i+2]
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 = sorted(H)
mi = 10**9
for i in range(N-K):
    _ = H[i+K] - H[i]
    if mi > _:
        mi = _
print(mi)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n, k, *h = map(int, open(0).read().split())
h.sort()
ans = 0
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())
h = [int(input()) for _ in range(n)]
h.sort()

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

Submission status: Wrong Answer

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

ans_list = []
for index, tree in enumerate(trees):
    if  k-1 <= index:
        print(tree)
        ans_list.append(tree-trees[index-(k-1)])
print(min(ans_list))

Submission status: Wrong Answer

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

    h = sorted(h)

    res = 10 ** 9 + 1
    for i in range(N-K):
        if res > max(h[i:i+K]) - min(h[i:i+K]):
            res = max(h[i:i+K]) - min(h[i:i+K])
    
    print(res)

Submission status: Wrong Answer

Solution code:
n,k = map(int, input().split())
h = [0] * n
ans = 0
for i in range(n):
  h[i] = int(input())
h.sort()
for i in range(n-k):
  temp = h[i+k-1] - h[i]
  if temp == 0:
    ans = temp
    break
  elif i == 0:
    ans = temp
  elif ans > temp:
    ans = temp
print(ans)

Submission status: Wrong Answer

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


sh = sorted(h)

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

print(min_)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
H = [int(input()) for i in range(N)]
h = sorted(H)
k=[0]*(N-2)
ans=0
for i in range(N-2):
    k[i] =h[i+2]-h[i]
ans=min(k)
print(ans)

Submission status: Wrong Answer

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

diff = 10 ** 6
for i in range(N):
    if i + K - 1 <= N - 1:
        diff = min(diff, h[i] - h[i+K-1])
    else:
        break

print(diff)

Submission status: Wrong Answer

Solution code:
a=list(map(int,input().split()))
n=[0]*a[1]
for i in range(a[0]):
  n.sort()
  tmp = int(input())
  if n[-1]<tmp and n[0]>tmp:
    n=n
  else:
    if n[-1]>tmp:
      n[-1] = tmp
    else:
      n[0] = tmp
print(n[-1]-n[0])

Submission status: Wrong Answer

Solution code:
a=list(map(int,input().split()))
n=[0]*a[1]
for i in range(a[0]):
  n.sort()
  tmp = int(input())
  if max(n)<tmp and min(n)>tmp:
    n=n
  else:
    if max(n)>tmp:
      n[-1] = tmp
    else:
      n[0] = tmp
print(n[-1]-n[0])

Submission status: Wrong Answer

Solution code:
# print('input >>')

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

h.sort()
hdiff = [h[i + 1] - h[i] for i in range(len(h) - 1)]
hdiff.sort()
ans = sum(hdiff[:K-1])

# print('----------output----------')
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def MAP(): return list(map(int,input().split()))
def INT(): return int(input())
def FLOAT(): return float(input())
MOD = 10**9+7

n,k = MAP()
h = [INT() for _ 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:


import sys
input = sys.stdin.readline
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
# A = [ int(input()) for _ in range(N) ]
##############################

from collections import Counter

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

c = Counter(H)
#print(c)

t = []
for k, v in c.most_common():
    if K >= v:
        K -= v
        t.append(k)
    else:
        t.append(k)
        K = 0

    if K == 0:
        break

print(max(t) - min(t))

Submission status: Wrong Answer

Solution code:
def christmasEve():
    N, K = map(int, input().split())
    h = [int(input()) for i in range(N)]
    strees = sorted(h)
    d = sorted([y-x for x,y in zip(strees, strees[1:])])
    print(sum([i for i in d[:K]]))
 
if __name__ == "__main__":
    christmasEve()

Submission status: Wrong Answer

Solution code:
def christmasEve():
    N, K = map(int, input().split())
    h = [int(input()) for i in range(N)]
    strees = sorted(h)
    d = sorted([y-x for x,y in zip(strees, strees[1:])])
    print(sum([i for i in d[1:K]]))

if __name__ == "__main__":
    christmasEve()

Submission status: Wrong Answer

Solution code:
def christmasEve():
    N, K = map(int, input().split())
    h = [int(input()) for i in range(N)]
    strees = sorted(h)
    d = sorted([ y -x for x ,y in zip(strees, strees[1:])])
    print(sum([i for i in d[1:N]]))

if __name__ == "__main__":
    christmasEve()

Submission status: Wrong Answer

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

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

H.sort()

output = H[K - 1] - H[0]

if output > H[-1] - H[-K]:
    output = H[-1] - H[-K]

print(output)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
tree = []
count = 0
ans = 0

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

tree.sort(reverse=True)
for i in range(N-2):
    if tree[i] - tree[i+1] <= 1 and tree[i+1] - tree[i+2] <= 1:
        count = tree[i] - tree[i+2]
        ans = max(count, ans)

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import collections
N,K = map(int,input().split())
h =[str(input()) for i in range(N)]
h.sort()
set_h = list(set(h))
c = collections.Counter(h)
for i in set_h:
    if int(c[str(i)]) == K:
        print(0)
        exit()
int_h = [int(s) for s in h]
print(max(int_h[:K]) - min(int_h[:K]))

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
h =[int(input()) for i in range(N)]
h.sort()
if len(h) != len(set(h)):
    print(0)
else:
    print(max(h[:K]) - min(h[:K]))

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