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(' '))
trees = sorted([ int(input()) for _ in range(n) ])

ans = None
for i in range(n-k+1):
  if ans is None:
    ans = trees[i+k-1] - trees[i]
  else:
    ans = min(ans, trees[i+k-1] - trees[i])
    
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from __future__ import print_function
import numpy as np
import sys
import math,string,itertools,fractions,heapq,collections,re,array,bisect,copy,functools
# from itertools import accumulate
# from collections import deque
import random

def eprint(*args, **kwargs):
    print(*args, file=sys.stderr, **kwargs)

n,k = map(int, input().split())
h = np.zeros(n, dtype = int)
for i in range(n):
    h[i] = int(input())

h.sort()
# eprint(h)
ll = np.zeros(n-k+1,dtype=int)
for i in range(n-(k-1)):
    ll[i] = abs(h[i] - h[i+(k-1)])
    
# eprint(ll)
print(ll.min())
    
    
# sorted(h)
# np.sort(h)
# h.sort()
# print(h)

Submission status: Accepted

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

h = sorted(H)
#print(h)

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

print(min(d))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Tue May 14 20:13:03 2019

@author: avina
"""

n,k = map(int, input().split())
l = []
for i in range(n):
    l.append(int(input()))
l.sort()
i = 0
mins = 1e10
while i < n - k +1:
  mins = min(mins, l[i+k-1] - l[i])
  i+=1
print(mins)

Submission status: Accepted

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

def main():
    h = H
    h.sort()
    length = len(h)
    ans = float('inf')
    for i in range(length):
        end_index = i + K - 1
        if end_index > length - 1:
            break
        ans = min(ans, h[end_index] - h[i])

    print(ans)

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

print(ans)

Submission status: Accepted

Solution code:
N, K = map(int,input().split())
H = []
H_dif = []

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

H.sort()

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

print(min(H_dif))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

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

sorted_tree = sorted(h)
sorted_tree2 = sorted_tree[K-1:]

min_diff = 1e9
for (min_h, max_h) in zip(sorted_tree, sorted_tree2):
    if (max_h - min_h) < min_diff:
        min_diff = max_h - min_h

print(min_diff)

Submission status: Accepted

Solution code:
N, K = map(int,input().split())
h = []
for i in range(N):
    h.append(int(input()))
h.sort()
ans = 10e9
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(i) for i in input().split()]
hs = [int(input()) for _ in range(N)]
hs.sort()
diffs = []
for lh in range(N):
    rh = lh + K - 1
    if rh >= N:
        break
    diffs.append(hs[rh] - hs[lh])
print(min(diffs))

Submission status: Accepted

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

Submission status: Accepted

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

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)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
n,k=map(int,input().split())
l=[]
for i in range(n):
    t=int(input())
    l.append(t)
l.sort()
#print(l)
ans=100000000000
for i in range(len(l)-k+1):
    if(ans>l[i+k-1]-l[i]):
        ans=l[i+k-1]-l[i]
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

# h = [0]*n
# for i in range(n):
#     h[i] = int(input())

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

h.sort()

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

print(ans)

Submission status: Accepted

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

n, k = map(int, input().split())
h = [0]*n

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

h.sort()

ans = 10**9
for i in range(n-k+1):
    temp = h[i+k-1] - h[i]
    if ans > temp:
        ans = temp
print(ans)

Submission status: Accepted

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

n, k = map(int, input().split())
h = [0]*n

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

h.sort()

ans = 10**9
for i in range(n-k+1):
    temp = h[i+k-1] - h[i]
    if ans > temp:
        ans = temp
print(ans)

Submission status: Accepted

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

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 = list(sorted([int(input()) for _ in range(N)]))
ans = H[-1]
for s, e in zip(H, H[K - 1:]):
  ans = min(e - s, ans)

print(ans)

Submission status: Accepted

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

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

h_list.sort()

for j in range(n-k+1):
  if j == 0:
    min_df = h_list[k-1] - h_list[0]
  else:
    df = h_list[j+k-1] - h_list[j]
    if df < min_df:
      min_df = df

print(min_df)

Submission status: Accepted

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

H.sort(reverse=True)

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

print(min_score)

Submission status: Accepted

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

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

print(min_diff)

Submission status: Accepted

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

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

H.sort(reverse=False)

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

print(min_h)

Submission status: Accepted

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

Submission status: Accepted

Solution code:

N,K = map(int, input().split())
h_list = [int(input()) for _ in range(N)]
h_list.sort()
diff_list = []
for i in range(N-K+1):
    diff_list.append(h_list[i+K-1] - h_list[i])

print(min(diff_list))

Submission status: Accepted

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

min_diff = 1000000000
for i in range(N - K + 1):
    diff = h[i + K - 1] - h[i]
    if min_diff > diff:
        min_diff = diff
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)
ans = 1000000000
for i in range(N-K+1):
    res = H[i+K-1] - H[i]
    ans = min(res,ans)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

print(min(ans))

Submission status: Accepted

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

s_h = sorted(h,reverse=True)

i = 0
minD = 1000000000
while i+K <= N:
    diff = s_h[i] - s_h[i+K-1]
    minD = min(diff,minD)

    i = i + 1

print(minD)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
p=[int(input()) for i in range(n)]
p.sort()
q=9999999999999
for i in range (n-k+1):
    if p[k+i-1]-p[i]<q:
        q=p[k+i-1]-p[i]
print(q)

Submission status: Accepted

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

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

print(min(diff))

Submission status: Accepted

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

s = 1000000000
for i in range( N-K+1 ):
  s = min(s,li[i+K-1]-li[i])
print (s)

Submission status: Accepted

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

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

print(min(tmp))

Submission status: Accepted

Solution code:
N, K = [int(a) for a in input().split(" ")]
hs = sorted([int(input()) for i in range(N)])
ans = 10**10
for left, right in zip(range(0,N-K+2), range(K-1,N)):
    ans = min(ans, hs[right]-hs[left])
print(ans)

Submission status: Accepted

Solution code:
N,K = list(map(int,input().split()))
H = [int(input()) for i in range(N)]
H.sort()
result = 10000000000
for i,h in enumerate(H):
    if K + i - 1 > N -1:
        break
    if result > H[K + i - 1] - H[i]:
        result = H[K + i - 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()
diff = [0]*(N-K+1)
for i in range(N-K+1):
  diff[i] = h[i+K-1] - h[i]

print(min(diff))

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

ans = float('inf')

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

print(m)

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= list(map(int,input().split(" ")))
nums = []
for i in range(N):
    nums.append(int(input()))
nums.sort()
r = 0
l = 0
ans = []
for i,num in enumerate(nums[:-(K-1)]):
    r = nums[i+K-1]
    l = num
    # print("l:",l,"//r:",r)
    ans.append(r-l)
print(min(ans))

Submission status: Accepted

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

trees = []
for _ in range(n):
  trees.append(int(input()))
  
  
trees.sort()
ans = 10**9+1
for i in range(n-k+1):
  if ans > trees[i+k-1] - trees[i]:
    ans = trees[i+k-1] - trees[i]
    
print(ans)

Submission status: Accepted

Solution code:
import numpy as np
from copy import deepcopy

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

H = sorted(H)

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

Submission status: Accepted

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

h.sort()
ans = float('inf')
for i in range(N - K + 1):
    ans = min(ans, h[i + K - 1] - h[i])
print(ans)

Submission status: Accepted

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

H.sort()
ans = float('inf')
for i in range(N-K+1):
  diff = abs(H[i] - H[i+K-1])
  if diff < ans:
    ans = diff
print(ans)

Submission status: Accepted

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

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

print(min(m))

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_list=[]
for i in range(n):
    h_list.append(int(input()))
h_list.sort()
i=0
Min=10**9
while i+k-1<=n-1:
    d=h_list[i+k-1]-h_list[i]
    if d<Min:
        Min=d
    i=i+1
print(Min)

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

Submission status: Accepted

Solution code:
# coding: utf-8
N,k = map(int,input().rstrip().split())
h = sorted([ int(input()) for _ in range(N) ])
list = []
for i in range(N-k+1):
    list.append(h[i+k-1] - h[i])
print(min(list))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
from math import *
from itertools import *
from collections import *
from functools import *
try:
    from math import gcd
except Exception:
    from fractions import gcd


def solve(N: int, K: int, h: "List[int]"):
    h.sort()
    ret = float('inf')
    for l, r in zip(h, h[K-1:]):
        ret = min(ret, r - l)
    return ret


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    h = [ int(next(tokens)) for _ in range(N) ]  # type: "List[int]"
    result = solve(N, K, h)
    print(result)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import heapq
import queue

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

trees = []
heapq.heapify(trees)

for _ in range(N):
    heapq.heappush(trees, int(input()))

ans = 1 + 10**9

q = queue.Queue(maxsize=K-1)

for _ in range(K-1):
    q.put(heapq.heappop(trees))

for _ in range(N-K+1):
    curmin = q.get()
    nextmin = heapq.heappop(trees)

    ans = min(ans, nextmin-curmin)

    q.put(nextmin)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys

r=input().split()
n=int(r[0])
k=int(r[1])
h=sorted([int(x.strip()) for x in sys.stdin.readlines()])

d=[h[x+k-1]-h[x] for x in range(n-k+1)]
ans=min(d)
            
print(ans)

Submission status: Accepted

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

h.sort()

min_d = 1000000000
for i in range(n-k+1):
    d = h[i+k-1] - h[i]
    if d < min_d:
        min_d = d

print(min_d)

Submission status: Accepted

Solution code:
#115 C

N,K= map(int,input().split())
trees =[]

for _ in range(N):
    trees.append(int(input()))
trees.sort()
ans=10**10
for i in range(N-K+1):
    min_=trees[i]
    max_=trees[i+K-1]
    ans=min(ans,max_-min_)
print(ans)

Submission status: Accepted

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

h.sort()
ans = 10 ** 9

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

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

h = []
for _ 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 = []
for i in range(n):
    h.append(int(input()))

h = sorted(h)

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

print(ans)

Submission status: Accepted

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

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

h.sort()

# h_sa = [0]*N

# for i in range(1,N):
#     h_sa[i] = h[i] - h[i-1]

# print(h)

# h_sa.sort()
answer = 10**9
for i in range(N):
    if i + K -1 >= N:
        break
    # if answer > sum(h_sa[i:i+K]):
    #     answer = sum(h_sa[i:i+K])
    # print(h[i+K-1]-h[i])

    if answer > h[i+K-1]-h[i]:
        answer = h[i+K-1]-h[i]





print(answer)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
H = [None] * N

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

H = sorted(H)
ans = 10e9

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

p.sort()

ans = []
for i in range(K - 1, len(p)):
    ans.append(p[i] - p[i - K + 1])

print(min(ans))

Submission status: Accepted

Solution code:
import sys
def main():
  if 'get_ipython' not in globals():  
    # For Subsession
    N,K = map(int, input().split())
    h = []
    for _ in range(N):
      h.append(int(input()))
  else:
    N = 5
    K = 3
    h = [10,15,11,14,12]
    # N = 5
    # K = 3
    # h = [5,7,5,7,7]

  h.sort()  

  ans = (h[K-1] - h[0])
  
  max_i = len(h)-K+1
  for i in range(1,max_i):
    tmp = (h[i+K-1] - h[i])
    if ans > tmp:
      ans = tmp
    if ans == 0:
      print(ans)
      sys.exit()
  
  print(ans)

main()

Submission status: Accepted

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

Submission status: Accepted

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

l = 0
r = K - 1
diff = []
while r < N:
    diff.append(h[r]-h[l])
    l = l + 1
    r = r + 1

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 _ 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 _ in range(N)]
H.sort()
tmp=H[K-1]-H[0]
for i in range(0,N-K+1):
    if tmp>H[K+i-1]-H[i]:
        tmp=H[K+i-1]-H[i]
print(tmp)

Submission status: Accepted

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

Submission status: Accepted

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

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

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

vec.sort()

minV = 100000

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

print(min(l))

Submission status: Accepted

Solution code:
# 2019/04/23
# AtCoder Beginner Contest 115 - C

# Input
n, k = map(int, input().split())
h = list()

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

h.sort()

minh = h[n-1]
for i in range(n - k + 1):
    rng = h[i+k-1] - h[i]
    if rng < minh:
        minh = rng

print(minh)

Submission status: Accepted

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

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

Submission status: Accepted

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

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

h.sort()

l = []

for i in range(0,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 = [int(input()) for i in range(n)]
h.sort()
res = 10**10
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())
tree_list,new_tree_list = [],[]
for i in range(N):
    tree_list.append(int(input()))

tree_list.sort(reverse=True)

for i in range(len(tree_list)-K+1):
    new_tree_list.append(tree_list[0+i]-tree_list[K+i-1])
new_tree_list.sort()
print(new_tree_list[0])

Submission status: Accepted

Solution code:
import numpy as np

N,K = list(map(int, input().split()))
h = []
for _ in range(N):
    h.append(int(input()))
h = sorted(h)
res = h[N-1] - h[0]
for i in np.arange(0,N-K+1,1):
    tmp = h[i+K-1] - h[i]
    if res > tmp:
        res = tmp
    
print(res)

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

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
L=[]
for i in range(N) :
  L.append(int(input()))
L.sort()
M=[]
for x,y in zip(L,L[K-1:]) :
  M.append(y-x)
print(min(M))

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
L=[]
for i in range(N) :
  L.append(int(input()))
L.sort()
S=10**9
for i in range(N-K+1) :
  if (L[i+K-1]-L[i]) <= S :
    S=(L[i+K-1]-L[i])
print(S)

Submission status: Accepted

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

hs_sorted = sorted(hs)

min_diff = float("inf")
#print(hs_sorted)
for i in range(N-K+1):
    diff = hs_sorted[i+K-1] - hs_sorted[i]
    #print(diff)
    if diff < min_diff:
        min_diff = diff

print(min_diff)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

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

h_list.sort()
best = 1000000000
for i in range(N - K + 1):
    #diff = h_list[i:i+K][-1] - h_list[i:i+K][0]
    diff = h_list[i+K-1] - h_list[i]
    if diff < best:
        best = diff

print (best)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

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

h_list.sort()
'''
best = 1000000000
for i in range(N - K + 1):
    diff = h_list[i:i+K][-1] - h_list[i:i+K][0]
    if diff < best:
        best = diff
'''
print (min(h_list[i+K-1] - h_list[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()
m = float('inf')
for i in range(N-K+1):
  if h[i+K-1] - h[i] < m:
    m = h[i+K-1] - h[i]
print(m)

Submission status: Accepted

Solution code:
import math
n,k = map(int,input().split())
hList = [int(input()) for a in range(n)]
hList.sort()
mn = 10**10
for a in range(n - k + 1):
    mn=min([mn,hList[a+k-1]-hList[a]])
print(mn)

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()
l = 0
r = K-1
ans = 10**10
while r <= N-1:
    sub = H[r] - H[l]
    ans = min(ans, sub)
    r += 1
    l += 1

print(ans)

Submission status: Accepted

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

inf = 10**9
min_height = inf
for i in range(N-K+1):
    # print(i+(K-1), h[i+(K-1)], i, h[i])
    min_height = min(min_height, h[i+(K-1)] - h[i])

print(min_height)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
a, b = [int(i) for i in input().split()]
c = []
for i in range(a):
    c.append(int(input()))

c.sort()

temp = 999999999999999999999999999999999999999999999999999999999999999999999999999999999
for a in range(len(c) - b + 1):
    s = c[a + b - 1]
    t = c[a]
    if s - t < temp:
        temp = s - t

print(temp)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

n, k = map(int, input().split())
h = [int(input()) for _ in range(n)]
h = 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:
nk = input().split()
n = int(nk[0])
k = int(nk[1])
hlist = []
for i in range(n):
    hlist.append(int(input()))

hlist.sort()

submin = 999999999999999999999999999999999
for i in range(0, n - k + 1):
    submin = min(submin, hlist[i+k-1] - hlist[i])

print(submin)

Submission status: Accepted

Solution code:
ans = 10**9

N,K = map(int, input().split())
H = []
for i in range(N):
    x = int(input())
    H.append(x)
H = sorted(H)
for i in range(N-(K-1)):
    y = H[i+(K-1)]-H[i]
    ans = min(y,ans)
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()
hight = []
for i, j in enumerate(h):
    if i >= len(h) - K + 1:
        break
    hight.append(h[i + K - 1] - h[i])
hight.sort()
print(hight[0])

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import heapq

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

arr=[0]*N
for z in range(N):
  heapq.heapreplace(arr,int(input()))

arr2=[0]*K
for z in range(K):
  arr2[z]=heapq.heappop(arr)

r=arr2[-1]-arr2[0]

z=0
try:
  while True:
    arr2[z]=heapq.heappop(arr)
    if z==K-1:
      zz=0
    else:
      zz=z+1

    rb=arr2[z]-arr2[zz]
    if rb<r:
      r=rb

    z+=1
    if z==K:
      z=0

except Exception:
  pass

print(r)

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    n, k = map(int, input().split())
    trees = [0] * n
    for i in range(n):
        trees[i] = int(input())
    trees.sort()

    ans = 10 ** 9
    for i in range(n - k + 1):
        diff = trees[i + k - 1] - trees[i]
        if diff < ans:
            ans = diff
    print(ans)

Submission status: Accepted

Solution code:

def main():
    a, b = map(int, input().split())
    data = [int(input()) for i in range(a)]
    data.sort()
    ans = data[-1] - data[0]

    for i in range(a - b + 1):
        now_data = data[i + b - 1] - data[i]
        if now_data < ans:
            ans = now_data

    print(ans)



if __name__ == '__main__':
    main()