Problem:


Score : 300 points

Problem Statement

In some other world, today is Christmas Eve.

There are N trees planted in Mr. Takaha's garden. The height of the i-th tree (1 \leq i \leq N) is h_i meters.

He decides to choose K trees from these trees and decorate them with electric lights. To make the scenery more beautiful, the heights of the decorated trees should be as close to each other as possible.

More specifically, let the height of the tallest decorated tree be h_{max} meters, and the height of the shortest decorated tree be h_{min} meters. The smaller the value h_{max} - h_{min} is, the better. What is the minimum possible value of h_{max} - h_{min}?

Constraints

2 \leq K < N \leq 10^5

1 \leq h_i \leq 10^9

h_i is an integer.

Input

Input is given from Standard Input in the following format:

N K
h_1
h_2
:
h_N

Output

Print the minimum possible value of h_{max} - h_{min}.

Sample Input 1

5 3
10
15
11
14
12

Sample Output 1

2

If we decorate the first, third and fifth trees, h_{max} = 12, h_{min} = 10 so h_{max} - h_{min} = 2. This is optimal.

Sample Input 2

5 3
5
7
5
7
7

Sample Output 2

0

If we decorate the second, fourth and fifth trees, h_{max} = 7, h_{min} = 7 so h_{max} - h_{min} = 0. This is optimal.

There are not too many trees in these sample inputs, but note that there can be at most one hundred thousand trees (we just can't put a sample with a hundred thousand lines here).



Sample input:
5 3
10
15
11
14
12


Sample output:
2

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main112():
    N, K = (int(x) for x in input().split())
    height = []

    for n in range(N):
        height.append(int(input()))

    height = sorted(height)

    min_h = 10 ** 10

    for h in range(len(height) - K + 1):
        focus = height[h + K -1] - height[h]
        if focus < min_h:
            min_h = focus

    print(min_h)

main112()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
from collections import Counter
 
def main():
    input = sys.stdin.readline
    
    n, m = map(int, input().split())
    h = [int(input()) for i in range(n)]
    
    h.sort()
    
    #一番大きい値を初期値に
    min = h[len(h) - 1]
    
    #差が一番小さい区間を探す
    for i, v in enumerate(h):
        
        ku = i + (m - 1)
        if ku < len(h):
            sa = h[ku] - h[i]
            min = min if min < sa else sa
        else:
            break
    
    print(min)
    
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 = []
for i in range(n-k+1):
    ans.append(H[i+k-1]-H[i])
print(min(ans))

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!

N, K = map(int, input().rstrip().split(' '))
h = sorted([int(input()) for x in range(N)])

min_cost = 1e10
for i in range(N - K + 1):
    cost = h[i+K-1] - h[i]
    if cost < min_cost:
        min_cost = cost
        
print(min_cost)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
LH=[]
ans=10000000000
for i in range(N):
    LH.append(int(input()))
LH.sort()
for i in range(N-K+1):
    num=LH[K-1+i]-LH[i]
    if ans>num:
        ans=num
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):
    if h[i+K-1]-h[i]<ans:
        ans=h[i+K-1]-h[i]
print(ans)

Submission status: Accepted

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

h = sorted(h)

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

Submission status: Accepted

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

Submission status: Accepted

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

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

print(minh)

Submission status: Accepted

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

Submission status: Accepted

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

for i in range(n-k+1):
	intervals.append(heights[i+k-1] - heights[i])
print(min(intervals))

Submission status: Accepted

Solution code:
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
N, K = [int(x) for x in input().split()]
height = []
for _ in range(N):
    height.append(int(input()))

height.sort()
height.reverse()
sublist = []
for i in range(N-K+1):
    hmaxhmin = height[i]-height[i+K-1]
    sublist.append(hmaxhmin)

print(min(sublist))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
INF = 1000000007

input = open(0).read

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


def main():

    h.sort()

    ans = INF

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

    print(ans)
    return


main()

Submission status: Accepted

Solution code:
def ans():
  N, K = map(int,input().split())
  H = sorted([int(input()) for _ in range(N)])
  res = 1e9+7
  for i in range(N-K+1):
    x = H[i+K-1] - H[i]
    if(res > x):
      res = x
  print(res)
ans()

Submission status: Accepted

Solution code:
n,k=[int(i) for i in input().split()]
h=sorted([int(input()) for i in range(n)])
mini=100000000000
for i in range(n-k+1):
    if h[i+k-1]-h[i]<mini:
        mini=h[i+k-1]-h[i]
print(mini)

Submission status: Accepted

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

print(ans)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
Hi = []
for i in range(N):
    h = int(input())
    Hi.append(h)
Hi.sort()
min_diff = 100000000000
start_idx = 0
for i in range(N - K + 1):
    diff = Hi[i + K - 1] - Hi[i]
    if diff < min_diff:
        min_diff = diff
        start_idx = i
print(Hi[start_idx + K - 1] - Hi[start_idx])

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np
 
N,K=list(map(int,input().split()))
h=np.zeros(N)
for i in range(N):
    h[i]=int(input())
    
h=np.sort(h)
dh_min=10**9
for i in range(N-K+1):
    dh=h[i+K-1]-h[i]
    if dh<dh_min:
        dh_min=dh
print(int(dh_min))

Submission status: Accepted

Solution code:
n,k,*a=map(int,open(0).read().split());a.sort();print(min(a[s+k-1]-a[s]for s in range(n-k+1)))

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def christmas_eve(N: int, K: int, H: list) -> int:
    sorted_H = sorted(H, reverse=True)
    return min(sorted_H[i] - sorted_H[i + K - 1] for i in range(N - K + 1))


if __name__ == "__main__":
    N, K = [int(s) for s in input().split()]
    H = [int(input()) for _ in range(N)]
    ans = christmas_eve(N, K, H)
    print(ans)

Submission status: Accepted

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

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

h_arr.sort()
first_hd = h_arr[K-1]-h_arr[0]
min_hd = first_hd
for i in range(K-1, N):
    current_hd = h_arr[i] - h_arr[i-K+1]
    min_hd = min(min_hd, current_hd)

print(min_hd)

Submission status: Accepted

Solution code:

s=raw_input().split();
n=int(s[0])
k=int(s[1])

lt=[]*n;
for i in range(0,n):
	a=int(raw_input())
	lt.append(a)
lt.sort()
mx=1000000000000000
i=0; j=k-1;
while j<n:
	l=lt[j]-lt[i]
	j+=1;
	i+=1;
	if l<mx:
		mx=l;
print(mx)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, K = [int(x) for x in input().split()]
h = [int(input()) for x 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:
INF = 1000000007

N, K, *h = map(int, open(0).read().split())

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:
INF = 1000000007

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)

Submission status: Accepted

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

h.sort()

m = 10**10

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

print(m)

Submission status: Accepted

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

list = [int(input()) for i in range(N)]
list.sort()
min_val = 10**9
for i in range(N-K+1):
    min_val = min(min_val,list[i+K-1]-list[i])

print(min_val)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

hs = sorted(hs)

min_diff = float('inf')
for i in range(len(hs) - k + 1):
    min_diff = min(min_diff, hs[i+k-1]-hs[i])

print(min_diff)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

# sort
h_sorted = np.sort(hs)
min_delta = np.inf

# hmax-hminを求めていく
for i in range(N-K+1):
    delta = h_sorted[i+K-1] - h_sorted[i]
    if min_delta >= delta:
        min_delta = delta
print(int(min_delta))

Submission status: Accepted

Solution code:
# encoding:utf-8

import copy
import numpy as np
import random

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

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

h.sort()
# print(h)
max_tree = []
min_tree = []
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:
ri = lambda: int(input())
rl = lambda: list(map(int,input().split()))

N,K=rl()
h=[0]*N
for i in range(N):
    h[i] = ri()

h.sort()
s = h[K-1]-h[0]
ans = s

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

print(ans)

Submission status: Accepted

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

val = 1000000000

for i in range(n - k + 1):
	if val >= h[i] - h[i + k - 1]:
		val = h[i] - h[i + k - 1]
print(val)

Submission status: Accepted

Solution code:
import math, string, itertools, fractions, heapq, collections, re,  array, bisect, sys, random, time, copy, functools


sys.setrecursionlimit(10**7)
inf = 10 ** 20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]


def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    N, K = LI()
    h = []
    [h.append(I()) for i in range(N)]

    h = sorted(h)
    #  print(h)
    h_diff = [h[i+K-1] - h[i] for i in range(N-K+1)]
    print(min(h_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()
#for i in range(n-k+1):
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()
tmax=10**9
for a in range(N-K+1):
    h1,h2=H[a],H[a+K-1]
    tmax=min(tmax,abs(h1-h2))
    if(tmax==0):
        break
print(tmax)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

h.sort()
min_diff = 10 ** 10
for i in range(N - K + 1):
    diff = h[i + K - 1] - h[i]
    if diff < min_diff:
        min_diff = diff
print(min_diff)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
l=[int(input()) for _ in range(n)]
s=10**9
v=0
l.sort()
while v<n-k+1:
    m=l[v+k-1]-l[v]
    s=min(s,m)
    v+=1
print(s)

Submission status: Accepted

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

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())
height = [int(input()) for i in range(N)]

height.sort()
h_min = []
for i in range(N - K + 1):
    h_min.append(height[K - 1 + i] - height[i])
    if i > 0:
        if h_min[0] >= h_min[1]:
            del h_min[0]
        else:
            del h_min[1]

print(h_min[0])

Submission status: Accepted

Solution code:

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

a.sort()
min_high=1000000000000

for i in range(n-k+1):
    high=-a[i]+a[i+k-1]
#    print(high,a[i],i)
    if high<min_high:
        min_high=high

print(min_high)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
def main():
    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)]))
        
    
if __name__ == '__main__':
    main()

Submission status: Accepted

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

h = list(sorted(h))

ans = min(hr - hl for hr, hl in zip(h[K - 1:], h))
print(ans)

Submission status: Accepted

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

h=sorted(h)
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, *trees = map(int, open(0).read().split())
trees.sort()
print(min(trees[i+K-1] - trees[i] for i in range(N-K+1)))

Submission status: Accepted

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

Submission status: Accepted

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

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

print(result)

Submission status: Accepted

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

min_dist = trees[K-1] - trees[0]
for i in range(N-K+1):
    dist = trees[i+K-1] - trees[i]
    if min_dist > dist:
        min_dist = dist

print(min_dist)

Submission status: Accepted

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

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:
s=raw_input().split(' ')
n=int(s[0])
k=int(s[1])

ar=[]
for i in range(0,n):
	j=int(input())
	ar.append(j)

ar.sort()
i=0; j=k-1;
mn=100000000000
while j<n:
	if ar[j]-ar[i]<mn:
		mn=ar[j]-ar[i]
	j+=1; i+=1;
print(mn);

Submission status: Accepted

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

trees.sort()

print(min([max_h-min_h for min_h,max_h in zip(trees,trees[K-1:])]))

Submission status: Accepted

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

def solve(N,K,trees):
    trees.sort()
    
    print(min([max_h-min_h for min_h,max_h in zip(trees,trees[K-1:])]))
    return

solve(N,K,trees)

Submission status: Accepted

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

def solve(N,K,trees):
    trees.sort()
    
    diffs = [max_h-min_h for min_h,max_h in zip(trees,trees[K-1:])]
    ans = min(diffs)
    print(ans)
    return

solve(N,K,trees)

Submission status: Accepted

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

Submission status: Accepted

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

def solve(N,K,trees):
    trees.sort()

    diffs = []
    for i,h in enumerate(trees[:N-K+1]):
        min_h = h
        max_h = trees[i+K-1]
        #print(min_h,max_h)
        diffs.append(max_h-min_h)

    print(min(diffs))
    return

solve(N,K,trees)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

H.sort()

#print(H)

ans=H[n-1]-H[0]

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())
hlist = []
for i in range(n):
    hlist.append(int(input()))
hlist.sort()
h = []
for j in range(n-k+1):
    h.append(hlist[j+k-1]-(hlist[j]))
print(min(h))

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
h=[int(input()) for i in range(N)]
h=sorted(h)
#サイズKのスライディングウィンドウ
def_list = [h[i+K-1]-h[i] for i in range(N-K+1)]
print(min(def_list))

Submission status: Accepted

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

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

print(min(ans))

Submission status: Accepted

Solution code:
NK=input()
ListNK=NK.split() #ListNK[0]=N,[1]=K
N=int(ListNK[0])
K=int(ListNK[1])
Listh=[]
ListQ=[]
for i in range(0,N,1):
  hi=int(input())
  Listh.append(hi)

Listh.sort() #Listhの要素を小さい順に並べる
for i in range(0,N-K+1,1):
  Q=int(Listh[K+i-1]-Listh[i]) #hmax-hminの候補.Qminを求めればよい
  ListQ.append(Q)
ListQ.sort()
print(ListQ[0])

Submission status: Accepted

Solution code:

# coding: utf-8

# In[3]:


import numpy as np


# In[38]:


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


# In[43]:


h = np.array(h)
sorted_h = np.sort(h)
print(min(sorted_h[k-1:] - sorted_h[:-k+1]))

Submission status: Accepted

Solution code:
import numpy as np
n, k = list(map(int, input().split()))
hs = []
for _ in range(n):
    hs.append(int(input()))

hs.sort()
hs = np.array(hs)
ans = hs[k - 1:] - hs[:-(k - 1)]
ans = ans.min()
print(ans)

Submission status: Accepted

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

hs.sort()
ans = min(hs[i + k - 1] - hs[i] for i in range(n - k + 1))

print(ans)

Submission status: Accepted

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

hs.sort()
ans = min(i - j for i, j in zip(hs[k - 1:], hs))

print(ans)

Submission status: Accepted

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

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()
m = 1000000000
for i in range(N-K+1):
  if(m>H[i+K-1]-H[i]):
    m = H[i+K-1]-H[i]
print(m)

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

min_diff = 10**9 + 1

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

print(min_diff)

Submission status: Accepted

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

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

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

print(min(ans))

Submission status: Accepted

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

list = sorted(list_original)
sub = 10000000000

for j in range(n-k+1):
    if list[j+k-1]-list[j] <= sub:
        sub =list[j+k-1]-list[j]
    else:
        pass

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[i-1+K]-H[i])

print(ans)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
hlist = sorted([int(input()) for _ in range(N)])
minimum = max(hlist)-min(hlist)

for n in range(N-K+1):
  minimum = min(minimum, hlist[n+K-1]-hlist[n])

print(minimum)

Submission status: Accepted

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

h = sorted(h)

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

Submission status: Accepted

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

h.sort()

d = []

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

print(min(d))

Submission status: Accepted

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

Submission status: Accepted

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

min = 1000000000

h.sort()

for i in range(0, len(h)-k+1):
    diff = h[i+k-1] - h[i]
    if diff < min:
        min = diff

print(min)

Submission status: Accepted

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

m = []
h.sort()
for i in range(len(h)-k+1):
    m.append(h[i+k-1] - h[i])
print(sorted(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()
a=[]
for i in range(n-k+1):
    a.append(h[i+k-1]-h[i])
a.sort()
print(a[0])

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
height_list = list()
for i in range(N):
    height_list.append(int(input()))
height_list.sort(reverse=True)
diff_min = 0
for i in range(N-K+1):
    h_max = height_list[i]
    h_min = height_list[i+K-1]
    if i == 0 or h_max-h_min <= diff_min:
        diff_min = h_max - h_min
print(diff_min)

Submission status: Accepted

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

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())
li=list(range(n))
for i in range(n):
    li[i]=int(input())
li.sort()
ans=100000000000
for i in range(n-k+1):
    if ans>(li[i+k-1]-li[i]):
        ans=li[i+k-1]-li[i]
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

min = h[-1]
for i in range(0, len(h)-int(k)+1):
  res = h[i+int(k)-1] - h[i]
  if min > res:
    min = res
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()
ans = 10**9
for i in range(N-K+1):
    h1 = h[i]
    h2 = h[i+K-1]
    ans = min(ans,h2-h1)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np

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

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

arr = np.sort(np.array(arr)) #昇順

print(
	(arr[(k-1):] - arr[:-(k-1)]).min() )