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

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

print (minh)

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    N, K = map(int, input().split())
    height = sorted([int(input()) for n in range(N)])
    s =  [ height[idx + K - 1] - h for idx, h in enumerate(height)
            if idx + K - 1 < len(height)
        ]
    print(min(s))

Submission status: Accepted

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

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

heights.sort()

diff = [0]*n

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

Submission status: Accepted

Solution code:
(n, k) = [int(x) for x in input().split()]
banig = []
for x in range(n):
  banig.append(int(input()))
banig.sort()

difBanig = []
for bot in range(n-k+1):
  difBanig.append(banig[bot+k-1] - banig[bot])

print(min(difBanig))

Submission status: Accepted

Solution code:
nk = input()
nk = nk.split()
n = int(nk[0])
k = int(nk[1])
trees = []
for i in range(n):
  trees.append(int(input()))
trees.sort()
minimum = 1000000000
i = 0
while i<=n-k:
  if trees[i+k-1]-trees[i]<minimum:
    minimum = trees[i+k-1]-trees[i]
  i+=1
print(minimum)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np

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

diff = H[K-1:] - H[:-K+1]
print(diff.min())

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(n-k+1):
    ans = min(ans,abs(h[i] - h[i+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.sort()
ans=max(h)-min(h)
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 i in range(n)]
h.sort()
ans=float("inf")
for i in range(k-1,n):
    ans=min(ans,h[i]-h[i-k+1])
print(ans)

Submission status: Accepted

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

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())
H=[]
for i in range(N):
    H.append(int(input()))
H.sort()

ans=10**10

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=[0 for _ in range(n)]
for i in range(n):
    h[i]=int(input())
h.sort()
m=sum(h)
for i in range(n-k+1):
    m=min(h[i+k-1]-h[i],m)
print(m)

Submission status: Accepted

Solution code:
from collections import Counter
N , K = map(int,input().split())
h = list(int(input()) for _ in range(N))
h.sort()
min = 10 ** 10
for i in range(len(h) - K + 1):
    tmp_min = h[i + K - 1] - h[i]
    if min > tmp_min:
        min = tmp_min
print(min)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.buffer.readline().rstrip()
def resolve():
    n,k=map(int,input().split())
    H=[int(input()) for _ in range(n)]
    H.sort()
    ans=INF
    for i in range(n-k+1):
        ans=min(ans,H[i+k-1]-H[i])
    print(ans)
resolve()

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n, k, *h = map(int, open(0).read().split())
h.sort()
ans = 10e9+7
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 _ in range(n)]
h.sort()
between = [h[i+1]-h[i] for i in range(n-1)]
answer = 10**9
for i in range(1,n-1):
  between[i] += between[i-1]
between.insert(0,0)

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def solve(n, k, h):
    h.sort()
    diff = [h[i]-h[i-k+1] for i in range(k-1, n)]
    return min(diff)


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

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
H = [int(input()) for _ in range(N)]
H.sort()
ans = 1000000000
for i in range(N-K+1):
    value = H[i+K-1]-H[i]
    if ans > value:
        ans = value
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy,copy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

def main():
    n, k = MAP()
    h = [INT() for _ in range(n)]

    h.sort()

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

if  __name__=='__main__':
    main()

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

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

h.sort()

ans = 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 = []
for i in range(N):
    h = int(input())
    H.append(h)
H = sorted(H)
mi = 10**9
for i in range(N-K+1):
    _ = H[i+(K-1)] - H[i]
    if mi > _:
        mi = _
print(mi)

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
l=[]
ans=10**9

for _ in range(N):
    l.append(int(input()))
    
l.sort()
temp=l[0:K]
ans=min(ans,temp[-1]-temp[0])

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

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

Submission status: Accepted

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

# hをソートして、K本の木を含む区間をスライドさせていく。区間の両端の差が最も小さくなる区間が最適
h.sort()
h_diff = []

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

print(min(h_diff))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
def slove():
    import sys
    input = sys.stdin.readline
    n, k = list(map(int, input().rstrip('\n').split()))
    h = [int(input().rstrip('\n')) for _ in range(n)]
    h.sort()
    ma = 10 ** 10
    for i in range(k-1, n):
        ma = min(ma, abs(h[i] - h[i-k+1]))
    print(ma)


if __name__ == '__main__':
    slove()

Submission status: Accepted

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

sorted_H = sorted(H, reverse=True)
diff = sorted_H[0] - sorted_H[-1]

for i in range(n):
    try:
        diff_in = sorted_H[i] - sorted_H[i+k-1]
    except IndexError:
        break
    if diff_in < diff:
        diff = diff_in

print(diff)

Submission status: Accepted

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

m=10**9
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 _ in range(n)]
h.sort()

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

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())
trees = []
for _ in range(n):
    trees.append(int(input()))
trees.sort()
ans = 10**9
for i in range(n):
    if i+k -1 ==n:
        break
    ans = min(ans,trees[i+k-1] -trees[i])
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
from bisect import *
from collections import *
from copy import deepcopy
from datetime import *
from heapq import *
from itertools import *
# from math import *
from operator import *
from pprint import *

sys.setrecursionlimit(10**8)

input = sys.stdin.readline

def main():
    """ main """
    N,K = map(int, input().split())
    HS = [int(input()) for _ in range(N)]
    HS.sort()

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

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, K = map(int,input().split())
h = [int(input()) for _ in range(N)]
h.sort()
m = 10**9
for i in range(N-K+1):
    if h[i+K-1] - h[i] < m:
        m = h[i+K-1] - h[i]
for i in range(N-K+1):
    if h[i+K-1] - h[i] == m:
        print(int(m))
        break

Submission status: Accepted

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

Submission status: Accepted

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

answer = min(h2 - h1 for h1, h2 in zip(h, h[k - 1:]))
print(answer)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
in1 = [int(x) for x in input().strip().split()]

N = in1[0]
K = in1[1]

trees = [int(input()) for i in range(N)]
trees.sort()

ans = 10 ** 10
for i in range(0, N - K + 1):
	#print(trees[i+K-1], trees[i])
	ans = min(ans, trees[i+K-1] - trees[i])
	
print(ans)

Submission status: Accepted

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

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

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:
if __name__ == "__main__":
    N, K = map(int, input().split())
    h = list()
    for i in range(N):
        h.append(int(input()))

    h = sorted(h)

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

l = sorted([int(input()) for _ in range(a)])

m = [l[i+b-1] - l[i] for i in range(len(l)-b+1)]

print(min(m))

Submission status: Accepted

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

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

Submission status: Accepted

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

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, M = map(int, input().split())
h = sorted([int(input()) for _ in range(N)])

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

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

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

Submission status: Accepted

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


def main():
    N, K = map(int, input().split())
    H = sorted([int(input()) for _ in range(N)])
    ans = 1000000000
    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 = []
for i in range(n):
    h.append(int(input()))


sh = sorted(h)

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

print(min_)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
n,k=map(int,input().split())
h=[int(input()) for i in range(n)]
h.sort()
ans=10**10
for i in range(n):
    if i+k-1>=n:break
    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 _ in range(N)]

H.sort()
ans = 10**9

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

print(ans)

Submission status: Accepted

Solution code:
N, K = map(int,input().split())
h = [int(input()) for i in range(N)]
h.sort(reverse = True)
ans=10**9
#print(h)
for i in range(N-K+1):
    ans = min(ans,h[i]-h[i+K-1])
#    print(ans)
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()
a=10**9
for i in range(N-K+1):
    b=H[K+i-1]-H[i]
    if a>b:
        a=b
print(a)

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()
#print(h)
result = h[-1] -h[0]
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 = sorted(H)
A=[0]*(N-K+1)
ans=0
for i in range(N-K+1):
    A[i] =h[i+K-1]-h[i]
ans=min(A)
print(ans)

Submission status: Accepted

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

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

print(diff)

Submission status: Accepted

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

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

list.sort(h_list)

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np

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

h.sort()
hdiff = [h[i + 1] - h[i] for i in range(len(h) - 1)]
hdiff = np.array(hdiff)
ans = np.sum(hdiff[0:0+K-1])
# print('hdiff: %s' % hdiff)
sum_ = np.sum(hdiff[0:0+K-1])
for i in range(1, N-K+1):
    # print('i %s' % i)
    # print('hdiff[i:i+K-1]: %s' % hdiff[i:i+K-1])]
    sum_ = sum_-hdiff[i-1]+hdiff[i+K-2]
    ans = min(ans, sum_)

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

n,k = MAP()
h = [INT() for _ in range(n)]
h.sort()
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)]
h.sort()
res = 10 ** 9
for i in range(N - K + 1):
  hmin, hmax = h[i], h[i + K - 1]
  res = min(res, hmax - hmin)
print(res)

Submission status: Accepted

Solution code:

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

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

H.sort()

ans = 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=[int(i) for i in input().split()]
array=[]
for i in range(n[0]):
  array.append(int(input()))
array.sort()
ans=abs(array[0]-array[n[1]-1])
for i in range(n[0]-(n[1]-1)):
  if(ans>abs(array[i]-array[i+n[1]-1])):
    ans=abs(array[i]-array[i+n[1]-1])
print(ans)

Submission status: Accepted

Solution code:
def christmasEve2():
    N, K = map(int, input().split())
    strees = sorted([int(input()) for _ in range(N)])
    print(min(sorted([y - x for x, y in zip(strees, strees[K-1:])])))


if __name__ == "__main__":
    christmasEve2()

Submission status: Accepted

Solution code:
def christmasEve():
    N, K = map(int, input().split())
    h = [int(input()) for i in range(N)]
    strees = sorted(h)
    result=10**9
    for i in range(N-K+1):
        result = min(result, strees[i+K-1] - strees[i])
    print(result)

if __name__ == "__main__":
    christmasEve()

Submission status: Accepted

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

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):
    # 最適な選び方は、連続した並びの木k本となる
    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 _ 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())
h = [int(input()) for i in range(N)]
h.sort()
Max = []
Min = []
ans = 999999999999999
for i in range(N-K+1): #N個のうちK個の連続する数の区間はN-K+1個だけ作れる
    Max.append(i+K-1) #ソートされたある区間での最大値はi+K-1
    Min.append(i)     #ソートされたある区間での最小値はi
for i in range(N-K+1):
    ans = min(ans,h[Max[i]] - h[Min[i]])
print(ans)

Submission status: Accepted

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

for i in range(N):
    b=int(input())
    a.append(b)

a=sorted(a)

b=[]
for j in range(N-K+1):
    c=a[j+K-1]-a[j]
    b.append(c)    
        
print(min(b))

Submission status: Accepted

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

height.sort()

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

print(ans)

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    N, K = map(int, input().split())
    H = [int(input()) for _ in range(N)]
    H.sort()
    dis_min = 10**9
    for i in range(N-K+1):
        dis_min = min(dis_min, H[i+K-1]-H[i])
        
    print(dis_min)

Submission status: Accepted

Solution code:
N,K = [int(x) for x in input().split()]
H = [0]*N
for i in range(N):
  H[i] = int(input())
H.sort(reverse=True)
ans = 100000000000000000
for i in range(N-K+1):
  ans = min(ans,H[i]-H[i+K-1])
print(ans)

Submission status: Accepted

Solution code:
n, k = [int(x) for x in input().split()]
h = sorted([int(input()) for i 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 = sorted([int(input()) for _ in range(n)],reverse=True)
mn = max(h)
for i in range(n-k+1):
    mn1 = h[i] - h[i+k-1]
    if mn1 < mn:
        mn = mn1
print(mn)

Submission status: Accepted

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

d=10**10
for i in range(N-K+1):
  d=min(d,abs(h[i]-h[i+K-1]))

print(d)

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.readline
N, K = list(map(int, readline().split()))
h = []
for _ in range(N):
    h.append(int(readline()))

h.sort()

L = 0
resp = 10**9 + 1
while L + K <= N:
    b = h[L + K - 1] - h[L]
    if resp > b:
        resp = b
    # print(h[L + K - 1],h[L],resp)
    L += 1

print(resp)

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
h = [int(input()) for _ in range(n)]
h.sort()
minv = float('inf')
for i in range(n-k+1):
  if minv > h[i+k-1]-h[i]:
    minv = h[i+k-1]-h[i]

print(minv)

Submission status: Accepted

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

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 i 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())
h = [int(input()) for hogehoge in range(n)]
h = sorted(h)
ans = 10 ** 10
for i in range (n - k + 1):
         if (h[i + k - 1] - h[i]) < ans:
                  ans = (h[i + k - 1] - h[i])
print(ans)

Submission status: Accepted

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

q=sorted(h)
ansList=[]
for c,d in zip(q,q[k-1:]):
  ansList.append(d-c)

print(min(ansList))

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
H = [int(input()) for _ in range(N)]
H = sorted(H)
ans = 1e9
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 _ 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())
H = list(sorted([int(input()) for _ in range(N)]))

res = 10 ** 10
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 = list(sorted([int(input()) for _ in range(N)]))

diff = [h - H[0] for h in H]

res = 10 ** 10
for i in range(N - (K - 1)):
    res = min(res, diff[i + (K - 1)] - diff[i])

print(res)

Submission status: Accepted

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

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

trees.sort()
trees_dif=[trees[i+K-1]-trees[i] for i in range(N-K+1)]
print(min(trees_dif))

Submission status: Accepted

Solution code:
N, K = list(map(int, input().split()))
H = [int(input()) for _ in range(N)]
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:
N, K = map(int, input().split())
H = [int(input()) for _ in range(N)]
H.sort()
m = 1e10
for i in range(N - K + 1):
    d = H[i + K - 1] - H[i]
    if d < m:
        m = d
print(m)

Submission status: Accepted

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

hh = sorted(h)
ans = 10 ** 9
for i in range(N-K+1):
    ans = min(ans, hh[i+K-1] - hh[i])

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

print(INF)