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: Accepted

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

Submission status: Accepted

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

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

an.sort()
dn = [-1 for _ in range(n - k + 1)]
for i in range(n - k + 1):
  dn[i] = an[i+k-1] - an[i]

print(min(dn))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,K= map(int,input().split())
H = []
for i in range(N):
  H.append(int(input()))
H=sorted(H) 
div=H[N-1]-H[0]
for i in range(0,N-(K-1)):
  a=H[i+K-1]-H[i]
  if div>a:
    div=a
print(div)

Submission status: Accepted

Solution code:
import sys
from collections import deque
input = sys.stdin.readline
import numpy as np
N, K = [int(x) for x in input().split()]
h = []
for _ in range(N):
    h.append(int(input()))
h.sort()
i = 0
ans = []
while N - i >= K:
    temp = []
    temp.append(h[i])
    temp.append(h[i+K-1])
    ans.append(temp[-1] - temp[0])
    i += 1
print(min(ans))

Submission status: Accepted

Solution code:
def main(n: int, k: int, h: list):
    h.sort()

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


if __name__ == "__main__":
    n, k = map(int, input().split())
    h = [int(input()) for _ in range(n)]

    main(n, k, h)

Submission status: Accepted

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

H.sort()

for i in range(0, N - (K-1)):
    min_K = H[i]
    max_K = H[i + K - 1]

    if i == 0:
        min_diff = max_K - min_K
    else:
        min_diff = min(min_diff, max_K - min_K)

print(min_diff)

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.readline
n,k=map(int,input().split())
a=[int(input()) for i in range(n)]
a.sort()
res=float("INF")
for i in range(0,n-k+1):
    b=a[i+k-1]-a[i]
    if res>b:
        res=b
print(res)

Submission status: Accepted

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

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

input_list.sort()

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

print(ans)

Submission status: Accepted

Solution code:
import sys
def input():
  return sys.stdin.readline().rstrip()
n, k = [int(i) for i in input().split()]
h = sorted(int(input()) for _ in range(n))
print(min(h[i + k - 1] - h[i] for i in range(n - k + 1)))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**6)
if sys.platform in (['ios','darwin','win32']):
	sys.stdin=open('Untitled.txt')
input = sys.stdin.readline
def INT(): return int(input())
def MAP(): return [int(s) for s in input().split()]

N, K = MAP()
A = [INT() for _ in range(N)]

A.sort()
#print(A)
ans = float('INF')
for i in range(N-K+1):
	a = A[i+K-1]-A[i]
	#print(a)
	ans = min(a, ans)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
L=[int(input()) for _ in range(N)]
LS = sorted(L)
import sys
ANS = sys.maxsize
#print(LS)
for i in range(N-K+1):
  #print("i={}, i+K-1={}, ANS={}, LS[i+K-1]={},LS[i]={}".format(i, i + K -1, ANS, LS[i+K-1],LS[i]))
  ANS = min(ANS, LS[i+K-1] - LS[i])
print(ANS)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# coding: utf-8
import sys

n,k = map(int, input().split())
H = sorted(map(int, sys.stdin.readlines()))
ans = 10**9
for i in range(n-k+1):
    dif = H[i+k-1] - H[i]
    if dif == 0:
        print(0)
        break
    elif dif < ans:
        ans = dif
else:
    print(ans)

Submission status: Accepted

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

    S.sort(reverse=True)

    diff_arr = [S[i] - S[i+K-1] for i in range(len(S)-K+1)]
    # [print(S[i], S[i+K-1], i, i+K-1) for i in range(len(S)-K)]
    
    # print(diff_arr)
    # mx_idx = diff_arr.index(min(diff_arr))
    # mn_idx = mx_idx + K - 1

    print(min(diff_arr))

if __name__ == "__main__":
    main()

Submission status: Accepted

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

    S.sort(reverse=True)

    diff_arr = [S[i] - S[i+K-1] for i in range(len(S)-K+1)]
    print(min(diff_arr))

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
#115C
import sys
N, K = map(int, sys.stdin.readline().split())
H = [int(sys.stdin.readline()) for _ in range(N)]

H.sort()
#print(H)
ans = H[K-1] - H[0]
for i in range(K, N):
    tmp = H[i] - H[i-K+1]
    if ans > tmp:
        ans = tmp
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

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

print(d)

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
h = []
for i in range(n):
    h.append(int(input()))
h.sort()
h_max = 0
h_min = 0
r = 0
ans = []
#print(h)
for i in range(r,n-k+1):
    if n - r < k: 
        break
    h_max = h[r+k-1]
    #print(h[r:k])
    h_min = h[r]
    ans.append(h_max - h_min)
    r += 1
print(min(ans))

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys

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

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

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

print(minimum)
#print(h)

Submission status: Accepted

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

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

h.sort()

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

print(min(length))

Submission status: Accepted

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

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

h.sort()
r = [h[i+k-1]-h[i] for i in range(n-k+1)]
print(min(r))

Submission status: Accepted

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

h_list = [int(input()) for _ in range(N)]
h_list.sort(reverse=True)

minmax = 10e10
for i in range(N - K + 1):
    minmax = min(minmax, h_list[i] - h_list[i + K - 1])

print(minmax)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
n,k=map(int,input().split())
h=[]
for i in range(n):
    h.append(int(input()))
ans=10**10
c=0
h.sort()
for i in range(n-k+1):
    c=h[i+k-1]-h[i]
    if c<ans:
        ans=c
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, K = [int(str) for str in input().strip().split()]
hs = [int(input().strip()) for _ in range(N)]

hs.sort()

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
A = []
for _ in range(n):
    A.append(int(input()))
A.sort()
ans = 100000000000
for i in range(n - k + 1):
    min_ = A[i]
    max_ = A[i + k - 1]
    if max_ - min_ < ans:
        ans = max_ - min_
print(ans)

Submission status: Accepted

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

ans = 10**9
for i,j in zip(h,h[k-1:]):
  ans = min(ans,j-i)
print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
N, K = map(int, input().split())
h = []
for i in range(N):
    h1 = int(input())
    h.append(h1)
    
h.sort()
a = []
for i in range(N - (K - 1)):
    a.append(h[i+K-1] - h[i])
    
print(min(a))

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

tr.sort()
INF = float('inf')
ans = INF
for i in range(N-K+1):
    dif = tr[i+K-1] -tr[i]
    if dif < ans:
        ans = dif

print(ans)

Submission status: Accepted

Solution code:
import numpy as np
N, K, *H = map(int, open(0).read().split())
H.sort()
H = np.array(H)
print(min(H[(K-1):]-H[:-(K-1)]))

Submission status: Accepted

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

N, K = [int(x) for x in input().strip().split()]
H = sorted([int(input()) for _ in range(N)])
ans = 10 ** 9
for n in range(N - (K - 1)):
    ans = min(ans, H[n+K-1] - H[n])

print(ans)

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline

def main():
    N, K = map(int, input().split())
    h = sorted([int(input()) for i in range(N)])
    
    ans = float('inf')
    
    for i in range(N-K+1):
        ans = min(ans, h[i+K-1] - h[i])
    
    print(ans)
    
if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

b=[]
li.sort()
for j in range(n+1-k):
  b.append(li[j+k-1]-li[j])
  
print(min(b))

Submission status: Accepted

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

Submission status: Accepted

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

print(res)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

sa=[]

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

print(min(sa))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

h.sort()
S=10000000000000
for i in range(n-k+1):
  if S>h[i+k-1]-h[i]:
    S=h[i+k-1]-h[i]
print(S)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys, math
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
sys.setrecursionlimit(10**8)
inf = float('inf')
ans=inf
count=0

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

Submission status: Accepted

Solution code:
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools
sys.setrecursionlimit(10**8)
mod = 10**9+7
mod2 = 998244353
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))

n,k = inpl()
a = inpln(n)
a.sort()
res = INF
for i in range(n-k+1):
    res = min(res, a[i+k-1] - a[i])
print(res)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
H = sorted([int(input()) for i in range(N)], reverse=True)
ans = []
for i in range(N):
    try:
        ans.append(H[i] - H[i+K-1])
    except:
        pass
print(min(ans))

Submission status: Accepted

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

lis = []

for _ in range(n):
    lis.append(int(input()))

lis.sort()

min = 10**10
for i in range(len(lis)-k+1):
    dist = lis[i+k-1] - lis[i]
    if min > dist: min = dist
print(min)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# 2019-11-11 18:30:26(JST)
import sys
# import collections
# import math
# from string import ascii_lowercase, ascii_uppercase, digits
# from bisect import bisect_left as bi_l, bisect_right as bi_r
# import itertools
# from functools import reduce
# import operator as op
# from scipy.misc import comb # float
# import numpy as np 

def main():
    n, k, *h = (int(x) for x in sys.stdin.read().split())
    h.sort()
    diffs = []
    for i in range(n - k + 1):
        diffs.append(h[i+k-1] - h[i])
    print(min(diffs))

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# 2019-11-11 18:30:26(JST)
import sys
# import collections
# import math
# from string import ascii_lowercase, ascii_uppercase, digits
# from bisect import bisect_left as bi_l, bisect_right as bi_r
# import itertools
# from functools import reduce
# import operator as op
# from scipy.misc import comb # float
# import numpy as np 

def main():
    n, k, *h = (int(x) for x in sys.stdin.read().split())
    h.sort()
    min_diff = h[-1] - h[0] # default value
    for i in range(n - k + 1):
        diff = h[i+k-1] - h[i]
        min_diff = min(min_diff, diff)
    print(min_diff)

if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
H = [int(input()) for n in range(N)]
H.sort()
ans = 10000000000
for n in range(N-K+1):
    ans = min(ans,H[n+K-1]-H[n])
print(ans)

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
H = []
for n in range(N):
    H.append(int(input()))
H.sort()
ans = 10000000000
for n in range(N-K+1):
    ans = min(ans,H[n+K-1]-H[n])
print(ans)

Submission status: Accepted

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

min_dif = 10**9 + 1
for shift in range(n - k + 1):
  dif = H[shift + k - 1] - H[shift]
  min_dif = min(min_dif, dif)
print(min_dif)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
l = [int(input()) for _ in range(n)]
l.sort()
ans = 10**9+7
for i in range(n-k+1):
    if ans > l[i+k-1]-l[i]:
        ans = l[i+k-1]-l[i]
print(ans)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
a = sorted([int(input()) for _ in range(n)])
left = 0
right = k - 1
ret = float('inf')
while right < n:
    ret = min(ret, a[right] - a[left])
    left += 1
    right += 1
print(ret)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import os, sys, re, math

N,K = list(map(int,input().split(' ')))
trees = []

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

trees.sort(reverse=True)
ret = trees[0]

for i in range(N-K+1):
	tmp = trees[i] - trees[i+K-1]
	ret = min(ret, tmp)

print(ret)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
h=[]
for i in range(n):
    h.append(int(input()))
h2=sorted(h)
start=0
goal=k-1
ans=10000000000
while goal<=n-1:
    ans=min(ans,h2[goal]-h2[start])
    start+=1
    goal+=1
print(ans)

Submission status: Accepted

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

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

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from sys import stdin
from itertools import groupby

n,k = [int(x) for x in stdin.readline().rstrip().split()]
li = [int(stdin.readline().rstrip()) for _ in range(n)]
li.sort()

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

print(mi)

Submission status: Accepted

Solution code:
import os, sys, re, math

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

h.sort()
result = 10 ** 9
for i in range(N - K + 1):
    result = min(result, h[i + K - 1] - h[i])

print (result)

Submission status: Accepted

Solution code:
n, k = list(map(int, input().split(' ')))
ts = []
for i in range(n):
  ts.append(int(input()))
ts = sorted(ts)  # 1e5 * log(1e5)
ans = 1e10
for i in range(n-k+1):  # 1e5
  ans = min(ans, ts[i + k - 1] - ts[i])
print(ans)

Submission status: Accepted

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


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


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

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
hs = []
for n in range(N):
    h = int(input())
    hs.append(h)
hs.sort()
ret = hs[-1] - hs[0] 
for i in range(0, N - K + 1):
    hmin = hs[i]
    hmax = hs[i + K - 1]
    ret = min(ret, hmax - hmin)

print(ret)

Submission status: Accepted

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

Submission status: Accepted

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

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

h.sort(reverse=True)

dh_max = h[0]

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

print(dh_max)

Submission status: Accepted

Solution code:
N, K = [int(x) for x in input().split()]
H = sorted([int(input()) for _ in range(N)])
minh = H[0]
H = [h-minh for h in H]
l = K-1
ans = H[-1]
for i in range(N-l):
    ans = min(ans, H[i+l]-H[i])
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

X = []
for n in range(N-K+1):
    x = H[n] - H[n+K-1]
    X.append(x)

print(min(X))

Submission status: Accepted

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

h.sort()

ret = float('inf')
for i in range(N - K + 1) :
    ret = min(ret, h[i+K-1] - h[i])
print(ret)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

L = []

for i in range(0,N - K + 1):
  diff = H[i + K - 1] - H[i]
  L.append(diff)

    
print(min(L))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def c_christmas_eve():
    N, K = [int(i) for i in input().split()]
    H = sorted([int(input()) for _ in range(N)])
    # 木の高さで昇順ソートしたなら、問題の「最大値と最小値の差」がすぐ求まる
    return min([H[j + K - 1] - H[j] for j in range(N - K + 1)])

print(c_christmas_eve())

Submission status: Accepted

Solution code:
nk = input().rstrip().split(" ")
n = int(nk[0])
k = int(nk[1])
h = []
for i in range(n):
    h.append(int(input()))
h.sort()
res = h[-1] - h[0]
for i in range(n - k + 1):
    res = min(res, h[i + k - 1] - h[i])
print(res)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

H.sort()

reslist = []

for i in range(N):
        j = i + K - 1

        if j >= N:
                break

        df = H[j] - H[i]
        reslist.append(df)

res =min(reslist)
print(res)

Submission status: Accepted

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

result = float("Inf")

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

print(result)

Submission status: Accepted

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