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:
def main():
    #from sys import stdin, setrecursionlimit
    #setrecursionlimit(10**9)
    #r = input()
    #n = int(input())
    #a, b = map(int, input().split())
    #s, y = input().split()
    #a = input().split()
    #a = [int(input()) for i in range(n)]
    #a = list(map(int, input().split()))
    #a = [list(map(int, input().split())) for i in range(n)]
    #a = [int(s) - 1 for s in input().split()]
    #l=[int(input()) for _ in range(int(input()))]
    #res = 0
    n,k = map(int,input().split())
    a = sorted([int(input()) for i in range(n)])
    b = list(a[i]-a[i-k+1] for i in range(k-1,n))
    print(min(b))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
h=[int(input()) for _ in range(N)]
h.sort(reverse=True)
min=float("inf")
#print(h)
for i in range(N-(K-1)):
    #print(h[i],h[i+K-1])
    if min>(h[i]-h[i+K-1]):
        min=h[i]-h[i+K-1]
print(min)

Submission status: Accepted

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

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

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

Submission status: Accepted

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

Submission status: Accepted

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

remainder_lst = []
for i in range(n-k+1):
    remainder = height_lst[i+k-1] - height_lst[i]
    remainder_lst.append(remainder)

answer = min(remainder_lst)
print(answer)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
h = []
for _ in range(n):
    h.append(int(input()))
h.sort()
# answer = min(h[k - 1] - h[0], h[-1] - h[-k])
# print(answer)
answer = float('inf')
for i in range(n - k + 1):
    cost = h[k - 1 + i] - h[i]
    # print(h[i:k+i])
    if answer > cost:
        answer = cost

print(answer)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np

N, K = map(int, input().split())
h = np.array([int(input()) for _ in range(N)])
h.sort()
h = h[::-1]

print((h[:-K + 1] - h[K - 1:]).min())

Submission status: Accepted

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

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()
m = 10**9
for i in range(0, n-k+1):
  if H[i+k-1] - H[i] < m:
    m = H[i+k-1] - H[i]
print(m)

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
import numpy as np
H=np.array([int(input()) for _ in range(N)])
H.sort()
print((H[K-1:]-H[:-K+1]).min())

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
H=[int(input()) for _ in range(N)]
H.sort()
import numpy as np
H=np.array(H)
print((H[K-1:]-H[:-K+1]).min())

Submission status: Accepted

Solution code:
#chirstmas eve
n,k=map(int,input().split())
lists=[] 
for i in range(n):
    a=int(input())
    lists.append(a)
lists=sorted(lists)
ans=10**10
for i in range(n-k+1):
    ans=min(ans,lists[i+k-1]-lists[i])
print(ans)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
h=[int(input()) for i in range(n)]
h=sorted(h)
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)]
H.sort()
min_ = float('inf')
for i in range(n-k+1):
    d = H[i+k-1] - H[i]
    min_ = min(min_, d)
print(min_)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
H = list(int(input()) for _ in range(N))
H.sort()
ans = 10 ** 18
for i in range(N - K + 1):
    tmp = H[i + K - 1] - H[i]
    ans = min(ans, tmp)
print(ans)

Submission status: Accepted

Solution code:
import sys
stdin = sys.stdin

mod = 10**9 + 7

ns = lambda: stdin.readline().rstrip()
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))

N, K = na()
h = list()
for _ in range(N):
    h.append(ni())

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

Submission status: Accepted

Solution code:
def main():
    N,K=map(int,input().split())
    h=[]
    for i in range(N):
        a=int(input())
        h.append(a)
    
    h.sort()
    result=10**10
    count=0
    for i in range(N-K+1):
        count=h[i+K-1]-h[i]
        result=min(result,count)
    print(result)
if __name__=="__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
inf = sys.maxsize // 2
n,k = map(int,input().split())
h = []
for i in range(n):
    h.append(int(input()))
h.sort()
#print(h)
ans = inf
for i in range(n - k + 1):
    #print(i, i+k)
    tmp = h[i+k-1] - h[i]
    ans = min(ans, tmp)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
n, k = map(int,input().split())
h = []
for i in range(n):
    h.append(int(input()))

ans = 10 ** 18
h.sort()
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:
n,k=map(int,input().split())
a=[int(input()) for i in range(n)]
a=sorted(a)
ans=10**9
for i in range(n-k+1):
    tmp=a[i+k-1]-a[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.sort()
ans=10**10
for i in range(n-k+1):
    t=h[i+k-1]-h[i]
    ans=min(t,ans)
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, *a = map(int, open(0).read().split())
a.sort()

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

Submission status: Accepted

Solution code:
#
# 　　  ⋀_⋀　 
#　　  (･ω･)  
# .／ Ｕ ∽ Ｕ＼
#  │＊　合　＊│
#  │＊　格　＊│ 
#  │＊　祈　＊│ 
#  │＊　願　＊│ 
#  │＊　　　＊│ 
#      ￣
#
import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,sqrt,factorial,hypot,log #log2ないｙｐ
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from fractions import gcd
from random import randint
def ceil(a,b): return (a+b-1)//b
inf=float('inf')
mod = 10**9+7
def pprint(*A): 
    for a in A:     print(*a,sep='\n')
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    N,K=MI()
    H=LIN(N)
    H.sort()
    ans = inf
    for l in range(N):
        r = l+K-1
        if r>=N:
            break
        ans = min(ans, H[r]-H[l])
    print(ans)
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import Counter

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

cnt = 0
for i in range(len(CH)):
    if CH[i] >= K:
        cnt += 1
        break
ans = 999999999999999
for i in range(N-K+1):
    ans = min(ans,H[K-1+i] - H[i]) 

if cnt != 0:
    print('0')
else:
    print(ans)

Submission status: Accepted

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

Submission status: Accepted

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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N, K = map(int,input().split())
h = sorted([int(input()) for k in range(N)])
ans = float("inf")
for i in range(N-K+1):
#    print(h[i+K-1],h[i])
    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 = H[K-1] - H[0]
for i in range(N-K+1):
  l = H[K+i-1] - H[i]
  ans = min(ans, l)
print(ans)

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
n, k = map(int,input().split()) 
h = []
for i in range(n):
    h.append(int(input()))

h.sort()
"""
ans = 0
for i in range(k):
    ans += h[i]
#尺取り
tmp = ans
for i in range(n - k):
    tmp = tmp - h[i] + h[i + k]
    if tmp < ans:
        ans = tmp
print(ans)
"""
ans = 10 ** 18
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:
import sys
input = sys.stdin.readline
N, K = map(int, input().split())
H = [int(input()) for _ in range(N)]
H.sort()

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

print(ans)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
def input():
    return sys.stdin.readline().rstrip()

def main():
    N,K=map(int,input().split())
    h=[int(input()) for i 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)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
stdin = sys.stdin

mod = 10**9 + 7

ns = lambda: stdin.readline().rstrip()
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))

n, k = na()
h = list()
for _ in range(n):
    h.append(ni())
h.sort()
inf = 10**10
ans = inf
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())
a=[int(input()) for i in range(n)]
ans=10**9
a=sorted(a)
for i in range(n-k+1):
    tmp=a[i+k-1]-a[i]
    ans=min(ans,tmp)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n, k, *a = map(int, open(0).read().split())
a.sort()

ans = 1e18
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 = [int(input()) for _ in range(N)]

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

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

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

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[k+i-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=10**12
for i in range(n-k+1):
    min_h=h[i+k-1]-h[i]
    ans=min(ans,min_h)
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=sorted([int(input()) for _ in range(n)])
a=10**9
for i in range(n-k+1):
    a1=h[i+k-1]-h[i]
    a=min(a,a1)
print(a)

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
import itertools

# import numpy as np

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

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

Submission status: 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((h[i+K-1] - h[i]), ans)
print(ans)

Submission status: Accepted

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

h.sort()

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

print(m)

Submission status: Accepted

Solution code:
N_hon, K_ko = map(int, input().split())
Height = list(int(input()) for i in range(N_hon))
Height.sort()
mini=float('inf')

for i in range(K_ko - 1, N_hon):
    mini = min(Height[i] - Height[i - (K_ko - 1)], mini)    
    
print(mini)

Submission status: Accepted

Solution code:
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):
  m=H[k+i]-H[i+1]
  if m<ans:
    ans=m
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import deque
n,k = map(int,input().split())
h = sorted([int(input()) for _ in range(n)])
x = deque([])
mn = 10**9
for i in h:
    x.append(i)
    if len(x) >= k:
        mn = min(mn,x[k-1]-x[0])
        x.popleft()
print(mn)

Submission status: Accepted

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

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

ans = max(h)
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())

res = float("inf")

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

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

print(res)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np

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

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

Submission status: Accepted

Solution code:
def main():
    N,K=map(int,input().split())
    h=[0 for i in range(0,N)]
    for i in range(0,N):
        h[i]=int(input())
    h.sort()
    ans=10**9
    for i in range(0,N-K+1):
        test=h[i+K-1]-h[i]
        if test<ans:
            ans=test
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

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


def calculate(n,k,arr):
    arr.sort()

    drr = []

    for i in range(1,n):
        drr.append(arr[i] - arr[i-1])


    s = 0
    j = 0
    sums = []

    for i in range(len(drr) - (k-1) + 1):

        while j < i + k - 1:
            s = s + drr[j]
            j = j + 1
        sums.append(s)
        s = s - drr[i]


    print(min(sums))

calculate(n,k,arr)

Submission status: Accepted

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


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

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


    print(min(sums))


calculate(n,k,arr)

Submission status: Accepted

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


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

    j = 0
    s = 0
    sums = []
    for i in range(len(drr) - (k-1) + 1):
        while j < i + (k-1):
            s = s + drr[j]
            j = j + 1

        sums.append(s)
        s = s - drr[i]

    print(min(sums))

calculate(n,k,arr)

Submission status: Accepted

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

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



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

    j = 0
    s = 0
    sums = []
    for i in range(0,len(brr) - (k-1) + 1):
        while j < i + (k-1):
            s = s + brr[j]
            j = j + 1
        sums.append(s)
        s = s - brr[i]

    print(min(sums))



calculate(n,k,arr)

Submission status: Accepted

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

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

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

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

calculate(n, k, arr)

Submission status: Accepted

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

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

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



calculate(n, k, arr)

Submission status: Accepted

Solution code:
import itertools
import math
import sys

n, k = map(int, input().split())
h = [int(input()) for i in range(n)]
h.sort()
ans = 10 ** 9
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:
import math
n, k = list(map(int, input().split()))

trees = [0] * n
for i in range(n):
    trees[i] = int(input())
sorted_trees = sorted(trees)

minh = sorted_trees[0]
maxh = sorted_trees[k-1]
mind = maxh - minh
for i in range(k, n):
    maxh = sorted_trees[i]
    minh = sorted_trees[i-k+1]
    if mind > maxh - minh:
        mind = maxh - minh

print(mind)

Submission status: Accepted

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

#n=5
#k=3
#h=[10,15,11,14,12]
#h=[5,7,5,7,7]

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

Submission status: Accepted

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

Submission status: Accepted

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

h_sorted = sorted(h)

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

print(ans)

Submission status: Accepted

Solution code:
N,K = map(int, input().split())
#h = list(map(int, input().split()))
h = [int(input()) for _ in range(N)]
h.sort()
min=100000000000000000000
for i in range(0,N-(K-1)):
  if h[i+(K-1)]-h[i]<min:
    min=h[i+(K-1)]-h[i]
print(min)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
H = [int(input()) for _ in range(N)]
H_sort = sorted(H)
ans = H_sort[K-1] - H_sort[0]
for j in range(K, N):
    h_diff = H_sort[j] - H_sort[j + 1 -K]
    ans = min(ans, h_diff)
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()
tree = []

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

print(min(tree))

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
k -= 1
a = sorted([int(input()) for i in range(n)])
ans_min = 10 ** 10
for i in range(n-k):
    result = a[i + k] - a[i]
    ans_min = result if ans_min > result else ans_min
print(ans_min)

Submission status: Accepted

Solution code:
import itertools
n,m = map(int,input().split())
l = [int(input()) for i in range(n)]

l.sort()
res = []

res = [l[i+m-1]-l[i] for i in range(n-m+1)]

print(min(res))

Submission status: Accepted

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

Submission status: Accepted

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

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

if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

h.sort()

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# with open('in.txt', 'w') as f:
#     f.write('200000 10000 1\n')
#     for _ in range(1, 200001):
#         f.write(str(_)+' '+str(_)+'\n')
n, k = input().split()
n = int(n)
k = int(k)
ls = []
m = {}
for _ in range(n):
    __ = int(input())
    m[__] = m.get(__, 0) + 1
ls = [(0, 0)]
ls.extend(list(m.items()))
ls.sort(key=lambda s: s[0])
for i in range(1, len(ls)):
    ls[i] = (ls[i][0], ls[i-1][1]+ls[i][1])
l, r = 0, 0
ans = 9999999999999999
# print(ls)
while True:
    if r < len(ls) and l < len(ls):
        if ls[r][1] - ls[l][1] < k:
            r += 1
        elif ls[r][1] - ls[l][1] >= k:
            # print(l, r)
            ans = min(ans, ls[r][0] - ls[l+1][0])
            l += 1
    else:
        break
print(ans)

Submission status: Accepted

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

print(min(t))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

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:
import numpy as np
n, k = map(int, input().split())
h = [int(input()) for _ in range(n)]
h.sort()
h_max = np.array(h[k-1:])
h_min = np.array(h[:-k+1])
print(min(h_max - h_min))

Submission status: Accepted

Solution code:
n, k, *a = map(int, open(0).read().split())
a.sort()
for i in range(n-1): a[n-i-1] = abs(a[n-i-1]-a[n-i-2])
a[0] = 0
for i in range(n-1): a[i+1] += a[i]

mn = 1e18
for i in range(n-k+1): mn = min(mn, a[i+k-1]-a[i])
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()

ans = float('inf')
for i in range(N):
    if (i+K-1 > N-1):
        break
    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 = int(1e10)
for i in range(n-(k-1)):
    if h[i+(k-1)] - h[i] < ans:
        ans = h[i+(k-1)] - h[i]
#print(h)
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**9
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]*N
for i in range(N):
  h[i]=int(input())
h.sort()
mini=1000000001

for j in range(0,N-K+1):
  mini=min(mini,h[j+K-1]-h[j])
  
print(mini)