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 calc_min_diff(h, K):
        h.sort()
        min_h = h[K - 1] - h[0]
        for i in range(K, len(h)):
                min_h = min(min_h, h[i] - h[i - K + 1])
        return min_h


N, K = [int(v) for v in input().split()]
h = [int(input()) for _ in range(N)]
print(calc_min_diff(h, K))

Submission status: Accepted

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

trees = [int(input()) for i in range(n)]

trees.sort(reverse=True)

mindiff = trees[0]-trees[k-1]
for i in range(n-k+1):
    if(trees[i]-trees[i+k-1] <mindiff):
        mindiff = trees[i] - trees[i+k-1]
print(mindiff)

Submission status: Accepted

Solution code:
def merge(p, q):
    result=[]
    i,j=0,0
    #p[i]とq[j]を比べて小さい方をresultに加える。
    while i<len(p) and j<len(q):
        #.appendで加える。小さかった方の数字を一つ進める。
        #これで小さいものから順にresultに加えることができる。
        if p[i] <= q[j]:
            result.append(p[i])
            i+=1
        else:
            result.append(q[j])
            j+=1
    #片方の配列が全て比較済みとなると、そこで上のwhile分は終了してしまう
    #なのでまだ比較していない要素が残っている配列をそのままresultに連結する
    #これは.extendを使う。まだ比較していない要素はスライスで簡単に分けることができる。
    if i<len(p):result.extend(p[i:])
    if j<len(q):result.extend(q[j:])
    #resultを返して終了
    return result

# mergesort(a): 配列 a をマージソートしたものを返す。
def mergesort(a):
    #要素が一つになるまで分割したらそれ自体を返す。
    if len(a)==1:return a
    #最初の半分を切り取ってソート
    p=mergesort(a[:len(a)//2])
    #残りの半分を切り取ってソート
    q=mergesort(a[len(a)//2:])
    #merge関数で連結させる
    return merge(p,q)

N, K = map(int, input().split())
tree_list = mergesort([int(input()) for i in range(N)])

gap = mergesort([tree_list[i+K-1]-tree_list[i] for i in range(N-K+1)])
print(gap[0])

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/abc115/tasks/abc115_c

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

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

h.sort()

ans = int(1e15)

# print(h)

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

min_diff = 99999999999
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())

data_list = []
for i in range(N):
    data = int(input())
    data_list.append(data)
data_list.sort()

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

Submission status: Accepted

Solution code:
from sys import stdin
import numpy as np

N, K, *h = map(int, stdin.read().split())
h = np.array(h)
h.sort()

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

tree_list.sort()
min = 1145141919810
for i in range(K-1,N):

    if min > (tree_list[i] - tree_list[i-K+1]):
        min = tree_list[i] - tree_list[i-K+1]

print(min)

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

Submission status: Accepted

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

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

H.sort(reverse=True)

diff = list()
for n in range(N-K+1):
    diff.append(H[n] - H[n+K-1])

diff.sort()
print(diff.pop(0))

Submission status: Accepted

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

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

Submission status: Accepted

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

main()

Submission status: Accepted

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

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

print(min(diff))

Submission status: Accepted

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

for i in range(1,n+1) :
    h[i] = int(input())

h.sort()
c = h[n] #highest

for j in range(1,n-k+2) :
    d = h[j+k-1] - h[j]
    c = d + 0 if c > d else c + 0

print(c)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N, K = list(map(int, input().split()))
H=[]
for n in range(N):
    H.append(int(input()))
H.sort()
D = []
for i in range(N-(K-1)):
    D.append(abs(H[i+K-1] - H[i]))
print(min(D))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np

all_tree, sel_tree = map(int, input().split())
tree = []
for i in range(all_tree):
    tree.append(int(input()))

tree = np.array(tree)
tree = np.sort(tree)

highest = 10 ** 9
for begin in range(all_tree - sel_tree + 1):
    
    end = begin + sel_tree - 1
    diff = tree[end] - tree[begin]
    highest = min(highest, diff)
    #print(begin, end, highest, diff, tree[end], tree[begin])
    
print(highest)

Submission status: Accepted

Solution code:
List = input()
List = List.split()
N = int(List[0])
K = int(List[1])
H = []
for i in range(N):
    height = int(input())
    H.append(height)
H.sort()
distance = 0
minDis = H[K-1] - H[0]
for i in range(N-K+1):
    distance = H[i+K-1] - H[i]
    if distance < minDis:
        minDis = distance
print(minDis)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
a,b=input().split()
c,d=int(a),int(b)
l=[int(input()) for i in range(c)]
L=sorted(l)
x=L[d-1]-L[0]
for i in range(c-d+1):
    if L[d+i-1]-L[i]<x:
        x=L[d+i-1]-L[i]
print(x)

Submission status: Accepted

Solution code:
from heapq import heappush, heappop
import bisect

class Solver(object):
    def __init__(self):
        self.trees = []
        self.n, self.k = list(map(int, input().split(" ")))
        for _ in range(self.n):
            self.trees.append(int(input()))
            # heappush(self.trees, int(input()))
        self.trees.sort()

    def solve(self):
        global_min = self.trees[self.k-1] - self.trees[0]
        for i in range(1, self.n - self.k + 1):  # 0 .. n-k
            shortest = self.trees[i]
            tallest = self.trees[i+self.k-1]
            local_min = tallest - shortest
            if local_min < global_min:
                global_min = local_min
        print(global_min)


if __name__ == "__main__":
    s = Solver()
    s.solve()

Submission status: Accepted

Solution code:
#ABC115C-ChristmasEve.py

from sys import stdin

input = stdin.readline

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

h = [int(input()) for _ in range(N)]
h.sort()

hdiff = [None]*(N-K+1)

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

print(min(hdiff))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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):
    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()
tail = N - 1
minimumAnswer = h[tail]-h[0]
for i in range(N - K+1):
    if minimumAnswer > h[i+K-1]-h[i]:
        minimumAnswer = h[i+K-1]-h[i]
print(minimumAnswer)

Submission status: Accepted

Solution code:
def merge(a,b): #整列済みの配列aとbを併合
    r =[]
    #結果の格納先を用意
    ai = 0 #aは何番目まで処理したか?
    bi = 0 #bは何番目まで処理したか?
    for i in range(0, len(a)+len(b)):
        if (bi >= len(b) or (ai < len(a) and a[ai] < b[bi])):
         #aとbの先頭を比較(長さを超えないよう注意)
            r.append(a[ai])
            ai = ai + 1
        else:
            r.append(b[bi])
            bi = bi + 1 #aとbの先頭の小さい方をrへ
    return r
def mergesort(a): #配列aを整列
    n = len(a)
    if n <= 1: #短いときはそれでよし
        return a
    else:
        l = mergesort(a[0 : n//2])
        r = mergesort(a[n//2 : n])
        #前半と後半をそれぞれ整列
        return merge(l,r)
        #整列した結果を併合
N,K=map(int,input().split())
h=[]
for i in range(0,N):
    h.append(int(input()))
h_New=mergesort(h)
h_k=[]
for i in range(0,N-K+1):
    h_k.append(h_New[i+K-1]-h_New[i])
print(min(h_k))

Submission status: Accepted

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

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

sorted_hs = sorted(hs)

ret = None
for i in range(n-k+1):
    if ret is None or ret > sorted_hs[i+k-1] - sorted_hs[i]:
        ret = sorted_hs[i+k-1] - sorted_hs[i]

print(ret)

Submission status: Accepted

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

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 = H[-1] - H[0]
for i in range(len(H) - 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 = [int(input())for _ in range(n)]
h.sort()
h = [h[i + k - 1] - h[i] for i in range(n - k + 1)]

print(min(h))

Submission status: Accepted

Solution code:
n_tree, n_choice = [int(data) for data in input().split()]
heights = []
for i in range(n_tree):
  heights.append(int(input()))

diff = float("inf")
heights = sorted(heights)
for i in range(n_tree - n_choice + 1):
  h_min = heights[i]
  h_max = heights[i + n_choice - 1]
  if (h_max - h_min < diff):
    diff = h_max - h_min

print(diff)

Submission status: Accepted

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

a = []

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

print(min(a))

Submission status: Accepted

Solution code:
import numpy as np
n, k = (int(i) for i in input().split())

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

sorted_h = np.sort(h)
diff = np.hstack((sorted_h[k-1:], np.zeros(k-1)))

print(int(np.min(np.abs(diff - sorted_h)[:-k+1])))

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
hn = [int(input()) for i 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 = []
    for i in range(n):
        height = int(input())
        h.append(height)

    h.sort()

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

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import math
import sys
from operator import itemgetter

# N, K = map(int, input().split())
# h = list(map(int, input().split()))
# S = [input() for _ in range(H)]
# X = [[0 for j in range(100)] for i in range(200)]


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

if __name__ == '__main__':
	### input
	n, k = map(int, input().split())
	h = []
	for i in range(n):
		h.append(int(input()))
	###solve
	solve(n, k, h)

Submission status: Accepted

Solution code:
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache
import sys
sys.setrecursionlimit(10000)
INF = float("inf")
YES, Yes, yes, NO, No, no = "YES", "Yes", "yes", "NO", "No", "no"
dy4, dx4 = [0, 1, 0, -1], [1, 0, -1, 0]
dy8, dx8 = [0, -1, 0, 1, 1, -1, -1, 1], [1, 0, -1, 0, 1, 1, -1, -1]


def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W


def ceil(a, b):
    return (a + b - 1) // b


# aとbの最大公約数
def gcd(a, b):
    if b == 0:
        return a
    return gcd(b, a % b)


# aとbの最小公倍数
def lcm(a, b):
    g = gcd(a, b)
    return a / g * b


def main():
    N, K = map(int, input().split())
    H = list(sorted([int(input()) for _ in range(N)]))
    ans = INF
    for i in range(N - K + 1):
        ans = min(ans, H[i + K - 1] - H[i])
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys

def main():
  input = sys.stdin.readline
  N, K = map(int, input().split())
  H = [int(input()) for _ in range(N)]
  H = sorted(H)
  ans = max(H) - min(H)
  for i in range(N-K+1):
    res = H[i+K-1] - H[i]
    ans = min(ans, res)
  print(ans)


if __name__ == '__main__':
  main()

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
tree = []
for i in range(n):
    tree.append(int(input()))
tree.sort()
num_m=10**10
for i in range(n-m+1):
    num_m = min(num_m,tree[i+m-1]-tree[i])
print(num_m)

Submission status: Accepted

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

Submission status: Accepted

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

def main():
  n, k = map(int, input().split())
  hs = [int(input()) for _ in range(n)]
  hs.sort()
  _min = hs[-1]
  for i in range(n-k+1):
    kouho = hs[i+k-1] - hs[i]
    if kouho < _min:
      _min = kouho
  print(_min)
  pass

if __name__=='__main__':
  main()

Submission status: Accepted

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

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

for i in range(n-k+1):
    hmin = H[i]
    hmax = H[i+k-1]
    d.append(hmax - hmin)

print(min(d))

Submission status: Accepted

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

Submission status: Accepted

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

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

l = sorted(l)
  

base_d = l[k-1] - l[0]

for i in range(n-k+1):
  d = l[i+k-1] - l[i]
  if base_d >= d:
    base_d = d
    
    
print(base_d)

Submission status: Accepted

Solution code:
import copy
n, k = [int(i) for i in input().split()]

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

h_arr = sorted(h_arr)
r = 10**10
for i in range(n-k+1):
    r = min(r,h_arr[i+k-1]-h_arr[i])

print(r)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#115-C
n,k = (int(i) for i in input().split())
t = [int(input()) for i in range(n)] 
#t = [10,15,11,14,12]
t = sorted(t)
min = t[k-1]-t[0]
for i in range(n-k+1):
    s = t[i+k-1]-t[i]
    if s < min:
        min = s

print(min)
#print(t)

Submission status: Accepted

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

H.sort(reverse = True)
ans = H[0] - H[0 + K - 1]
for i in range(N - K + 1):
    if H[i] - H[i + K - 1] < ans:
        ans = H[i] - H[i + K - 1]

print(ans)

Submission status: Accepted

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

H = []

D = []

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

H.sort()
#print(H)

for j in range(N-K+1):
	D.append(H[j+K-1] - H[j])

print(min(D))

Submission status: Accepted

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

H.sort()

ans = 10**100

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

Submission status: Accepted

Solution code:
import numpy as np

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

H.sort()

ans = float('inf')
for i in range(n):
    if i+k>n:
        break
    tmp = abs(H[i+k-1]-H[i])
    if ans>tmp:
        ans = tmp

print(ans)

Submission status: Accepted

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

heights = []
for i in range(N-K+1):
  heights.append(H[i+K-1]-H[i])
print(min(heights))

Submission status: Accepted

Solution code:

if __name__ == '__main__':
    n,k=map(int, input().split())
    l = sorted(list(map(int, [input() for i in range(n)])),reverse=False)
    # print(l)
    ans=1000000000
    for i in range(n-k+1):
        m=int(l[i+k-1]-l[i])
        if ans>m:
            ans=m
    print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
l = map(long, raw_input().split())

height = []
for i in range(l[0]):
    height.append(long(input()))
    
height.sort()

diff = 1000000000000L
for x in range(len(height)-l[1]+1):
  if diff > height[x+l[1]-1] - height[x]:
    diff = height[x+l[1]-1] - height[x]
    
print diff

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

def main():
    [N, K] = list(map(int, input().split()))
    h = []
    for i in range(N):
        h.append(int(input()))

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


    print(min_d)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n_k = list(map(int, input().split()))
n, k = n_k[0], n_k[1]
tree_list = []
for i in range(n):
    tree_list.append(int(input()))
tree_list.sort()
min = 1000000001
for i in range(n-k+1):
    if tree_list[i+k-1]-tree_list[i] <= min:

        min = tree_list[i+k-1]-tree_list[i]
print(int(min))

Submission status: Accepted

Solution code:
n_k = list(map(int, input().split()))
n, k = n_k[0], n_k[1]
tree_list = []
for i in range(n):
    tree_list.append(int(input()))
tree_list.sort()
min = 999999999
for i in range(n-k+1):
    if tree_list[i+k-1]-tree_list[i] <= min:

        min = tree_list[i+k-1]-tree_list[i]
print(int(min))

Submission status: Accepted

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

err_list = [h_list[i+K-1] - h_list[i] for i in range(N-K+1)]
print(min(err_list))

Submission status: Accepted

Solution code:
def run(N, K, h):
    h = sorted(h)
    diff = [h[i+1] - h[i] for i in range(N-1)]
    diff_all = max(h) - min(h)
    r_diff_sum = [0]
    for d in diff:
        r_diff_sum.append(r_diff_sum[-1] + d)
    l_diff_sum = [0]
    for d in reversed(diff):
        l_diff_sum.append(l_diff_sum[-1] + d)
    ret = diff_all
    # 右から何個を除外するか
    for i in range(N - K + 1):
        ret = min(ret, diff_all - r_diff_sum[i] - l_diff_sum[N - K - i])
    return ret


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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
start = list(map(int, input().split()))
n,k = start[0], start[1]

lst = [int(input()) for i in range(n)]

lst.sort()
lst.reverse()

diff_lst = list()
for i in range(n-k+1):
    diff_lst.append(lst[i] - lst[i+k-1])
print(min(diff_lst))

Submission status: Accepted

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

Submission status: Accepted

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

p.sort()
minp = p[N - 1] - p[0]
for i in range(N - K + 1):
    minp = min(minp, p[i + K - 1] - p[i])

print(minp)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from statistics import mean, median,variance,stdev
import sys
import math

x = input().split()
#y = input().split()


#y = int(input())
#x = int(input())
a = []
b = int(x[0])
num = int(x[1])
for i in range(b):
    a.append(int(input()))
a = sorted(a)
min = a[num-1]-a[0]
for i in range(num,b):
    if min > a[i]-a[i-num+1]:
        min = a[i]-a[i-num+1]
if min > a[b-1]-a[b-num]:
    min = a[b-1]-a[b-num]
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=10000000000
for i in range(n-k+1):
    ans=min([h[i+k-1]-h[i],ans])
print(ans)

Submission status: Accepted

Solution code:
line=[]
d=0

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

for j in range(n):
  p=int(input())
  line.append(p)
  
#print(line)
line.sort()
#print(line)
#print(line[1]-line[0])  
min=line[k]-line[0]
for mm in range(0,n-k+1):
  d=(line[mm+k-1]-line[mm])
  if min>d:
    min=d
    
print(min)

Submission status: Accepted

Solution code:
N,K = [int(x) for x in input().split()]
h = [int(input()) for _ in range(N)]
i=0
hdif = 1000000000000
h.sort()
while(i+K-1<N):
    hdif=min(h[i+K-1]-h[i],hdif)
    i+=1
print(hdif)

Submission status: Accepted

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

Submission status: Accepted

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

h.sort(reverse=True)

ans=None
for i in range(N-(K-1)):
  diff=h[i]-h[i+(K-1)]
  if ans is None or diff<ans:
    ans=diff
    
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
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 = []
for _ in range(N):
    H.append(int(input()))
H.sort()
D = []
# print(H)
for i in range(N-K+1):
    # print(i, i+K-1)
    D.append(H[i+K-1] - H[i])
# print(D)
print(min(D))

Submission status: Accepted

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

ans = float('inf')
for i in range(n-k+1):
    if ans > hl[i+k-1] - hl[i]:
        ans = hl[i+k-1] - hl[i]
print(ans)

Submission status: Accepted

Solution code:
n, k = (int(i) for i in input().split())
hl = [int(input()) for _ in range(n)]
hl.sort()
print(min([hl[i+k-1] - hl[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=100000000000000
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 = (int(i) for i in input().split())
h = [int(input()) for i in range(n)]
h2 = sorted(h)
ans = 999999999999999
for i in range(n-k+1):
  ans =min(ans,abs(h2[i]-h2[i+k-1]))
print(ans)

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
L = sorted([int(input()) for i in range(N)])
ans = float("inf")
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=[int(input()) for i in range(N)]

h=sorted(h)

min=1000000000000
for i in range(N-K+1):
    t=h[i+K-1]-h[i]
    if(t<min):
        min=t

print(min)

Submission status: Accepted

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

Submission status: Accepted

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

H = [int(input()) for n in range(N)]

H.sort()

R = [H[i+K-1] - H[i] for i in range(N+1-K)]

print(min(R))

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
lst = []
for _ in range(N):
    lst.append(int(input()))
lst = sorted(lst)
ret = 100000000000
 
for idx, x in enumerate(lst[:-K+1], 0):
    tmp = lst[idx + K-1] - x
    if tmp < ret:
        ret = tmp
print(ret)

Submission status: Accepted

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

R = []

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

R = sorted(R)
minv = 2000000000

for j in range(n-k+1):
    minv = min(minv,R[j+k-1]-R[j])

print(minv)

Submission status: Accepted

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

h.sort()


min = 10 ** 20

for i in range(N):
  if i+K > N:
    break
  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())

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

x.sort()

y=[]

for j in range(n-k+1):
  p=x[j+k-1]-x[j]
  y.append(p)

y.sort()

print(y[0])

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

min = 10 ** 20

for i in range(N):
  if i+K > N:
    break
  diff = h[i] - h[i+K-1]
  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)]
h.sort()
d = []
for i in range(n-k+1):
  d.append(h[i+k-1]-h[i])
d.sort()
print(d[0])

Submission status: Accepted

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

h = sorted(h)


min = 10 ** 9 + 1
for i in range(n - k + 1):
  h_min = h[i]
  h_max = h[i + k - 1]
  h_diff = h_max - h_min
  if h_diff < min:
    min = h_diff

print(min)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, K = map(int, input().split(' '))
list = [int(input()) for _ in range(N)]
list.sort()
gap = []
for i in range(N - K + 1):
    gap.append(list[i + K - 1] - list[i])

ans = min(gap)
print(ans)