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())
 
#print(n,k)
Ar = [0]*100001
 
for i in range(n):
#	print("i=",i)
	dat = int(input())
	Ar[i] = dat
#	print(Ar)
 
ans = 1000000000
 
for i in range(n-k+1):
	h = Ar[i]
	l = Ar[i]
#	print("Start = ",i)
	for j in range(i,i+k+1):
		if h < Ar[j]:
			h = Ar[j]
		if l > Ar[j]:
			l = Ar[j]
	if ans > h-l:
		ans = h-l
#	print("diff=",ans,h,l)
 
print (ans)

Submission status: Wrong Answer

Solution code:
n,k= map(int, input().split())
 
#print(n,k)
Ar = [0]*100001
 
for i in range(n):
#	print("i=",i)
	dat = int(input())
	Ar[i] = dat
#	print(Ar)
 
ans = 1000000001
 
for i in range(n-k+1):
	h = Ar[i]
	l = Ar[i]
#	print("Start = ",i)
	for j in range(i,i+k+1):
#		print("check st",j)
		if h < Ar[j]:
			h = Ar[j]
		if l > Ar[j]:
			l = Ar[j]
	if ans > h-l:
		ans = h-l
#	print("diff=",ans,h,l)
 
print (ans)

Submission status: Wrong Answer

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

#print(n,k)
Ar = [0]*100001

for i in range(n):
#	print("i=",i)
	dat = int(input())
	Ar[i] = dat
#	print(Ar[i])

sum = 0
for i in range(k):
	sum = sum + Ar[i]

tmp = 0
for i in range(k,n):
#	print(i)
	if sum > sum - Ar[i-k] + Ar[k]:
		sum = sum - Ar[i-k] + Ar[k]

print (sum)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
a,b=map(int,input().split())
c=[0]*a
import sys
for i in range(a):
  c[i]=int(input())
for j in range(a):
  for z in range(j,a-1):
    if c[j]==c[z]:
      print(0)
      sys.exit()
print(max(c)-min(c))

Submission status: Wrong Answer

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

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

print(ans)

Submission status: Wrong Answer

Solution code:
# import bisect
# from collections import Counter, deque
# import copy
# from fractions import gcd
# from functools import reduce
# from itertools import accumulate, permutations, combinations, combinations_with_replacement, groupby, product
# import math
# import numpy as np
import sys
sys.setrecursionlimit(10 ** 5 + 10)
# input = sys.stdin.readline

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

    ans=10**5
    for i in range(N-K+1):
        ans=min(H[i+K-1]-H[i], ans)
    print(ans)
resolve()

Submission status: Wrong Answer

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

for i in range(N):
    A[i] = int(input())
    
A.sort()
print(A)

ans = A[K-1]-A[0]
for i in range(N-K):
    if A[K+i]-A[i+1] < ans:
        ans = A[K+i]-A[i+1]
        
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
N,K = map(int,input().split())
D = dict()
L = []
for i in range(N):
  a = int(input())
  L.append(a)
  if a not in D:
    D[a] = 0
  D[a] += 1  
for key in D:
  if D[key] == K:
    print(0)
    sys.exit(0)
L.sort()
print(L[K-1]-L[0])

Submission status: Wrong Answer

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

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] - h[0]
for j in range(0,k-n):
  if ans < h[j+k-1] - h[0]:
    ans = h[j+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 = sorted(h)
minH = 10000
for x in range(N - K + 1):
    sumh = H[x + K - 1] - H[x]
    if minH > sumh:
        minH = sumh
print(minH)

Submission status: Wrong Answer

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

print(h)
h_n = []
for i in range(0,n):
    l = h[i:i+k]
    if len(l) == k:
        h_n.append(l)

seed = max(h)
for i in range(0,len(h_n)):
    high = max(h_n[i]) - min(h_n[i])
    if seed > high:
        seed = high

print(seed)

Submission status: Wrong Answer

Solution code:
x,y=list(map(int, input().split()))
list=[]
for i in range(x):
	list.append(int(input()))
list.sort()
print(list)
ans=10**10

for j in range(x-y+1):
	if list[j+y-1]-list[j]<ans:
		ans=list[j+y-1]-list[j]
print(ans)

Submission status: Wrong Answer

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

H.sort()

min_diff = 10**5
for i in range(N-K+1):
    min_diff = min(min_diff, H[i+K-1] - H[i])
    
    # print('temp mindiff {}'.format(min_diff))
print(min_diff)

Submission status: Wrong Answer

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

H.sort()

min_diff = 10**5
for i in range(N-K+1):
    min_diff = min(max(H[i:i+K]) - min(H[i:i+K]), min_diff)
    # print('temp mindiff {}'.format(min_diff))
print(min_diff)

Submission status: Wrong Answer

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

h = sorted(h)
diff = [h[i + 1] - h[i] for i in range(n - 1)]
diff = sorted(diff)

ans = sum(diff[:k - 1])
print(ans)

Submission status: Wrong Answer

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

h = sorted(h)

left = h[:k]
right = h[-k:]

left_h = left[-1] - left[0]
right_h = right[-1] - right[0]

print(min(left_h, right_h))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
h = []
for i in range(n):
    h.append(int(input()))
#ここまで入力
h.sort()
mn = 10**5
for i in range(n-k+1):
    if mn > (h[i+k-1]-h[i]):
        mn = h[i+k-1]-h[i]
print(mn)

Submission status: Wrong Answer

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

h.sort()

for j in range(n-k+1):
    l.append(abs(max(h[j:j+3])-min(h[j:j+3])))

print(min(l))

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(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())
ab = []
for _ in range(n):
    a=int(input())
    ab.append(a)
ab.sort()
print(ab)
ans = 1000000000000000000000

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
from collections import Counter

N, K = list(map(int,input().split()))
H = []
for i in range(N):
    H.append(int(input()))
H = sorted(H,reverse = False)
C = Counter(H)
CH = list(C.values()) 

cnt = 0
for i in range(len(CH)):
    if CH[i] >= K:
        cnt += 1
        break
if cnt != 0:
    print('0')
else:
    print(H[K-1] - H[0])

Submission status: Wrong Answer

Solution code:
from collections import Counter

N, K = list(map(int,input().split()))
H = []
for i in range(N):
    H.append(int(input()))
H = sorted(H,reverse = False)
C = Counter(H)
CH = list(C.values()) 

cnt = 0
for i in range(len(CH)):
    if CH[i] >= K:
        cnt += 0
        break
if cnt != 0:
    print('0')
else:
    print(H[K-1] - H[0])

Submission status: Wrong Answer

Solution code:
from collections import Counter

N, K = list(map(int,input().split()))
H = []
for i in range(N):
    H.append(int(input()))
H = sorted(H,reverse = False)
C = Counter(H)
CH = list(C.values()) 

for i in range(N):
    if i >= K:
        print('0')
        break
    else:
        print(H[K-1]-H[0])
        break

Submission status: Wrong Answer

Solution code:
def main():
    N, K = (int(i) for i in input().split())
    H = [int(input()) for i in range(N)]
    ans = 10*12
    H.sort()
    for i in range(N-K+1):
        ans = min(ans, H[i+K-1] - H[i])
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    N, K = (int(i) for i in input().split())
    H = [int(input()) for i in range(N)]
    ans = 10*12
    H.sort()
    for i in range(N-K+1):
        ans = min(ans, abs(H[i+K-1] - H[i]))
    H = H[::-1]
    for i in range(N-K+1):
        ans = min(ans, abs(H[i+K-1] - H[i]))
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    N, K = (int(i) for i in input().split())
    H = [int(input()) for i in range(N)]
    H.sort()
    ans = 10*12
    # print(H)
    for i in range(N-K+1):
        # print(H[i], H[i+K-1])
        ans = min(ans, abs(H[i+K-1] - H[i]))
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    N, K = (int(i) for i in input().split())
    H = [int(input()) for i in range(N)]
    H.sort()
    ans = 10*12
    for i in range(N-K+1):
        ans = min(ans, H[i+K-1] - H[i])
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    N, K = (int(i) for i in input().split())
    H = [int(input()) for i in range(N)]
    H.sort()
    print(min(H[-1] - H[-K], H[K-1] - H[0]))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

x = []
for i in range(n-1):
    x.append(l[i+1] - l[i])
x  = sorted(x)

print(sum(x[0:k-1]))

Submission status: Wrong Answer

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

from itertools import combinations
x = list(combinations(l, 3))

y = []
for i in range(len(x)):
    y.append(max(x[i]) - min(x[i]))

print(min(y))

Submission status: Wrong Answer

Solution code:
import sys
import itertools

# import numpy as np

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N, K, *H = map(int, read().split())
H.sort()
print(min(H[i+K-1]-H[i]) for i in range(N-K+1))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
import numpy as np
n,k=map(int,input().split())
H=[int(input()) for i in range(n)]
H.sort()
H=np.array(H)
Hb,Ha=H[-k:],H[:k]
print(min(Hb-Ha))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import numpy as np

N, K = map(int, input().split())
h = [int(input()) for _ in range(N)]
h = np.array(sorted(h))
diff_h = np.zeros(N, dtype=np.int16)
diff_h[1:] = np.diff(h)
# print(h)
# print(diff_h)
cum_diff_h = np.cumsum(diff_h)
# print(cum_diff_h)
s = cum_diff_h[K - 1 :] - cum_diff_h[: -K + 1]
print(min(s))

Submission status: Wrong Answer

Solution code:
import numpy as np

N, K = map(int, input().split())
h = [int(input()) for _ in range(N)]
h = np.array(sorted(h))
diff_h = np.zeros(N, dtype=np.int16)
diff_h[1:] = np.diff(h)
print(h)
print(diff_h)
cum_diff_h = np.cumsum(diff_h)
print(cum_diff_h)
s = cum_diff_h[K - 1 :] - cum_diff_h[: -K + 1]
print(min(s))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
S = input().split(" ")
n = int(S[0])
k = int(S[1])

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

def calculate(n, k, arr):
    drr = []
    arr.sort()
    for i in range(1,n):
      drr.append(arr[i] - arr[i-1])

    s = 0
    j = 0
    sums = []
    for i in range(len(drr)- (k-1) + 1):
      while j < i + (k-1):
        s = s + drr[i]
        j = j + 1
      sums.append(s)
      s = s - drr[i]
    print(min(sums))
calculate(n, k, arr)

Submission status: Wrong Answer

Solution code:
S = input().split(" ")
n = int(S[0])
k = int(S[1])

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

def calculate(n, k, arr):
    drr = []
    arr.sort()
    for i in range(1,n):
      drr.append(arr[i] - arr[i-1])

    s = 0
    j = 0
    sums = []
    for i in range(n- (k-1)):
      while j < i + (k-1):
        s = s + drr[i]
        j = j + 1
      sums.append(s)
      s = s - drr[i]
    print(min(sums))
calculate(n, k, arr)

Submission status: Wrong Answer

Solution code:
S = input().split(" ")
n = int(S[0])
k = int(S[1])
arr = []
for i in range(n):
    arr.append(int(input()))

def calculate(n,k,arr):
    arr.sort()
    drr = []
    for i in range(1,n):
        drr.append(arr[i] - arr[i-1])
    drr.sort()
    drr = drr[:k-1]
    print(sum(drr))

calculate(n,k,arr)

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_sorted = sorted(h)

ans = 100000
for i in range(n-k+1):
    tmp = h_sorted[i+k-1] - h_sorted[i]
    ans = min(ans,tmp)

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()

def main():
    N, K = map(int, input().split())
    H = sorted([int(input()) for _ in range(N)], reverse=True)
    print(min([a - b for a, b in zip(H[:K], H[K-1:])]))

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
h = []
for _ in range(N):
    h_ = int(input())
    h.append(h_)
    
h.sort()
s = [0]*(N-2)
for i in range(N-2):
    s[i] = h[i+2] - h[i]
    
print(min(s))

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 = 10000000000000000
print(h)
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: Wrong Answer

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

H.sort()
ans = 100007
# print(H)
for i in range(0,len(H)-K+1):
	# print(H[i],H[i+K-1],len(H)-K-1)
	if ans >= H[i+K-1] - H[i]:
		ans =  H[i+K-1] - H[i]
		# print(ans)
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 = 100007
print(H)
for i in range(0,len(H)-K+1):
	# print(H[i],H[i+K-1],len(H)-K-1)
	if ans >= H[i+K-1] - H[i]:
		ans =  H[i+K-1] - H[i]
		# print(ans)
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys
input=sys.stdin.readline
n,k=map(int,input().split())
h=[0]*n
for i in range(n):
    h[i]=int(input())

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

Submission status: Wrong Answer

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

def one_int():
    return int(input())

def one_str():
    return input()

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

N,K = two_int()

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

sort_trees = sorted(trees)

mins = 10000000
for i in range(N-K+1):
    if sort_trees[i+K-1] - sort_trees[i] < mins:
        mins = sort_trees[i+K-1] - sort_trees[i]

print(mins)

Submission status: Wrong Answer

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

def one_int():
    return int(input())

def one_str():
    return input()

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

N,K = two_int()

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

sort_trees = sorted(trees)

mins = 10000000
for i in range(N-K+1):
    if abs(sort_trees[i] - sort_trees[i+K-1]) < mins:
        mins = abs(sort_trees[i] - sort_trees[i+K-1])

print(mins)

Submission status: Wrong Answer

Solution code:
N, K = map(int,input().split())
a = []
for i in range(N):
    a.append(int(input()))
a.sort()
ans = 10**9 + 7
for i in range(N-K):
    if ans > a[K-1+i] - a[i]:
        ans = a[K-1+i] - a[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 = 10**9
for i in range(0, n-2):
   x = [h[i], h[i+1], h[i+2]]
   if (max(x) - min(x) < ans):
      ans = max(x) - min(x)

print(ans)

Submission status: Wrong Answer

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

amari = n-k

if amari%2==0:
    left = amari//2+1
    right = -amari//2-1
    print(h[right] - h[left])

else:
    left1 = amari//2
    right1 = -amari//2 - 1
    left2 = amari//2 + 1
    right2 = -amari//2
    print(min((h[right1] - h[left1]), (h[right2] - h[left2])))

Submission status: Wrong Answer

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

h = sorted(h)
ans = []

for i in range(len(h)-K+1):
  ans.append(h[i] - h[i+K-1])
  if ans[-1] == 0:
    print(0)
    exit()

print(min(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()
min_number = 10**9+1
for i,j in zip(h[0:k],h[k-1:]):
    min_number = min(min_number,j-i)
print(min_number)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
import os
import itertools
import _collections
import string

class cin():
    def int():
        return int(sys.stdin.readline().rstrip())
    def string():
        return sys.stdin.readline().rstrip()
    def mapInt():
        return [int(x) for x in cin.string().split()]
    def stringList(n):
        return [input() for i in range(n)]
    def intListList(n):
        return [cin.mapInt() for i in range(n)]
    def intColsList(n):
        return [int(input()) for i in range(n)]

class Util():
    def roundUp(a,b):
        return -(-a // b)
    def toUpperMultiple(a,x):
        return Util.round_up(a,x) * x
    def toLowerMultiple(a,x):
        return (a // x) * x
class Math():
    def gcd(a,b):
        if b == 0:
            return a
        return Math.gcd(b,a % b)

def main():
    N,K = cin.mapInt()
    H = cin.intColsList(N)
    print(H)
    H.sort()
    ans = int(1e09)
    for i in range(N - K + 1):
        ans = min(ans,H[i + K - 1] - H[i])
    print(ans)
    return

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

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

minval = int(1e10)

#print(h)
for i in range(len(h)-k):
    if minval >= h[i+(k-1)] - h[i]:
        minval = h[i+(k-1)] - h[i]
print(minval)

Submission status: Wrong Answer

Solution code:
def main():
    N, K = map(int, input().split(" "))
    tree_height = []
    for _ in range(N):
        tree_height.append(int(input()))
    tree_height.sort()
    result = 10 ** 6
    for i in range(N - K + 1):
        result = min(result, tree_height[i + K - 1] - tree_height[i])
    print(result)


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(reverse=True)
ans=10**6

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

print(ans)

Submission status: Wrong Answer

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

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

print(ans)

Submission status: Wrong Answer

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

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

print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import combinations

N, K = [int(n) for n in input().split()]
h = [int(input()) for _ in range(N)]
ans = 0
for c in combinations(h, K):
    mc = max(c)-min(c)
    if ans < mc:
      ans = mc
print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import combinations

N, K = [int(n) for n in input().split()]
h = [int(input()) for _ in range(N)]
ans = []
for c in combinations(h, K):
    ans.append(c[K-1]-c[0])
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()
height = []
for i in range(n - 1):
    height.append(h[i + 1] - h[i])
height.sort()
ans = 0
for j in range(k - 1):
    ans += height[j]
print(ans)

Submission status: Wrong Answer

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

H_ = sorted(H)
H_[0] = abs(H_[0]-H_[1])
H_[-1] = abs(H_[-1]-H_[-2])
for i in range(1, n-1):
    H_[i] = min(abs(H[i]-H[i-1]), abs(H[i]-H[i+1]))

s = sum(H_)
for i in range(k, n-k):
    t = sum(H_[i:i+k])
    if s > t:
        s = t
        r = i
print(max(H[i:i+k])-min(H[i:i+k]))

Submission status: Wrong Answer

Solution code:
import sys
 
stdin = sys.stdin
 
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()
nas = lambda: stdin.readline().split()

n, k = na()
h = [ni() for i in range(n)]
h.sort()
d = [h[i] - h[i - 1] for i in range(1, n)]
d.sort()
ans = sum([d[i] for i in range(k - 1)])
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
 
stdin = sys.stdin
 
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()
nas = lambda: stdin.readline().split()

n, k = na()
h = [ni() for i in range(n)]
h.sort()
d = [h[i] - h[i - 1] for i in range(1, n)]
d.sort()
ans = 0
for i in range(k - 1):
    ans += d[i]
print(ans)

Submission status: Wrong Answer

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

A=sorted(A)

print(min(abs(A[0]-A[K-1]),abs(A[K*(-1)]-A[-1])))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
hn = []
for i in range(N):
  hn.append(int(input()))
hn = sorted(hn,reverse=True)
min = 99999999
for i in range(N-K+1):
  if min > (hn[i]-hn[K+i-1]):
    min = hn[i]-hn[K+i-1]
print(min)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
hn = []
for i in range(N):
  hn.append(int(input()))
hn = sorted(hn,reverse=True)
min = 99999999
for i in range(N-K+1):
  print("hn[i] is {} , hn[K+i] is {}".format(hn[i],hn[K+i-1]))
  if min > (hn[i]-hn[K+i-1]):
    min = hn[i]-hn[K+i-1]
print(min)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
hs = sorted([int(input()) for _ in range(N)])[::-1]
# N, K = 5, 3
# hs = [15, 14, 12, 11, 10]

min_diff = 10 ** 10
for i in range(0, N-K+1):
    tmp = hs[i:i+1]
    min_diff = min(min_diff, tmp[0] - tmp[-1])

print(min_diff)

Submission status: Wrong Answer

Solution code:
a,b= map(int, input().split())
l=[int(input()) for i in range(a)]
l.sort()
minans=100
for i in range(a-b):
  ans=l[i+b-1]-l[i]
  minans=min(ans,minans)
print(minans)

Submission status: Wrong Answer

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

h.sort()

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

print(min(d))

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

Submission status: Wrong Answer

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

ans=10**9

for i in range(N-2):
    if ans>h[i+2]-h[i]:
        ans=h[i+2]-h[i]
    else:
        continue
print(ans)

Submission status: Wrong Answer

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

ans=10**9

for i in range(N-2):
    if ans>h[i+2]-h[i]:
        ans=h[i+2]-h[i]
    else:
        continue
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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


#わかんない

Submission status: Wrong Answer

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

trees = []
for _ in range(0, n):
  trees.append(int(input()))
trees.sort()
trees.reverse()
ans = 100000000000
for i in range(0, n-k):
  ans = min(ans, trees[i] - trees[i+k-1])
print(ans)

Submission status: Wrong Answer

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

trees = []
for _ in range(0, n):
  trees.append(int(input()))
trees.sort()
trees.reverse()
print(trees[0] - trees[n-k])

Submission status: Wrong Answer

Solution code:
# ABC115 C - Christmas Eve

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

h = sorted(h)
ans = 9999999999
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 = []
for i in range(N):
    H.append(input())
H =sorted(H)

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

print(ans)

Submission status: Wrong Answer

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

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