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:
from statistics import mean, median,variance,stdev
import sys
import math

x = input().split()
#y = input().split()


#y = int(input())
#x = int(input())
a = []
b = int(x[0])
num = int(x[1])
for i in range(b):
    a.append(int(input()))
a = sorted(a)
min = a[num-1]-a[0]
for i in range(num,b):
    if min > a[i]-a[i-num]:
        min = a[i]-a[i-num]
if min > a[b-1]-a[b-num]:
    min = a[b-1]-a[b-num]
print(min)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

l.sort()
min=l[n-1]
for i in range(n-k):
	ini = l[k+i-1]-l[i]
	min = ini if min > ini else min
	
print ini

Submission status: Wrong Answer

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

h_diff = 99999999
h.sort()
for i in range(N-K+1):
    if h_diff > h[i+K-1] - h[i]:
        h_diff = h[i+K-1] - h[i]

print(h_diff)

Submission status: Wrong Answer

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

l.sort()
min=sum(l[:k])
ini=min
for i in range(n-k):
	ini = ini + l[k+i]-l[i]
	min = ini if min > ini else min
	
print ini

Submission status: Wrong Answer

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

@author: Owner
"""

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

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

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

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

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

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


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

h.sort()
diff_min = 10^(9)
#print(h)
for i in range(len(h)-K+1):
    #print(h[i], h[i+K-1])
    temp_diff = h[i+K-1] - h[i]
    
    if diff_min > temp_diff:
        diff_min = temp_diff
    if diff_min == 0:
        break
"""
for l in list(itertools.combinations(h,K)):
    
    temp_diff = l[np.argmax(l)] - l[np.argmin(l)]
    if diff_min > temp_diff:
        diff_min = temp_diff
    if diff_min == 0:
        break
"""     
print(diff_min)

Submission status: Wrong Answer

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

left = 0
right = n-1
ans = 1e9
while right - left + 1 > k:
    if hs[left+1] - hs[left] >= hs[right] - hs[right-1]:
        left += 1
    else:
        right -= 1

print(hs[right] - hs[left])

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

a.sort()
print(a)

print(min([a[i+K-1]-a[i] for i in range(N-K+1)]))

Submission status: Wrong Answer

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

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

s = sum(h)
mean = s / N
def calc_diff(n):
    return n - mean

h_smean = map(calc_diff, h)

h_sorted = sorted(h_smean, reverse=False)
diff_h = np.diff(h_sorted, n=1, axis=-1)
diff_h_sorted = sorted(diff_h, reverse=False)

print(int(sum(diff_h_sorted[0:K-1])))

Submission status: Wrong Answer

Solution code:
import collections
import numpy as np

def conv(cc, ks, K):
    if np.convolve(cc, np.ones(ks+1), mode = 'valid').max()>=K:
        return ks
    else:
        return conv(cc, ks+1, K)

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

cc=np.array(list(collections.Counter(h).values()))

ks=int((K-collections.Counter(h).most_common()[0][1])/2)
print(conv(cc,ks,K))

Submission status: Wrong Answer

Solution code:
import collections


N, K = map(int, input().split())
trees = [int(input()) for i in range(N)]
h_max = max(trees)
h_min = min(trees)
hights = collections.Counter(trees)

min_diff = h_min
for hight, count in hights.most_common():
    if count >= min_diff:
        break

    for start in range(max([hight - 100, h_min]), min([hight + 100, h_max + 1])):
        num = 0
        diff = -1
        for i in range(h_max - start + 1):
            diff += 1
            num += hights.get(start + i, 0)
            if num >= K:
                break

        if num >= K and i < min_diff:
            min_diff = i

print(min_diff)

Submission status: Wrong Answer

Solution code:
import math

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

crr_min = 10^9

for i in range(0,math.ceil((n-k+1)/2)):
    tmp1 = abs(h[i+k-1] - h[i])
    tmp2 = abs(h[-i] - h[1-i-k])
    if tmp1==0 or tmp2==0:
        crr_min=0
        break

    if tmp1<crr_min:
        crr_min=tmp1

    if tmp2<crr_min:
        crr_min=tmp2


print(crr_min)

Submission status: Wrong Answer

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

#ans=10000000000
ans = max(list1)-min(list1)
for i in range(k,n):
  if min(list1) <= h[i] <= max(list1):
    list1.append(h[i])
    list1.sort()
    if list1[-2]-min(list1) < ans:
      list1.pop(-1)
    if  min(list1)-list1[1] < ans:
      list1.pop(0)

print(ans)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a = [int(input()) for i in range(n)]
b = [a[i+1]-a[i] for i in range(n-1)]
c = []
p = 0
m = 0
for i in range(n-k+1):
 for j in range(k-1):
  p += b[j+m]
 c.append(p)
 m += 1
 p = 0
print(min(c))

Submission status: Wrong Answer

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

@author: Owner
"""

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

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

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

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

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

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


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

h.sort()
diff_min = 10^(9)

for i in range(len(h)-K+1):
    temp_diff = h[i+K-1] - h[i]
    
    if diff_min > temp_diff:
        diff_min = temp_diff
    if diff_min == 0:
        break
"""
for l in list(itertools.combinations(h,K)):
    
    temp_diff = l[np.argmax(l)] - l[np.argmin(l)]
    if diff_min > temp_diff:
        diff_min = temp_diff
    if diff_min == 0:
        break
"""     
print(diff_min)

Submission status: Wrong Answer

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

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

h_sorted = sorted(h, reverse=False)
diff_h = np.diff(h_sorted, n=1, axis=-1)
diff_h_sorted = sorted(diff_h, reverse=False)

print(sum(diff_h_sorted[0:K-1]))

Submission status: Wrong Answer

Solution code:
import collections

def conv(cc, ks, K):
    tm=max([sum(cc[j:j+(ks+1)]) for j in range(len(cc)-(ks-1))])
    if tm>=K:
        return ks
    else:
        return conv(cc, ks+1, K)

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

cc=list(collections.Counter(h).values())

ks=int((K-collections.Counter(h).most_common()[0][1])/2)
print(conv(cc,ks,K))

Submission status: Wrong Answer

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

for i in range(N):
    a.append(int(input()))
for i in range(N):
    for j in range(i):
        if i != j:
            b.append(abs(a[i] - a[j]))
b = sorted(b)
print(b[K])

Submission status: Wrong Answer

Solution code:
import numpy as np
import copy

candidate = 100000000
N,K = list(map(int, input().split()))
h = [int(input()) for i in range(N)]
for i in range(N-K+1):
    hcp = copy.copy(h)
    diff = 0
    choose_h =[]
    choose_h.append(hcp.pop(i))

    for j in range(K-1):
        choose_h.append(hcp.pop(np.abs(np.asarray(hcp) - h[i]).argmin()))
    diff = max(choose_h)-min(choose_h)
    if(diff < candidate):
        candidate = diff
print(candidate)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
h=sorted(list(int(input()) for i in range(n)))
h+=h
mi=float('inf')
ansma=0
ansmi=0
for i in range(n-k):
    tmp=h[i:i+k]
    tmpma=max(tmp)
    tmpmi=min(tmp)
    if i == n:
        break
    if mi > tmpma - tmpmi:
        mi=tmpma-tmpmi
        ansma=tmpma
        ansmi=tmpmi
 
print(ansma-ansmi)

Submission status: Wrong Answer

Solution code:
import collections

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

h = sorted(h)
c = collections.Counter(h)
flg = False
for i in c:
    if c[i] >= 3:
        print(0)
        flg = True
        break

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

Submission status: Wrong Answer

Solution code:
line = input().split(" ")
n = int(line[0])
k = int(line[1])
h = []
sa = 999


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

h = sorted(h)

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

print(sa)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
h=sorted(list(int(input()) for i in range(n)))
h+=h
mi=float('inf')
ansma=0
ansmi=0
for i in range(n-k-1):
    tmp=h[i:i+k]
    tmpma=max(tmp)
    tmpmi=min(tmp)
    if i == n:
        break
    if mi > tmpma - tmpmi:
        mi=tmpma-tmpmi
        ansma=tmpma
        ansmi=tmpmi
 
print(ansma-ansmi)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
tree = [int(n) for n in range(N)]
diff = 10 ** 9
tree.sort()
for i in range(N - (K - 1)):
    if tree[i] == tree[i + 1] == tree[i + 2]:
        diff = 0
        break
    elif tree[i + K - 1] - tree[i] < diff:
        diff = tree[i + K - 1] - tree[i]
print(diff)

Submission status: Wrong Answer

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

#ans=10000000000
ans = max(list1)-min(list1)
for i in range(k,n):
  if min(list1) < h[i] < max(list1):
    list1.append(h[i])
    list1.sort()
    if list1[-2]-min(list1) < ans:
      list1.pop(-1)
    if  min(list1)-list1[1] < ans:
      list1.pop(0)

print(ans)

Submission status: Wrong Answer

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

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

ans=sys.maxsize
flag=0
temp = []

for i in range(len(trees)-k+1):
  for roop in range(k-1):
    temp.append(trees[i+roop])
  if(flag==1):
    break
  for j in range(i+k-1,len(trees)):
      temp.append(trees[j])
      diff = max(temp) - min(temp)
      if(diff==0):
        ans=0
        flag=1
        break
      if(ans>diff):
        ans=diff
      del temp[k-1]
  temp.clear()

print(ans)

Submission status: Wrong Answer

Solution code:
import collections

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

h = sorted(h)
c = collections.Counter(h)
flg = False
for i in c:
    if c[i] >= 3:
        print(0)
        flg = True
        break

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

Submission status: Wrong Answer

Solution code:
n=input().split()
n=[int(i) for i in n]
h=[0]*n[0]
for i in range(n[0]):
    h[i]=int(input())
h.sort()
print(h)
q=-1
for i in range(0,len(h)-n[1]+1):
    a=h[i+n[1]-1]-h[i]
    print(i+n[1]-1,i)
    if q==-1 or q>a:
        q=a
print(q)

Submission status: Wrong Answer

Solution code:
import collections

def conv(cc, ks, K):
    tm=max([sum(cc[j:j+(ks+1)]) for j in range(len(cc)-(ks-1))])
    if tm>=K:
        return ks
    else:
        return conv(cc, ks+1, K)

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

cc=list(collections.Counter(h).values())

print(conv(cc,0,K))

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
h=sorted(list(int(input()) for i in range(n)))
h+=h
mi=float('inf')
ansma=0
ansmi=0
a=0
for i in range(n-2):
    tmpma=h[i+k-1]
    tmpmi=h[i]
    if n <= i:
        break
    if mi > tmpma - tmpmi:
        a=i
        mi=tmpma-tmpmi

print(h[a+k-1]-h[a])

Submission status: Wrong Answer

Solution code:
import numpy as np
import copy

candidate = 100000000
N,K = list(map(int, input().split()))
h = [int(input()) for i in range(N)]

for i in range(N-K+1):
    hcp = copy.copy(h)
    hcp.pop(i)
    diff = 0
    for j in range(K-1):
        choose_h = hcp.pop(np.abs(np.asarray(hcp) - h[i]).argmin())
        diff += abs(h[i]-choose_h)
    if(diff < candidate):
        candidate = diff
print(candidate)

Submission status: Wrong Answer

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

q=-1
for i in range(len(h)-n[1]):
    a=h[i+n[1]-2]-h[i]
    if q==-1 or q>a:
        q=a
print(q)

Submission status: Wrong Answer

Solution code:
import collections

def conv(cc, ks, K):
    tm=max([sum(cc[j:j+(ks+1)]) for j in range(len(cc)-(ks-1))])
    if tm<K:
        return conv(cc, ks+1, K)
    else:
        return ks

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

cc=list(collections.Counter(h).values())

print(conv(cc,0,K))

Submission status: Wrong Answer

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

q=-1
for i in range(len(h)-n[1]):
    a=h[i+n[1]-1]-h[i]
    if q==-1 or q>a:
        q=a
print(q)

Submission status: Wrong Answer

Solution code:
import collections

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

h = sorted(h)
c = collections.Counter(h)
flg = False
for i in c:
    if c[i] >= 3:
        print(0)
        flg = True
        break

if flg == False:
    print(min(abs(h[n-1] - h[n-k-1]),abs(h[0] - h[k-1])))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

trees = sorted(trees,reverse=True)
print(trees)
average = sum(trees)/len(trees)
min_dist = 1e+10
if abs(max(trees) - average) > abs(min(trees) - average):
    for i in range(n)[k-1:]:
        print(i)
        if min_dist > abs(trees[i] - trees[i-k+1]):

            min_dist = abs(trees[i] - trees[i-k+1])
else:
    for i in range(n-k+1):
        if min_dist > abs(trees[i+k-1] - trees[i]):
            min_dist = abs(trees[i+k-1] - trees[i])
print(min_dist)

Submission status: Wrong Answer

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

if list(np.diff(hs)) == list(np.zeros(n-1, dtype=int)):
    print('0')
else:
    sorted_hs = np.sort(hs)
    try:
        print(min([min(np.diff(sorted_hs[i::k-1])) for i in range(k)]))
    except:
        print('0')

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
h=sorted(list(int(input()) for i in range(n)))
h+=h
mi=float('inf')
ansma=0
ansmi=0
a=0
for i in range(n):
    tmpma=h[i+k]
    tmpmi=h[i]
    if n <= i:
        break
    if mi > tmpma - tmpmi:
        a=i
        mi=tmpma-tmpmi

print(h[a]-h[a+k])

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
in_t = input().split()
n = int(in_t[0])
k = int(in_t[1])
list = []
for i in range(k):
 list.append(int(input()))
for i in range(n - k):
 list.append(int(input()))
 list.sort()
 diff_max = list[k-1] - list[k-2]
 diff_min = list[1] - list[2]
 if (diff_max > diff_min):
  list.remove(list[0])
 else:
   list.remove(list[k-1])
print(list[k-1] - list[0])

Submission status: Wrong Answer

Solution code:
N, K= map(int,input().split())
l=[]
kekka=[]
sin=[]
hu=[]
sei=[]
cunt=0
for i in range(N):
    a=int(input())
    l.append(a)
l=sorted(l)
if len(l)%2==1:
    center=l[len(l)//2]
else:
    center=((l[len(l)//2-1])+(l[len(l)//2]))//2
for i in range(len(l)):
    a=l[i]-center
    kekka.append(a)
for i in range(len(l)):
        if kekka[i]<0:
               hu.append(kekka[i])
        else:
            sei.append(kekka[i])
#print(center)
#print(sei)
#print(hu)
while K!=cunt:
    if abs(hu[-1])<=abs(sei[0]):
            sin.append(hu[-1])
            hu.remove(hu[-1])
            cunt+=1
    else:
        sin.append(sei[0])
        sei.remove(sei[0])
        cunt+=1
sin=sorted(sin)
#print(sin)
print(sin[-1]-sin[0])
        
    
        
#print(center)

Submission status: Wrong Answer

Solution code:
import collections

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

h = sorted(h)
c = collections.Counter(h)
flg = False
for i in c:
    if c[i] >= 3:
        print(0)
        flg = True
        break

if flg == False:
    print(abs(h[0] - h[k-1]))

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
h=sorted(list(int(input()) for i in range(n)))
h+=h
mi=float('inf')
ansma=0
ansmi=0
a=0
for i in range(int(len(h)/2)):
    tmpma=h[i+k]
    tmpmi=h[i]
    if mi > max(tmpma,tmpmi) - min(tmpma,tmpmi):
        a=i
        mi=tmpma-tmpmi

print(h[a]-h[a+k])

Submission status: Wrong Answer

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

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

li.sort()

co = K - 1
hmin = 10000
for i in range(N-K+1):
        if hmin > li[i+co] - li[i]:
                hmin = li[i+co] - li[i]
                if hmin == 0:
                        break
print(hmin)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
import itertools
N,K = map(int,input().split())
h = [input() for i in range(N)]
def f(x):
    return int(max(x)) - int(min(x))
l = list(map(f, itertools.combinations(h, K)))
print(min(l))

Submission status: Wrong Answer

Solution code:
from collections import deque

n,k = map(int,input().split())
hs = sorted([int(input()) for _ in range(n)])

section = deque(hs[:k])
ans = hs[k-1] - hs[0]

for i in range(k,n):
    target = hs[k]
    diff = target - section[1]
    if ans >= diff:
        ans = diff
        section.popleft()
        section.append(target)

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

p.sort()

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

print(m)

Submission status: Wrong Answer

Solution code:
a, b = (int(i) for i in input().split()) 

list=[]
 
for i in range(a):
    list.append(int(input()))

new_list = sorted(list)

sabun = None

for i in range(len(new_list)-1):

    if i+b == len(new_list)-1:
        break

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

    if sabun == None:
        sabun = bbb
        continue

    if sabun > bbb:
        sabun = bbb
    
print(sabun)

Submission status: Wrong Answer

Solution code:
from collections import Counter
n,m=map(int,input().split())
tree=[]
tree2=[]
for i in range(n):
    tree.append(int(input()))
heikin=int(sum(tree)/len(tree))
c=Counter(tree)
saihin=c.most_common(1)[0][0]
for i in range(n):
    tree2.append(tree[i]-saihin)
    tree[i]=tree[i]-heikin
tree.sort()
tree.reverse()
tree2.sort()
tree2.reverse()
a1=tree[1]-tree[m-1]
a2=tree2[1]-tree2[m-1]
if a1>a2:
    print(a2)
else:
    print(a1)

Submission status: Wrong Answer

Solution code:
a, b = (int(i) for i in input().split()) 

list=[]
 
for i in range(a):
    list.append(int(input()))

new_list = sorted(list)

sabun = None

for i in range(len(new_list)-1):

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

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

    if sabun == None:
        sabun = bbb
        continue

    if sabun > bbb:
        sabun = bbb
    
print(sabun)

Submission status: Wrong Answer

Solution code:


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

Submission status: Wrong Answer

Solution code:
b,c = map(int, input().split())
a = []
while True:
    try:
        tmp = int(input())
        a.append(tmp)
    except EOFError:
        break
a = sorted(a)
min_diff = a[c-1] - a[0]
max_diff = a[-1] - a[-1-1*(c-1)]
print(min(min_diff,max_diff))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
candidate = 100000000
N,K = list(map(int, input().split()))
h = [int(input()) for i in range(N)]
h.sort()
for i in range(N-K+1):
    diff = 0
    for j in range(K-1):
        diff += abs(h[j+i]-h[j+i+1])
    if(diff < candidate):
        candidate = diff
print(candidate)

Submission status: Wrong Answer

Solution code:

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

if __name__ == '__main__':
  solve()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

print(min_l)

Submission status: Wrong Answer

Solution code:
import collections

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

else:
  A = sorted(S, reverse=True)
  for i in range(len(A)-W+1):
    if sa > A[i]-A[i+W-1]:
      sa = A[i]-A[i+W-1]
  
    if sa == 0:
      break

  print(sa)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
b,c = map(int, input().split())
a = []
while True:
    try:
        tmp = int(input())
        a.append(tmp)
    except EOFError:
        break
b = sorted(a)
min_diff = b[c-1] - b[0]
max_diff = b[-1] - b[-1-1*(c-1)]
print(min(min_diff,max_diff))

Submission status: Wrong Answer

Solution code:
import collections

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

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



def eva(L):
    h_l = min(L)
    h_h = max(L)
    return h_h -h_l

candidate = []
result = eva(P[:3])

def dfs(now_l,k,l):
    global result
    print(now_l)
    if len(now_l)== k :
        f = eva(now_l)
        if f < result :
            result = f
        return 
    for i in range(len(l)) :
        dfs(now_l+[l[i]], k,l[i+1:])
        if len(now_l) == k-1 :
            break
    return 

if any([i>= K for i in collections.Counter(P).values()]) :
    print(0)
else :
    dfs([],K, P) 
    print(result)

Submission status: Wrong Answer

Solution code:
import collections
n,k = map(int, input().split())
h = [int(input()) for _ in range(n)]
h.sort()
c = collections.Counter(h).most_common()
# print(c)
ans = []
if c[0][1] >= k:
    print(0)
    exit()
for i in range(len(c)):
    count = c[i][1]
    tmp = [c[i][0]]
    for j in range(i + 1, len(c)):
        count+=c[j][1]
        tmp.append(c[j][0])
        if count >= k:
            break
    else:
        tmp.clear()
    if tmp:
        # print(tmp)
        ans.append(tmp[-1] - tmp[0])
print(min(ans))

Submission status: Wrong Answer

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

h = sorted(h)

ans = max(h[0:3]) - min(h[0:3])
for i in range(1, n-2):
    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 = []
for i in range(n):
    l.append(int(input()))
l.sort()
#print(l)
m = 10000000
for i in range(n-k+1):
#    print(str(l[i+k-1])+":"+str(l[i]))
    a = l[i+k-1]-l[i]
    if m > a:
        m = a
print(m)

Submission status: Wrong Answer

Solution code:
import collections

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

h = sorted(h)
c = collections.Counter(h)
flg = False
for i in c:
    if c[i] >= 3:
        print(0)
        flg = True
        break

if flg == False:
    print(abs(h[0] - h[2]))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
tree=[]
for i in range(n):
    tree.append(int(input()))
heikin=int(sum(tree)/len(tree))
for i in range(n):
    tree[i]=tree[i]-heikin
tree.sort()
tree.reverse()
print(abs(tree[1]-tree[m-1]))

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
tree=[]
for i in range(n):
    tree.append(int(input()))
heikin=int(sum(tree)/len(tree))
for i in range(n):
    tree[i]=tree[i]-heikin
tree.sort()
tree.reverse()
print(tree)
print(abs(tree[1]-tree[m-1]))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
import itertools,math
n=input().split()
n=[int(i) for i in n]
h=[]
for i in range(n[0]):
    h.append(int(input()))

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import numpy as np

N, K = map(int, input().split())
A = []
for i in range(N):
    N = int(input())
    A.append(N)
    
def getNearestValue(A, num):
    idx = np.abs(np.asarray(A) - num).argmin()
    return A[idx]
    
sorted_A = sorted(A)
if len(sorted_A) % 2 == 0:
    a  = int(len(sorted_A) / 2)
    chu = int((sorted_A[a] + sorted_A[a - 1]) / 2)
else:
    a = int(len(sorted_A) / 2)
    chu = sorted_A[a]

L = []
for i in range(K):
    near = getNearestValue(A, chu)
    L.append(near)
    A.remove(near)
    
print(max(L) - min(L))

Submission status: Wrong Answer

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

P = sorted([int(input()) for i in range(N)])
def eva(L):
    h_l = min(L)
    h_h = max(L)
    return h_h -h_l

candidate = []
result = eva(P[:3])
print(result)
def dfs(now_l,k,l):
    global result
    if len(now_l)== k :
        f = eva(now_l)
        if f < result :
            result = f
        return 
    for i in range(len(l)) :
        dfs(now_l+[l[i]], k,l[i+1:])
        if len(now_l) == k-1 :
            break
    return 
dfs([],K, P)