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:
# Christmas Eve

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

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

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

Submission status: Wrong Answer

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

m = float('inf')

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

print(m)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
""" Atcoder """

import sys
input = sys.stdin.readline

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

H.sort()

min_h = 1000000000

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

Submission status: Wrong Answer

Solution code:
N,K=(int(i) for i in input().split())
hlist=[int(input()) for i in range(N)]
hlist.sort()
print(hlist)
dif=[]
for j in range(N-K+1):
    dif.append(hlist[j+K-1]-hlist[j])
print(min(dif))

Submission status: Wrong Answer

Solution code:
N,K=(int(i) for i in input().split())
hlist=[int(input()) for i in range(N)]
hlist.sort()
print(hlist)
dif=[]
for j in range(N-K):
    dif.append(hlist[j+K-1]-hlist[j])
print(min(dif))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

h_list.sort()
answer = h_list[N-1] - h_list[0]

for i in range(N-K+1):
    a = h_list[i+K-2] - h_list[i]
    answer = min(a,answer)

print(answer)

Submission status: Wrong Answer

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

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

cand = [0]*(n-k+1)
cand[0] = h[k-1] - h[0]


for j in range(1,n-k+1):
    cand[j] = cand[j-1] + h[k+j-1] - h[j]

print(min(cand))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Wed Jan  2 09:59:01 2019

@author: Yamazaki Kenichi
"""

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

h.sort()

ans = h[K-1] - h[0]

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

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Wed Jan  2 09:59:01 2019

@author: Yamazaki Kenichi
"""

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

h.sort()

ans = h[K-1] - h[0]

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

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Wed Jan  2 09:59:01 2019

@author: Yamazaki Kenichi
"""

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

h.sort()

ans = h[K-1] - h[0]

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

Submission status: Wrong Answer

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

import numpy as np
dlist = np.diff(np.sort(hlist))

ans = []
for i in range(len(dlist)-1):
    ans.append(sum(dlist[i:i+len(dlist)-2]))
print(min(ans))

Submission status: Wrong Answer

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

import numpy as np
dlist = np.diff(np.sort(hlist))

ans = []
for i in range(len(dlist)-1):
    ans.append(dlist[i]+dlist[i+1])
print(min(ans))

Submission status: Wrong Answer

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

H.sort()
res = 10000000000
for i in range(N-K+1):
    print(H[i:i+K])
    res = min(res, max(H[i:i+K]) - min(H[i:i+K]))
print(res)

Submission status: Wrong Answer

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

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

h_array.sort()

#最小値を更新していくので、初期値を無限として設定しておく
result = float("inf")
#indexに注意
for i in range(K - 1, N):
    result = min(result, h_array[i] - h_array[i - K + 1])

print(result)

Submission status: Wrong Answer

Solution code:
from collections import Counter
n,k=map(int,input().split())
l=[]
for i in range(n):
    l.append(int(input()))
l.sort()
d=Counter(l)
for i in d:
    if(d[i]>=k):
        print(0)
        exit()
print(l[k-1]-l[0])

Submission status: Wrong Answer

Solution code:
N,K=map(int,raw_input().split())
P=[]
minus=0
save=0

for i in range(N):
  P.append(raw_input())
  
P.sort();
K-=1;

if K>=0:
  minus=int(P[K])-int(P[0])

for i in range(N):
  if i+K>=N:
    break
    
  if minus>int(P[i+K])-int(P[i]):
    minus=int(P[i+K])-int(P[i])
  
print minus

Submission status: Wrong Answer

Solution code:
N,K=map(int,raw_input().split())
P=[]
minus=1000000
save=0

for i in range(N):
  P.append(raw_input())
  
P.sort();
K-=1;

for i in range(N):
  if i+K>=N:
    break
    
  if minus>int(P[i+K])-int(P[i]):
    minus=int(P[i+K])-int(P[i])
  
print minus

Submission status: Wrong Answer

Solution code:
N,K=map(int,raw_input().split())
P=[]
minus=1000000
save=0

for i in range(N):
  P.append(raw_input())
  
P.sort();
K-=1;

for i in range(N):
  if i+K>N-1:
    break
  save=int(P[i+K])-int(P[i])
  if minus>save:
    minus=save
  i+=K
  
print minus

Submission status: Wrong Answer

Solution code:
N,K=map(int,raw_input().split())
P=[]
minus=1000000
save=0

for i in range(N):
  P.append(raw_input())
  
P.sort();
K-=1;

for i in range(N):
  if i+K>=N:
    break
  save=int(P[i+K])-int(P[i])
  if minus>save:
    minus=save
  
print minus

Submission status: Wrong Answer

Solution code:
N,K=map(int,raw_input().split())
P=[]
minus=100
save=0

for i in range(N):
  P.append(raw_input())
  
P.sort();
K-=1;

for i in range(N):
  if i+K>=N:
    break
  save=int(P[i+K])-int(P[i])
  if minus>save:
    minus=save
  
print minus

Submission status: Wrong Answer

Solution code:
a,b= map(int,input().split(" "))
ar = []
for i in range(a):
    l = int(input())
    ar.append(l)
ar.sort()
c = 10 * 9 + 7
for i in range((a-b)+1):
    if c > ar[(i+b)-1] - ar[i]:
        c = ar[(i+b)-1] - ar[i]
print(c)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
h = sorted([int(input()) for _ in range(n)], reverse=True)
u = []
for i, v in enumerate(h[:-2]):
    a = h[i:i+3]
    u.append(max(a) - min(a))
print(min(u))

Submission status: Wrong Answer

Solution code:

# coding: utf-8

# In[33]:


"""問題文

とある世界では、今日はクリスマスイブです。

高羽氏の庭には N
本の木が植えられています。i 本目の木 (1≤i≤N) の高さは hi

メートルです。

彼は、これらの木のうち K

本を選んで電飾を施すことにしました。より美しい光景をつくるために、できるだけ近い高さの木を飾り付けたいです。

より具体的には、飾り付ける木のうち最も高いものの高さを hmax
メートル、最も低いものの高さを hmin メートルとすると、hmax−hmin が小さいほど好ましいです。hmax−hmin

は最小でいくつにすることができるでしょうか？
制約

    2≤K<N≤105

1≤hi≤109
hi

    は整数である。

入力

入力は以下の形式で標準入力から与えられる。

N

 K


h1


h2


:


hN


出力

hmax−hmin

としてありうる最小の値を出力せよ。
入力例 1
Copy

5 3
10
15
11
14
12

出力例 1
Copy

2
"""


# In[45]:


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


# In[46]:


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


# In[49]:


p.sort()


# In[54]:


min_val=abs(p[0]-p[K-1])

for i in range(N-K):
    diff=abs(p[i+K-1]-p[i])
    #print(diff)
    if diff<min_val:
        min_val=diff
print(min_val)

Submission status: Wrong Answer

Solution code:

# coding: utf-8

# In[33]:


"""問題文

とある世界では、今日はクリスマスイブです。

高羽氏の庭には N
本の木が植えられています。i 本目の木 (1≤i≤N) の高さは hi

メートルです。

彼は、これらの木のうち K

本を選んで電飾を施すことにしました。より美しい光景をつくるために、できるだけ近い高さの木を飾り付けたいです。

より具体的には、飾り付ける木のうち最も高いものの高さを hmax
メートル、最も低いものの高さを hmin メートルとすると、hmax−hmin が小さいほど好ましいです。hmax−hmin

は最小でいくつにすることができるでしょうか？
制約

    2≤K<N≤105

1≤hi≤109
hi

    は整数である。

入力

入力は以下の形式で標準入力から与えられる。

N

 K


h1


h2


:


hN


出力

hmax−hmin

としてありうる最小の値を出力せよ。
入力例 1
Copy

5 3
10
15
11
14
12

出力例 1
Copy

2
"""


# In[45]:


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


# In[46]:


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


# In[49]:


p.sort()
p


# In[54]:


min_val=abs(p[0]-p[K-1])

for i in range(N-K):
    diff=abs(p[i+K-1]-p[i])
    #print(diff)
    if diff<min_val:
        min_val=diff
print(min_val)

Submission status: Wrong Answer

Solution code:

# coding: utf-8

# In[33]:


"""問題文

とある世界では、今日はクリスマスイブです。

高羽氏の庭には N
本の木が植えられています。i 本目の木 (1≤i≤N) の高さは hi

メートルです。

彼は、これらの木のうち K

本を選んで電飾を施すことにしました。より美しい光景をつくるために、できるだけ近い高さの木を飾り付けたいです。

より具体的には、飾り付ける木のうち最も高いものの高さを hmax
メートル、最も低いものの高さを hmin メートルとすると、hmax−hmin が小さいほど好ましいです。hmax−hmin

は最小でいくつにすることができるでしょうか？
制約

    2≤K<N≤105

1≤hi≤109
hi

    は整数である。

入力

入力は以下の形式で標準入力から与えられる。

N

 K


h1


h2


:


hN


出力

hmax−hmin

としてありうる最小の値を出力せよ。
入力例 1
Copy

5 3
10
15
11
14
12

出力例 1
Copy

2
"""


# In[35]:


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


# In[36]:


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


# In[44]:


min_val=abs(p[0]-p[1])
for i in range(N):
    for j in range(N):
        if i!=j:
            if abs(p[i]-p[j])<min_val:
                min_val=abs(p[i]-p[j])
print(min_val)

Submission status: Wrong Answer

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

m = 10e9
for i in range(N-K):
    dh = h[K-1+i]-h[0+i]
    m = min(m, dh)

print(m)

Submission status: Wrong Answer

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

h.sort()
shortest_index_from = N
shortest_diff = N
for i in range(N-K+1):
    temp_diff = h[i+K-1] - h[i]
    #print(str(h[i+K-1]) + ", " + str(h[i]))
    if temp_diff < shortest_diff:
        shortest_index_from = i
        shortest_diff = temp_diff
        #print("index=" + str(i) + " diff=" + str(temp_diff))
print(shortest_diff)

Submission status: Wrong Answer

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

h.sort()
print(h)
l = []
for i in range(N-K+1):
    l.append(h[i+K-1] - h[i])
print(min(l))

Submission status: Wrong Answer

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

list2=list()
h.sort()


for i in range(N-K):
    list2.append(h[i+K-1]-h[i])
    continue
print(min(list2))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
h_list = [None] * N
for n in range(N):
    h_list[n] = int(input())

h_list = list(sorted(h_list))

min_diff = 10 ** 6
for i in range(N):
    if K - 1 + i >= N:
        break
    diff = h_list[K - 1 + i] - h_list[i]
    if diff < min_diff:
        min_diff = 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.sort()
minv = 1000000;
for i in range(N-K+1):
  v = h[i+K-1] - h[i]
  minv = min(minv, v)
print(minv)

Submission status: Wrong Answer

Solution code:
n,k=[int(i) for i in input().split()]
a=[0]*n
for i in range(0,n,1):
    a[i]=int(input())
a.sort(reverse=True)
sa=[100001]*100001
j=0
while(n!=(j+k-1)):
    sa[j]=a[j]-a[j+k-1]
    j=j+1

print(min(sa))

Submission status: Wrong Answer

Solution code:
n,k=[int(i) for i in input().split()]
a=[0]*n
for i in range(0,n,1):
    a[i]=int(input())
a.sort(reverse=True)
sa=[0]*(n-k)
for j in range(0,n-k,1):
    sa[j]=a[j]-a[j+k]
print(min(sa))

Submission status: Wrong Answer

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

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

h_list.sort()

min_diff = 9999999999
for i in range(N - K):
    diff = abs(h_list[i] - h_list[i + K - 1])
    min_diff = diff if diff < min_diff else min_diff

print(min_diff)

Submission status: Wrong Answer

Solution code:
S = input()
S = S.split()
N = int(S[0])
K = int(S[1])
h = [int(input()) for i in range(N)]
h.sort()
ans1 = h[K-1] - h[0]
ans2 = h[N-1] - h[N-K]
if(ans1 < ans2):
    print(ans1)
else:
    print(ans2)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
h = [int(input()) for i in range(N)]
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 = [int(input()) for i in range(N)]
h.sort()
# print(h)
min=h[N-1]
for i in range(N):
  if(i+K>N):
    break
    tmp = h[i+K-1]-h[i]
    if(min>tmp):
      min = tmp
print(min)

Submission status: Wrong Answer

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

ans = 1000000000

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

print(ans)

Submission status: Wrong Answer

Solution code:
NK = input().split(' ')
N, K = int(NK[0]), int(NK[1])
trees = []
for i in range(N):
  trees.append(int(input()))
trees.sort()
min = float("inf")
for i in range(N-K):
  h = trees[i+K-1]-trees[i]
  if h < min:
    min = h
print(min)

Submission status: Wrong Answer

Solution code:
NK = input().split(' ')
N, K = int(NK[0]), int(NK[1])
trees = []
for i in range(N):
  trees.append(int(input()))
trees.sort()
min = 0
for i in range(N-K):
  h = trees[i]-trees[i+K]
  if h < min:
    min = h
print(min)

Submission status: Wrong Answer

Solution code:
NK = input().split(' ')
N, K = int(NK[0]), int(NK[1])
trees = []
for i in range(N):
  trees.append(int(input()))
trees.sort()
min = 0
for i in range(N-K):
  h = trees[i+K]-trees[i]
  if h < min:
    min = h
print(min)

Submission status: Wrong Answer

Solution code:
import copy

NK = input().split(' ')
N, K = int(NK[0]), int(NK[1])
trees = []
current = 0
for i in range(N):
  tree = int(input())
  if len(trees) < K:
    trees.append(tree)
  elif tree > min(trees) and tree < max(trees):
    tmp1 = copy.deepcopy(trees)
    tmp2 = copy.deepcopy(trees)
    tmp1.remove(min(trees))
    tmp2.remove(max(trees))
    if max(tmp1)-min(tmp1) > max(tmp2)-min(tmp2):
      trees = copy.deepcopy(tmp2)
    else:
      trees = copy.deepcopy(tmp1)
print(max(trees)-min(trees))

Submission status: Wrong Answer

Solution code:
import copy

NK = input().split(' ')
N, K = int(NK[0]), int(NK[1])
trees = []
current = 0
for i in range(N):
  tree = int(input())
  if len(trees) < K:
    trees.append(tree)
  elif tree > min(trees) and tree < max(trees):
    tmp1 = copy.deepcopy(trees)
    tmp2 = copy.deepcopy(trees)
    tmp1.remove(min(trees))
    tmp2.remove(max(trees))
    if max(tmp1)-min(tmp1) > max(tmp2)-min(tmp2):
      trees = tmp2
    else:
      trees = tmp1
print(max(trees)-min(trees))

Submission status: Wrong Answer

Solution code:
n,k=[int(x) for x in input().split()]
h = [int(input()) for x in range(n)]
h=sorted(h)
minimum=1000
for x in range(n-k+1):
    if(minimum>h[k+x-1]-h[x]):
        minimum=h[k+x-1]-h[x]
print(minimum)

Submission status: Wrong Answer

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

heights.sort()

# あり得る最大のサイズ差
answer = max(heights) - min(heights)

for i in range(0, n-k+1):
    x = heights[i:i + k]
    print(x)
    candidate = max(x) - min(x)
    if candidate < answer:
        answer = candidate

print(answer)

Submission status: Wrong Answer

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

#順
heights.sort()
ordered = heights[0:k]
item = max(ordered) - min(ordered)

# 逆
heights.sort(reverse=True)
ordered = ordered = heights[0:k]
item2 = max(ordered) - min(ordered)

if item > item2:
  print(item2)
print(item)

Submission status: Wrong Answer

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

#順
heights.sort()
ordered = heights[0:k-1]
item = max(ordered) - min(ordered)

# 逆
heights.sort(reverse=True)
ordered = ordered = heights[0:k-1]
item2 = max(ordered) - min(ordered)

if item > item2:
  print(item2)
print(item)

Submission status: Wrong Answer

Solution code:
n,k=[int(x) for x in input().split()]
h = [int(input()) for x in range(n)]
h=sorted(h)
minimum=1000
for x in range(n-k):
    if(minimum>h[k+x-1]-h[x]):
        minimum=h[k+x-1]-h[x]
print(minimum)

Submission status: Wrong Answer

Solution code:
N, K = (int(x) for x in input().split())
h = [int(input()) for i in range(K)]
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())
print(N, K)
t = [int(input()) for i in range(N)]
t.sort()

print(min(t[j+K-1] - t[j] for j in range(N-K+1)))

Submission status: Wrong Answer

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

l = []

for j in range(N-K+1):
    l.append(t[j] - t[j+K-1])
print(min(l))

Submission status: Wrong Answer

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

t = [int(input()) for i in range(N)]
t.sort(reverse = True)
difference = t[0] - t[K-1]
print(difference)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
h = []
for _ in range(N):
	h.append(int(input()))
_max = h.count(max(h))
if _max == K:
	print(0)
else:
	h.sort(reverse=True)
	ans = h[0:K]
	print(max(ans)-min(ans))

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
h = []
for _ in range(N):
	h.append(int(input()))
_max = h.count(max(h))
if _max == K:
	print(0)
else:
	h.sort()
	ans = h[0:K]
	print(max(ans)-min(ans))

Submission status: Wrong Answer

Solution code:
#! /usr/bin/env python3
# -*- coding: utf-8 -*-


nnk = list(map(lambda x: int(x), input().split(" ")))
ps = [int(input()) for l in range(nnk[0])]
ps.sort()

hmax = ps[-1]
hmin = ps[-nnk[1]]

print(hmax - hmin)

Submission status: Wrong Answer

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

a.sort()

mindiff = 10000

for i in range(int(N)-(S - 1)):
    v = 0
    for j in range(int(S)-1):
        v += a[i+j+1]-a[i+j]
        if mindiff < v:break

    if mindiff > v: mindiff = v

print(mindiff)

Submission status: Wrong Answer

Solution code:
N,K=(int(x) for x in input().split())
i=1
p=[]
while i<=N:
    p.append(int(input()))
    i=i+1

p.sort()
high=[]
i=0
while i+K<=N:
        L=int(K+i)
        a = p[int(L-1)]
        b = p[i]
        deff = int(a - b)
        high.append(int(deff))
        i = i + 1

print(p)
print(high)
print(int(min(high)))

Submission status: Wrong Answer

Solution code:
Num = []
Num = list(map(int,input().split(" "))) #0-all 1-pick
lists = []
for i in range(0,Num[0]):
    lists.append(int(input()))
lists.sort()
lists.reverse()
tmp = 100000
for j in range(0,Num[0] - Num[1] + 1):
    if (tmp > lists[j] - lists[j+Num[1]-1]):
        tmp = lists[j] - lists[j+Num[1]-1]
print(tmp)

Submission status: Wrong Answer

Solution code:
N, K = list(map(int, input().strip().split(" ")))
hList = []
for n in range(N):
    hList.append(int(input().strip()))
    
hList.sort()
answer = 1000000
for i in range(N - (K-1)):
    answer = min(answer, hList[i+K-1] - hList[i])
print(answer)

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()
ans = 1000000
s = 0
while s + k - 1 <= n - 1:
  mm = h[s+k-1] - h[s]
  if mm < ans:
    ans = mm
  s += 1
print(int(ans))

Submission status: Wrong Answer

Solution code:
from sys import stdin
a = stdin.readline().split(" ")
num=int(a[0])
find=int(a[1])
j=1
w2=[]
while j<=num:
    z = stdin.readline().rstrip('\n')
    w2.append(int(z))
    j+=1
w2.sort()
minnum=w2[2]-w2[0]
for i in range(len(w2)-2):
    minnum=min(minnum,w2[i+2]-w2[i])
print(minnum)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
h=[int(input()) for i in range(N)]
h.sort()
mindif=h[K-1]-h[0]
for i in range(N-K):
    tmp=h[i:i+K-1]
    dif=tmp[-1]-tmp[0]
    if mindif>dif:
        mindif=dif
print(mindif)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
h=[int(input()) for i in range(N)]
h.sort()
mindif=h[K-1]-h[0]
for i in range(N-K):
    tmp=h[i:i+K]
    dif=tmp[-1]-tmp[0]
    if mindif>dif:
        mindif=dif
print(mindif)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

m = []

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

res = 10**5

for i in range(n-k+1):
    if sum(m[i:i+k-1]) < res:
        res = sum(m[i:i+k-1])
else:
    print(res)

Submission status: Wrong Answer

Solution code:
n, k = map(int,input().split())
lis = []
for _ in range(n):
  lis.append(int(input()))
ans = 100000000000000
for i in range(n-k+1):
  s = lis[i+k-1]-lis[i]
  if s < ans:
    ans = s
print(ans)

Submission status: Wrong Answer

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

trees = list()

for i in range(N):
  trees.append(int(input()))
  
trees = sorted(trees)
print(trees)
diff_list = []

for i in range(N - K + 1):
  diff = trees[i + K - 1] - trees[i]
  diff_list.append(diff)
  
print(diff_list)
print(min(diff_list))

Submission status: Wrong Answer

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

trees = list()

for i in range(N):
  trees.append(int(input()))
  
trees = sorted(trees)
diff_list = []

for i in range(N - 2):
  diff = trees[i + 2] - trees[i]
  diff_list.append(diff)
  
print(min(diff_list))

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
N,K = [int(i) for i in input().split()]

l = [int(input()) for j in range(N)]
l = sorted(l)
dif = 1000000000
for a in range(N-K+1):
    d = l[a+K-1] - l[a]
    print(a)
    print(d)
    if dif >= d:
        dif = d

print(dif)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
h = [int(input()) for _ in range(n)]
h.sort()
ans = 10**10
for i in range(n-k+1):
  ans = min(ans, h[n-k+1]-h[i])
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()
#print(h)
ans=10000000000

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

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
h = sorted(list(map(int, [input() for i in range(n)])))
a = []
for i in range(n-1):
    a.append(h[i+1] - h[i]) 
ans = float('inf')

if k == 1:
    ans = 0
elif n == k:
    ans = sum(a)
else:
    for j in range(n-k):
        b = sum(a[j:j+k-1])
        if ans > b:
            ans = b
    if ans > sum(a[n-k-1:]):
        ans = sum(a[n-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()
min_range = H[:K]
max_range = H[-K:]

minimum = max(min_range) - min(min_range)
maximum = max(max_range) - min(max_range)

print(minimum if minimum < maximum else maximum)

Submission status: Wrong Answer

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

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

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

'''
n = 20
k = 11
list = [7,7,11,11,11,11,11,16,15,15,25,25,25,24,24,24,23,23,23,24]
'''

dict = {}
for l in list:
    if l in dict:
        dict[l] += 1
    else:
        dict[l] = 1

#print('dict=',dict)

list2 = sorted(dict.items(), key=lambda x: x[0])

#print(list2)

lkey = [x[0] for x in list2]
lval = [x[1] for x in list2]
#print(lkey)
#print(lval)

dict = {k:v for k,v in zip(lkey, lval)}
#print('dict=',dict)

sum = 0

def f(i):
    global sum
    sum += i
    return sum

dict = {k:f(v) for k,v in zip(lkey, lval)}
#print('dict=',dict)

hmax = hmin = 0
diffmin = 10**7
#print(diffmin)
#pos = 5
offset = 0

for pos in range(n):
#    pos = 1
#    print(pos, offset, diffmin)
    for i, x in enumerate(dict.items()):
        height = x[0]
        qtysum = x[1]

        if i < pos:
            offset = qtysum
    #        print('offset:', offset)
            continue
        if i == pos:
            hmin = height

#        if i == pos:
#            offset = qtysum

        if hmin == 0:
            hmin = height
#        print(x[0], x[1])
        if k <= qtysum - offset:   # dict.items()[pos][1]:
            hmax = x[0]
            if hmax - hmin < diffmin:
                diffmin = hmax - hmin
#            print(i,"hmax=",hmax,' hmin=',hmin, "diffmin=",diffmin)
            break
    if k > qtysum - offset:
        pass
        break


print(diffmin)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

h = []
for i in range(N) : 
    s = input()
    h.append(int(s))
    
h = sorted(h)
minDiff = h[len(h) - 1] - h[0]
for i in range(N) : 
    try : 
        diff = h[len(h) - K + i] - h[i]
        minDiff = min(minDiff , diff)
    except : 
        continue
print(minDiff)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def main():
  N, K = map(int, input().split(' '))
  # N本の木があって，i本目の木はh_i[m]
  # K本選んで電飾する.
  # 木の内一番高いのをh_max[m], 低いものをh_min[m]
  # min(h_max - h_min)をしたい.
  trees = [int(input()) for _ in range(N)]
  trees.sort()
  trees = [n - trees[0] for n in trees]
  print(trees[K-1] - trees[0])

main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N,K = list(map(int, input().split()))
h = [int(input()) for i in range(N)]
candidate = None
candidate = 1000000000
h.sort()
for i in range(N-K-1):
    tmp = h[i+K-1] - h[i]
    if candidate == None or tmp < candidate:
        first_flg = False
        candidate = tmp
print(candidate)

Submission status: Wrong Answer

Solution code:
N,K = list(map(int, input().split()))
h = [int(input()) for i in range(N)]
first_flg = True
candidate = 1000000000
h.sort()
for i in range(N-K-1):
    tmp = h[i+K-1] - h[i]
    if first_flg or tmp < candidate:
        first_flg = False
        candidate = tmp
print(candidate)

Submission status: Wrong Answer

Solution code:
a,b=map(int,input().split())
c=[]
for i in range(a):
	c.append(int(input()))
c=sorted(c)
print(c)
f=[]
for k in range(a-b+1):
	e=[]
	f.append(e)
	for i in range(b):
		e.append(c[i+k])
print(f)
g=[]
for i in range(len(f)):
	g.append(max(f[i])-min(f[i]))
print(min(g))

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.sort(reverse=True)
c = collections.Counter(h)
flg = False
for i in c:
    if c[i] >= k:
        print(0)
        flg = True
        break

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

Submission status: Wrong Answer

Solution code:
a,b=map(int,input().split())
c=[]
for i in range(a):
	c.append(int(input()))
c=sorted(c)
f=[]
for k in range(a-b+1):
	e=[]
	f.append(e)
	for i in range(b):
		e.append(c[i+k])
print(f)
g=[]
for i in range(a-b+1):
	g.append(max(f[i])-min(f[i]))
print(min(g))

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a = [int(input()) for i in range(n)]
a.sort()
ans = 1000000000000000
for i in range(n-k):
  ans = min(ans,a[i+k-1]-a[i])
    
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.sort(reverse=True)
c = collections.Counter(h)
flg = False
for i in c:
    if c[i] >= k:
        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:
import collections

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

h.sort(reverse=True)
c = collections.Counter(h)
flg = False
for i in c:
    if c[i] >= k:
        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(diff)