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():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    #from collections import defaultdict
    from itertools import combinations, permutations, accumulate, groupby
    #from itertools import product
    from bisect import bisect_left,bisect_right
    from heapq import heapify, heappop, heappush
    from math import floor, ceil
    #from operator import itemgetter

    #inf = 10**17
    #mod = 10**9 + 7

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

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
h = [int(input()) for i in range(n)]
l = sorted(h, reverse = True)
ans = l[0] - l[k - 1]
for i in range(1, n - k + 1):
    ans = min(ans, l[i] - l[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=sorted(h,reverse=True)
tmp=10**10
for i in range(N-K+1):
  tmp=min(tmp,h[i]-h[i+K-1])
print(tmp)

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:
import sys, math, itertools, collections, bisect
n,k=map(int,input().split())
h=[]
for i in range(n):
    tmp=int(input())
    h.append(tmp)
h.sort()
min=10000000000
for i in range(len(h)-k+1):
    tmp2=h[i+k-1]-h[i]
    if min>tmp2:
        min=tmp2
print(min)

Submission status: Accepted

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

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

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())
mylist=[]
temp=0
amount=9999999999
for i in range(N):
    mylist.append(int(input()))

mylist.sort()
#print(mylist)

for i in range(N-K+1):
    #print(mylist[i:i+K])
    #temp=sum(mylist[i:i+K])
    temp=mylist[i+K-1]-mylist[i]
    
    amount=min(temp,amount)
    
print(amount)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

mindiff = float('inf')
for i in range(N-K+1):
    diff = sor[i+K-1] - sor[i] 
    if diff <= mindiff:
        mindiff = diff
print(mindiff)

Submission status: Accepted

Solution code:
def main():
    from builtins import int, map, list, float, print, min
    import sys
    sys.setrecursionlimit(10 ** 6)

    input = sys.stdin.readline
    input_list = (lambda: input().rstrip().split())
    input_number = (lambda: int(input()))
    input_number_list = (lambda: list(map(int, input_list())))

    N, K = input_number_list()
    h = [input_number() 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)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

A = sorted(A)

t = A[-1]

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

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
h = [int(input()) for _ in range(n)]
h = sorted(h)
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 = [None] * N
for _ in range(N):
    h[_] = int(input())

h.sort()
ans = 10000000000
tmp = sum(h[:K])
for i in range(N - K + 1):
    ans = min(ans, h[i + K - 1] - h[i])
print(ans)

Submission status: Accepted

Solution code:
def main():
    n, k, *h = map(int, open(0).read().split())
    s = sorted(h)
    t = min([s[i + k - 1] - s[i] for i in range(0, n - k + 1)])
    print(t)


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

Submission status: Accepted

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

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

    ans.sort()

    print(ans[0])


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
h=[]
for _ in range(n):
    h.append(int(input()))
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=[]
for i in range(N):
    h=int(input())
    H.append(h)
H.sort()
i=0
ans=max(H)-min(H)+1
while i+K-1<len(H):
    min_ans=H[i+K-1]-H[i]
    if ans>min_ans:
        ans=min_ans
    i=i+1
print(ans)

Submission status: Accepted

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

H = sorted(H)
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:
import sys
from bisect import *
from heapq import *
from collections import *
from itertools import *
from functools import *
from math import *
from fractions import *

sys.setrecursionlimit(100000000)
input = lambda: sys.stdin.readline().rstrip()

def main():
    N, K = map(int, input().split())
    h = sorted(int(input()) for _ in range(N))
    print(min(j - i for i, j in zip(h, h[K - 1:])))

main()

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

i = 0
ans = 10 ** 9
while i + K - 1 < N:
  n = abs(h[i+K-1] - h[i])
  if ans > n: ans = n
  i += 1
print(ans)

Submission status: Accepted

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

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
trees = list(int(input()) for _ in range(N))
trees.sort()
ans = float("inf")
for i in range(N - K + 1):
    tmp_ans = trees[i + K - 1] - trees[i]
    if tmp_ans < ans:
        ans = tmp_ans
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(10000000)
INF = 1<<32


def solve(N: int, K: int, h: "List[int]"):

    h.sort()
    ans = INF

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

    print(ans)
    return



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 = [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:
# -*- coding: utf-8 -*-
n, k = map(int,input().split())
h = []
for i in range(n):
    h.append(int(input()))

h.sort()
ans = 10 ** 5 * 10 ** 10
for i in range(n - k + 1):
    #print(i, i + 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)]
H.sort()
min_ = float('inf')
for i in range(n-k+1):
    min_ = min(H[i+k-1]-H[i], min_)
print(min_)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
input = sys.stdin.readline

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

min_diff = 10**9
for hl, hr in zip(h, h[k-1:]):
    min_diff = min(min_diff, hr - hl)
print(min_diff)

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())
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 = sorted(list(int(input()) for _ in range(n)))

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

print(min(res))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, acos, atan, asin
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
def input(): return sys.stdin.readline().strip()
def STR(): return input()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
sys.setrecursionlimit(10 ** 9)
inf = sys.maxsize
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=[int(input())for _ in range(n)]
h.sort(reverse=1)
print(min(h[i-k+1]-h[i] for i in range(k-1,n)))

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 i in range(n-k+1):
    ans = min(abs(h[i]-h[i+k-1]),ans)
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+7

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=sorted([int(input()) for i in range(N)])
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)]
h.sort()
Min = h[K] - h[0]
for i in range(N-K+1):
  if h[K+i-1] - h[i] < Min:
    Min = h[K+i-1] - h[i]
    
print(Min)

Submission status: Accepted

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

Submission status: Accepted

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

for i in range(n):

    h[i] = int(input())

h.sort()

ans = float('inf')

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())
h=[int(input()) for i 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 = 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 = [0]*N

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

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

print(min(s))

Submission status: Accepted

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

list_H.sort()
ans = []

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

print(min(ans))

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N,K = map(int, input().split())
A = [0]*N
DP=[0]*N

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

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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()
deco = 10 ** 9
for i in range(N - K + 1):
    if h[i + K - 1] - h[i] < deco:
        deco = h[i + K - 1] - h[i]
print(deco)

Submission status: Accepted

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

h=sorted(h)
bet=[h[N-1]]*(N-K+1)

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

print(min(bet))
#print(h)
#print(bet)

Submission status: Accepted

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

answer = float("inf")
for i in range(n - k + 1):
    temporary = H[i + k - 1] - H[i]
    if temporary < answer:
        answer = temporary

print(answer)

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 j in range(0,n-k+1):
  if  h[j+k-1] - h[j] < ans:
    ans = h[j+k-1] - h[j]
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 = h[k - 1] - h[0]
for i in range(n - (k - 1)):
    if h[i + k - 1] - h[i] < ans:
        ans = h[i + k - 1] - h[i]
        
    if ans == 0:
        break

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()
ans=10**9+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 = []
for _ in range(N):
    h.append(int(input()))
h_sorted = sorted(h)
ans = False
for i in range(N - K + 1):
    if ans is not False:
        ans = min(ans, h_sorted[K + i - 1] - h_sorted[i])
    else:
        ans = h_sorted[K + i - 1] - h_sorted[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)
# print(H)
minH = 10 ** 9
for x in range(N - K + 1):
    sumh = H[x + K - 1] - H[x]
    # print("sumh", sumh)
    if minH > sumh:
        minH = sumh
print(minH)

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()
arr = []
for i in range(n-k+1):
    arr.append(abs(h[i] - h[i+k-1]))

print(min(arr))

Submission status: Accepted

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


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

    H.sort()
    result = float("inf")

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


if __name__ == "__main__":
    main()

Submission status: Accepted

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

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

Submission status: Accepted

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

    H = sorted(H)

    ans = float('inf')
    for ki in range(n - k + 1):
        diff = H[ki + k - 1] - H[ki]
        ans = min(diff, ans)

    print(ans)

if __name__ == "__main__":
    main()

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):
    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 = []
ans = 10**10

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

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

h.sort()
ans = h[N - 1] - h[0]
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())
a = []
for i in range(n):
  a.append(int(input()))
a.sort()

tmp = 1000000000
for i in range(n - m + 1):
  tmp = min(tmp, a[i + m - 1] - a[i])
print(tmp)

Submission status: Accepted

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

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

print(min(diff))

Submission status: Accepted

Solution code:
import bisect
# from collections import Counter, defaultdict, deque, namedtuple
# import copy
# from heapq import heappush, heappop, heapify
# from fractions import gcd
import itertools
# from operator import attrgetter, itemgetter
# import math

import sys

# import numpy as np

ipti = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)

def main():
    n, k = list(map(int,ipti().split()))
    h = [0] * n
    for i in range(n):
        h[i] = int(input())

    h.sort()

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

    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines

def main():
    N,K,*h = map(int, read().split())
    
    h.sort()
    ans = 10**10
    for i in range(N-K+1):
        tmp = h[i+K-1] - h[i]
        ans = min(ans, tmp)
    
    print(ans)


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(reverse=True)

seed = h[0]
for i in range(0,n-k+1):
    high = h[i] - h[i+k-1]
    seed = min(high,seed)

print(seed)

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

Submission status: Accepted

Solution code:

def submit():
    n, k = map(int, input().split())
    hlist = [int(input()) for _ in range(n)]

    hlist.sort()
    min_diff = float('inf')
    for i in range(0, n - k + 1):
        minh = hlist[i]
        maxh = hlist[i + k - 1]
        if maxh - minh < min_diff:
            min_diff = maxh - minh

    print(min_diff)
        

if __name__ == "__main__":
    submit()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
x,y=list(map(int, input().split()))
list=[]
for i in range(x):
	list.append(int(input()))
list.sort()
ans=10**10

for j in range(x-y+1):
	if list[j+y-1]-list[j]<ans:
		ans=list[j+y-1]-list[j]
print(ans)

Submission status: Accepted

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

H.sort()

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

print(min_diff)

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

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

hl.sort()

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

print(ans)

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

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 = 1e9
for i in range(K-1, N):
  diff = H[i] - H[i-K+1]
  ans = min(ans, diff)

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 = []
min1 = 10**10
for i in range(N):
    h = int(input())
    h_list.append(h)
h_list = sorted(h_list, reverse = True)
for i in range(N-K+1):
    cnt = h_list[i]-h_list[i+K-1]
    min1 = min(min1, cnt)
print(min1)

Submission status: Accepted

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

Submission status: Accepted

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

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

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**18
for i in range(k,n+1):
  ans=min(ans,h[i-1]-h[i-k])
print(ans)

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
h = sorted([int(input()) for _ in range(N)])
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=map(int,input().split())
h=list(int(input()) for i in range(N))
h=sorted(h)
t=[]
for i in range(N-K+1):
  s=h[i+K-1]-h[i]
  t.append(s)
print(min(t))

Submission status: Accepted

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

h.sort()

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

Submission status: Accepted

Solution code:
# coding: utf-8

(n,k) = map(int, input().rstrip().split(" "))

tree = list(sorted([int(input().rstrip()) for i in range(n)]))
ans = min([tree[i+k-1] - tree[i] for i in range(n-k+1)])
print(ans)

Submission status: Accepted

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

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

print(min(diff))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

mh = int(1e9) + 1
for l_idx in range(len(hs) - k + 1):
    r_idx = l_idx + k - 1
    mh = min(mh, hs[r_idx] - hs[l_idx])
print(mh)

Submission status: Accepted

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

Submission status: Accepted

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

h = np.array(h)
h_sort = np.sort(h)
h_shift = np.roll(h_sort, -K+1)
dif = (h_shift - h_sort)[:-K+1]
ans = np.min(dif[dif>=0])
print(int(ans))

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

diff = [[] for i in range(n-1)]
for i in range(n-1):
    diff[i] = h[i+1]-h[i]

t = [[] for i in range(n-1)]
t[0] = diff[0]
for i in range(1,n-1):
    t[i] = t[i-1]+diff[i]
t = [0] + t

# print(h)
# print(diff)
# print(t)

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,K,*h=map(int,open(0).read().split())
h.sort()
if K == N:
    print(h[-1] - h[0])
else:
    dif_h=[]
    for i in range(N-1):
        dif_h.append(h[i+1] - h[i])
    _min = base = sum(dif_h[:K-1])
    for i in range(K-1,N-1):
        base += dif_h[i]
        base -= dif_h[i-K+1]
        _min = min(base,_min)
    print(_min)