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():
    # n = int(input())
    n, m = map(int, input().split())
    # v = list(map(int, input().split()))
    # s = input()
    h = [int(input()) for _ in range(n)]

    h.sort()

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

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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from sys import stdin
from collections import defaultdict
input = stdin.readline
# ~ T = int(input())
T = 1
for t in range(1,T + 1):
	n,k = map(int,input().split())
	_input = []
	for i in range(n):
		_input.append(int(input()))
	_input.sort()
	min_ans = 10000000000
	for i in range(k - 1,len(_input)):
		min_ans = min(min_ans,_input[i] - _input[i - k + 1])
	print(min_ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# import sys
# sys.setrecursionlimit(10 ** 6)
# import bisect
# from collections import deque
# from decorator import stop_watch
#
#
# @stop_watch
def solve(N, K, h):
    h.sort()
    ans = 10 ** 9
    for i in range(K - 1, N):
        ans = min(ans, h[i] - h[i - (K - 1)])
    print(ans)


if __name__ == '__main__':
    N, K = map(int, input().split())
    h = [int(input()) for _ in range(N)]
    solve(N, K, h)

    # # test
    # from random import randint
    # from func import random_str
    # N, K = 10 ** 5, 10 ** 5 // 5
    # h = [randint(1, 10 ** 9) for _ in range(N)]
    # solve(N, K, h)

Submission status: Accepted

Solution code:
import math
from math import gcd,pi,sqrt
INF = float("inf")

import sys
sys.setrecursionlimit(10**6)
import itertools
from collections import Counter,deque
def i_input(): return int(input())
def i_map(): return map(int, input().split())
def i_list(): return list(i_map())
def i_row(N): return [i_input() for _ in range(N)]
def i_row_list(N): return [i_list() for _ in range(N)]
def s_input(): return input()
def s_map(): return input().split()
def s_list(): return list(s_map())
def s_row(N): return [s_input for _ in range(N)]
def s_row_str(N): return [s_list() for _ in range(N)]
def s_row_list(N): return [list(s_input()) for _ in range(N)]


def main():
    n, k = i_map()
    h = [i_input() for i in range(n)]
    h.sort()
    print(min(h[i+k-1] - h[i] for i in range(n-k+1)))


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

h.sort()

ans = 1000000000

for j in range(n-k+1):
    s = h[j+k-1] - h[j]
    if s < ans:
        ans = s

print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

n, k = map(int, input().split())
h = [0] * n
for i in range(n):
    h[i] = int(input())
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:
import bisect,collections,copy,itertools,math,string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())
def main():
    n, k = LI()
    h = [I() 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)
main()

Submission status: Accepted

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

Submission status: Accepted

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

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):
    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=list(int(input()) for i in range(n))
h.sort()

c=[]

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

print(min(c))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sun Sep 13 03:08:56 2020

@author: liang
"""

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

Submission status: Accepted

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

n,k = map(int, input().split())
h_list = []

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

sort_h_list = sorted(h_list)

L_index = 0
R_index = 0
L_value = sort_h_list[0]
R_value = sort_h_list[0]
count = 0
ans = 10**9

#尺取り法ぽいの
while (R_index < n):
    if (count < k):
        count += 1
        R_value = sort_h_list[R_index]
        if count == k:
            ans = min(ans,R_value - L_value)
        R_index += 1

    else:
        count -= 1
        L_index += 1
        L_value = sort_h_list[L_index]

print(ans)

Submission status: Accepted

Solution code:
import sys


# \n
def input():
    return sys.stdin.readline().rstrip()


def main():
    N, K = map(int, input().split())


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

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

    print(mm)


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

Submission status: Accepted

Solution code:
import numpy as np

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

sH = np.array(sorted(H), dtype=np.int64)
shift = sH[K-1:]
diff = shift - sH[:len(shift)]
ans = min(diff)
print(ans)

Submission status: Accepted

Solution code:
import sys
# sys.setrecursionlimit(100000)


def input():
    return sys.stdin.readline().strip()


def input_int():
    return int(input())


def input_int_list():
    return [int(i) for i in input().split()]


def main():

    n, k = input_int_list()
    H = []
    for _ in range(n):
        H.append(input_int())
    H = sorted(H)
    ans = float("inf")
    for i in range(n - k + 1):
        ans = min(ans, H[i + k - 1] - H[i])
    print(ans)

    return


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()))
    
h.sort()
h.reverse()

ls = []

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

print(min(ls))

Submission status: Accepted

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

sab = sorted(ab)

res = 10**10

for i in range(n-k+1):
    res = min(res,sab[i+k-1]-sab[i])

print(res)

Submission status: Accepted

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

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

print(result)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
# row = [int(x) for x in input().rstrip().split(" ")]
# n = int(input().rstrip())
# s = input().rstrip()

def resolve():
    import sys
    input = sys.stdin.readline

    n, k = [int(x) for x in input().rstrip().split(" ")]

    h_list = [int(input().rstrip()) for _ in range(n)]
    h_list.sort()
    ans = 1000000000
    for i in range(n - k + 1):

        val = h_list[i+k-1] - h_list[i]
        if val < ans:
            ans = val
    print(ans)


if __name__ == "__main__":
    resolve()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
L = []
mini = float('inf')

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

L.sort(reverse=True)

for i in range(len(L)-K+1):
    if L[i] - L[i+K-1] < mini:
        if L[i] - L[i+K-1] == 0:
            print(0)
            exit()
        else:
            mini = L[i] - L[i+K-1]

print(mini)

Submission status: Accepted

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

hl.sort()
ans = 10000000001

for j in range(n-k+1):
    s = hl[j+k-1] - hl[j]
    if s < ans:
        ans = s
    else:
        pass
print(ans)

Submission status: Accepted

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

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

print(min(H))

Submission status: Accepted

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

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

print(m)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

def solve():
    N, K = [int(i) for i in input().split()]
    H = []
    for _ in range(N):
        H.append(int(input()))
    H.sort()
    ans = sys.maxsize
    for i in range(N - K + 1):
        ans = min(ans, H[i + K - 1] - H[i])
    print(ans)

if __name__ == "__main__":
    solve()

Submission status: Accepted

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

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

trees.sort()
ans = float('inf')

for i in range(n - k + 1):
  diff = trees[i + k - 1] - trees[i]
  if ans > diff:
    ans = diff

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
lis = []
ans = 1000000000000000000

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

lis.sort()

for i in range(n):
  if i+k-1 >= len(lis):
    break
  else:
    mi = abs(lis[i] - lis[i+k-1])
    ans = min(ans, mi)

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

    min_diff = 10 ** 9

    for i in range(n - k + 1):
        diff = h_list[i] - h_list[i + k - 1]
        if diff < min_diff:
            min_diff = diff

    print(min_diff)


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

Submission status: Accepted

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

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

Submission status: Accepted

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

a.sort()
ans=100000000000000000000000

for j in range(N-K+1):
    if a[j+K-1]-a[j]<ans:
        ans=a[j+K-1]-a[j]


print(ans)

Submission status: Accepted

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

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

h = sorted(h)

for j in range(N-K+1):
    if ans > h[j+K-1]-h[j]:
        ans = h[j+K-1]-h[j]
print(ans)

Submission status: Accepted

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

hs = sorted(hs)

diff = 10**12
for i in range(n-k+1):
    diff = min(diff, hs[i+k-1] - hs[i])

print(diff)

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
s = []
for i in range(n):
    s.append(int(input()))
s.sort()
s.reverse()
ans = s[0]-s[-1]
for i in range(n-k+1):
    ans = min(ans,s[i]-s[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)])
list1 = []

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

print(min(list1))

Submission status: Accepted

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

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

print(min(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 = H[-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 = []
min_dif = 1000000000000000000000000

for n in range(N):
  h = int(input())
  H.append(h)

H.sort()

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

Submission status: Accepted

Solution code:

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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 = sorted([int(input()) for n in range(N)])
ans = H[-1]-H[0]

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

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
H=[]
for _ in range(N):
    H.append(int(input()))
H.sort()
min = 1e12
for i in range(N-(K-1)):
    diff = H[i+K-1] - H[i]
    if diff < min:
        min = diff

print(min)

Submission status: Accepted

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

print(min(tmp))

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())
tree = sorted([int(input())for i in range(N)])
d = 10**18  


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

Submission status: Accepted

Solution code:
S,K=list(map(int, input().split()))
H=[]
for _ in range(S):
  H.append(int(input()))
H.sort()
INF=float('inf')
a=[INF]*(S-K+1)
for i in range(S-K+1):
  a[i]=H[i+K-1]-H[i]
print(min(a))

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+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()
#小さい順に並べられたk本の木の、一番低い奴と高い奴の高さの差
dh = [int(1e9)]*n
for i in range(k-1,n):
    dh[i]=h[i]-h[i-k+1]
    
print(min(dh))

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()
#print(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:
N, K = map(int, input().split())
H = [int(input()) for _ in range(N)]
H.sort(reverse=True)

min_h = float('inf')
for i in range(N-K+1):
    temp = H[i] - H[i+K-1]
    min_h = min(min_h, temp)


print(min_h)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#C - Christmas Eve
N,K = map(int,input().split())
h = [int(input()) for _  in range(N)]
h = sorted(h, reverse = True)

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

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
h=[]
ma=10**9
for i in range(n):
  a=int(input())
  h.append(a)
h.sort()

for i in range(n-k+1):
  sa=h[k-1+i]-h[i]
  if sa<ma:
    ma=sa
    
print(ma)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

ans = h[N-1]
for i in range(0, 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())
A = [int(input()) for _ in range(N)]
B = sorted(A)
ans = []
for i in range(N-K+1):
  ans.append(B[i+K-1]-B[i])
print(min(ans))

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
A = [int(input()) for _ in range(N)]
B = sorted(A)
ans = []
flag = 0
for i in range(N-K+1):
  ans.append(B[i+K-1]-B[i])
  flag = 1
print(min(ans) if flag == 1 else "0")

Submission status: Accepted

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

Submission status: Accepted

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

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    N, K, *H = map(int, read().split())

    H.sort()

    ans = INF
    for i in range(N - K + 1):
        if ans > H[i + K - 1] - H[i]:
            ans = H[i + K - 1] - H[i]
    print(ans)
    return


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

h = sorted(h)
ans = 10**9
tmp = 0

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

Submission status: Accepted

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

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

h.sort()

s = h[-1] - h[1]

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

print(s)

Submission status: Accepted

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

nums = [0]*(n-1)
for i in range(n-1): nums[i] = h[i+1]-h[i]

ans = sum(nums[:k-1])
t = sum(nums[:k-1])
for i in range(k-1,n-1):
  t += nums[i] - nums[i-k+1]
  if t < ans: ans = t
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
h=[int(input()) for i 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 = []
for i in range(N):
    h = int(input())
    H.append(h)
A = []
H = sorted(H)
for i in range(N - K + 1):
    x = H[i + K - 1] - H[i]
    A.append(x)

A = sorted(A)
ans = A[0]
print(ans)

Submission status: Accepted

Solution code:
N, K = list(map(lambda k: int(k), input().split(" ")))
H = [int(input()) for i in range(N)]
H.sort()

print(min([H[i + K - 1] - H[i] for i in range(len(H) - K + 1)]))

Submission status: Accepted

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

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

print(min(l))

Submission status: Accepted

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

Submission status: Accepted

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

h.sort()
ans=10**9+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().strip().split(" "))
ar=[]
for i in range(n):
    ar.append(int(input()))
ar.sort()
m=999999999999999

for i in range(n-k+1):

    c=ar[i+k-1]-ar[i]
    if c<m:
        m=c
print(m)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
h = [int(input()) for _ in range(N)]
h.sort()
ans = max(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:
import numpy as np

N,K,*H = map(int,open(0).read().split())
H =np.array(sorted(H))

H = np.diff(H,n=1)
minH = 1e10

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

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_ = h[i + K - 1] - h[i]
    ans = min(ans, ans_)

print(ans)

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

Submission status: Accepted

Solution code:
import sys


inint = lambda: int(sys.stdin.readline())
inintm = lambda: map(int, sys.stdin.readline().split())
inintl = lambda: list(inintm())
instrm = lambda: map(str, sys.stdin.readline().split())
instrl = lambda: list(instrm())

n, k = inintm()

h = []
ans = 10**10

for _ in range(n):
    h.append(inint())

h.sort()

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
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
read = sys.stdin.buffer.read
sys.setrecursionlimit(10 ** 7)
INF = float('inf')

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

Submission status: Accepted

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

hmax = 0
hmin = 0
ans = 10**10
for i in range(N - K + 1):
    hmax = h[i + K - 1]
    hmin = h[i]
    if hmax - hmin < ans:
        ans = hmax - hmin
print(ans)

Submission status: Accepted

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

print(min(salist))

Submission status: Accepted

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

ans = 10**9
l.sort()

for i in range(n-k+1):
    tmp = l[k+i-1] - l[i]
    if tmp < ans:
        ans = tmp

print(ans)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
h = sorted(int(input()) for _ in range(n))
print(min(r - l for l, r in zip(h[: 1 - k], h[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))
print(min(r - l for l, r in zip(h[: 1 - k], h[k - 1 :])))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
if __name__ == '__main__':

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

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

	B = sorted(A,reverse=True)

	ans = 10**9
	for j in range(n-k+1):
		tmp = B[j] - B[j+k-1]
		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()
diff=[0]*(n-k+1)
for m in range(n-k+1):
  diff[m] = h[m+k-1]-h[m]
print(min(diff))

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()
diff=[]
for i in range(N-K+1):
  diff.append(h[i+K-1]-h[i])
print(min(diff))

Submission status: Accepted

Solution code:
# AtCoder Beginner Contest 115
# C - Christmas Eve

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

TREES=[]

for i in range (N):
    TREES.append(int(input()))
# print(TREES)

TREES.sort()
# print(TREES)

diff=[0]
for i in range (1,N):
    if i <K:
        d=abs(TREES[i]-TREES[0])
    else:
        d=abs(TREES[i]-TREES[i-K+1])
    diff.append(d)

# print(diff)
print(min(diff[K-1:]))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
H=[int(input()) for _ 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:
import sys
import numpy as np

stdin = sys.stdin
def ns(): return stdin.readline().rstrip()
def ni(): return int(stdin.readline().rstrip())
def nm(): return map(int, stdin.readline().split())
def nl(): return list(map(int, stdin.readline().split()))


def main():
    n, k = nm()
    H = [ni() for _ in range(n)]
    H = np.array(sorted(H))
    dif = H[1:] - H[:-1]
    cum_dif = np.append(0, np.cumsum(dif))
    ans = cum_dif[k - 1:] - cum_dif[:-(k - 1)]
    print(np.min(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()

res = H[-1] - H[0]

for i in range(N - K + 1):
    t1 = H[i]
    t2 = H[i + K - 1]
    res = min(res, abs(t2 - t1))

print(res)

Submission status: Accepted

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

Submission status: Accepted

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

hold = 0
ans = 10 ** 9 + 1
for i in range(K-1,N):
    hold = H[i] - H[i-(K-1)]
    ans = min(ans, hold)
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(reverse=True)

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

res = min(l)
print(res)