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

x = sorted(h)
ans = min(b - a for a, b in zip(x, x[(K - 1):]))

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

h = []
diff = 10000000000

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

h = sorted(h)

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

print(diff)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import itertools
min_all = 1000000000000000000000000
    
N,K = [int(i) for i in input().split(" ")]
H = [int(input()) for _ in range(N)]
H.sort()
    
for i in range(N+1-K):
  min_now = H[K-1+i] - H[i]
  if min_all > min_now:
    min_all = min_now
print(min_all)

Submission status: Accepted

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

answer = 10 ** 10

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

print(answer)

Submission status: Accepted

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

Submission status: Accepted

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

n3 = []

for i1 in range(n1):
    n3.append(int(input()))

n3.sort(reverse=True)

m1 = 1000000000
t1 = 0
for i2 in range(n1 - n2 + 1):
    t1 = n3[i2] - n3[i2 + n2 - 1]
    if t1 < m1:
        m1 = t1
print(m1)

Submission status: Accepted

Solution code:


def main():

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

    hList.sort()
    it = iter(hList)
    diffList = []
    bady = K-1


    for i in range(N-K+1):

        diff = hList[i]-hList[i+bady]
        diffList.append(abs(diff))
    

    print(min(diffList))







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

Submission status: Accepted

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

h = sorted(h)
ans = 10 ** 9 + 1
for i in range(len(h) - (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 = [int(input()) for _ in range(N)] 

heights.sort()

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

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 = []

m_dfr = 9999999999999999

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

H = sorted(H, reverse = True)
# print(H)

for i in range(0, N - K + 1):
	# print(H[i], H[i + K - 1])
	if m_dfr > H[i] - H[i + K - 1]:
		m_dfr = H[i] - H[i + K - 1]

print(m_dfr)

Submission status: Accepted

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

ans = h[-1]

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

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

ans = min(ans_list)
print(ans)

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
lst = [int(input()) for i in range(n)]
lst.sort()
for i in range(n-k+1):
    if i==0:
        m = lst[k-1]-lst[i]
    else:
        if lst[k+i-1]-lst[i] < m:
            m = lst[i+k-1]-lst[i]
print(m)

Submission status: Accepted

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

def main():
	n, k = map(int, input().strip().split())
	h = [0] * n
	for i in range(n):
		h[i] = int(input().strip())
	H = sorted(h)
	ans = 10**9
	for i in range(n-k+1):
		ans = min(ans, H[i+k-1]-H[i])
	print(ans)


if __name__ == '__main__':
	main()

Submission status: Accepted

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

H = sorted(int(input().strip()) for i in range(n))
min_diff = 10e9 + 5

cumsum = []
for h in H:
  cumsum.append(h)
  if len(cumsum) >= k:
    min_diff = min(min_diff, cumsum[-1] - cumsum[-k])

print(min_diff)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,k=map(int,input().split());l=sorted([int(input())for _ in[0]*n])
print(min(j-i for i,j in zip(l,l[k-1:])))

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
l=[int(input())for _ in[0]*n]
l.sort()
print(min(j-i for i,j in zip(l,l[k-1:])))

Submission status: Accepted

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

Hs.sort()

ans = 10 ** 9

for i in range(N - K + 1):
    # print((Hs[K - 1 + i], Hs[i]))
    delta = Hs[K - 1 + i] - Hs[i]
    if delta < ans:
        ans = delta

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from copy import deepcopy
from sys import exit,setrecursionlimit
import math
from collections import defaultdict,Counter,deque
from fractions import Fraction as frac

setrecursionlimit(1000000)


def main():
	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))

def zip(a):
	mae = a[0]
	ziparray = [mae]
	for i in range(1,len(a)):
		if(mae != a[i]):
			ziparray.append(a[i])
			mae = a[i]
	return ziparray

def is_prime(n):
	if n < 2: return False

	for k in range(2, int(math.sqrt(n)) + 1):
		if n % k == 0:
			return False

	return True

def list_replace(n,f,t):
	return [t if i==f else i for i in n]

def base_10_to_n(X, n):
	X_dumy = X
	out = ''
	while X_dumy>0:
		out = str(X_dumy%n)+out
		X_dumy = int(X_dumy/n)
	if(out == ''): return '0'
	return out

def gcd(l):
	x = l.pop()
	y = l.pop()
	while(x%y!=0):
		z = x%y
		x = y
		y = z
	l.append(min(x,y))
	return gcd(l) if len(l) > 1 else l[0]

class Queue():
	def __init__(self):
		self.q = deque([])
	def push(self,i):
		self.q.append(i)
	def pop(self):
		return self.q.popleft()
	def size(self):
		return len(self.q)
	def debug(self):
		return self.q

class Stack():
	def __init__(self):
		self.q = []
	def push(self,i):
		self.q.append(i)
	def pop(self):
		return self.q.pop()
	def size(self):
		return len(self.q)
	def debug(self):
		return self.q

class graph():
	def __init__(self):
		self.graph = defaultdict(list)
	def addnode(self,l):
		f,t = l[0],l[1]
		self.graph[f].append(t)
		self.graph[t].append(f)
	def rmnode(self,l):
		f,t = l[0],l[1]
		self.graph[f].remove(t)
		self.graph[t].remove(f)
	def linked(self,f):
		return self.graph[f]

class dgraph():
	def __init__(self):
		self.graph = defaultdict(set)
	def addnode(self,l):
		f,t = l[0],l[1]
		self.graph[f].append(t)
	def rmnode(self,l):
		f,t = l[0],l[1]
		self.graph[f].remove(t)
	def linked(self,f):
		return self.graph[f]

main()

Submission status: Accepted

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

Submission status: Accepted

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

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

h.sort()

ans = 10**100

k -= 1

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(4100000)


def inputs(num_of_input):
    ins = [input() for i in range(num_of_input)]
    return ins


def solve(N, K, inputs):
    trees = list(map(lambda x: int(x), inputs))
    trees.sort()

    min_diff = -1
    for i in range(K-1, N):
        diff = trees[i] - trees[i - K + 1]
        if min_diff == -1 or diff < min_diff:
            min_diff = diff
    return min_diff


def string_to_int(string):
    return list(map(lambda x: int(x), string.split()))


if __name__ == "__main__":
    [N, K] = string_to_int(input())
    ret = solve(N, K, inputs(N))
    print(ret)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
import itertools
sys.setrecursionlimit(10**5)
stdin = sys.stdin
bisect_left = bisect.bisect_left
bisect_right = bisect.bisect_right
def LI(): return list(map(int, stdin.readline().split()))
def LF(): return list(map(float, stdin.readline().split()))
def LI_(): return list(map(lambda x: int(x)-1, stdin.readline().split()))
def II(): return int(stdin.readline())
def IF(): return float(stdin.readline())
def LS(): return list(map(list, stdin.readline().split()))
def S(): return list(stdin.readline().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')

#A
def A():
    d = II()
    print("Christmas",end="")
    for _ in range(25 - d):
        print(" Eve", end="")
    print()
    return

#B
def B():
    n = II()
    p = IR(n)
    p.sort()
    print(sum(p) - p[-1] // 2)
    return

#C
def C():
    n, k = LI()
    h = IR(n)
    h.sort()
    ans = inf
    for i in range(n - k + 1):
        ans = min(ans, h[i + k - 1] - h[i])
    print(ans)
    return

#D
def D():
    return

#Solve
if __name__ == '__main__':
    C()

Submission status: Accepted

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

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

print(res)

Submission status: Accepted

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


def solve(N: int, K: int, h: "List[int]"):
    h = sorted(h)
    mn = 1e9
    for i in range(N-K+1):
        if mn > h[i+K-1] - h[i]:
            mn = h[i+K-1] - h[i]
    print(mn)
    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    h = [ int(next(tokens)) for _ in range(N) ]  # type: "List[int]"
    solve(N, K, h)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

mind = 10 ** 9 + 1

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

print(mind)

Submission status: Accepted

Solution code:
# ABC 115 C

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

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

h.sort()

ans = 1.0e+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())
tree_dict = {}
tree_list = []
for i in range(N):
    a = int(input())
    tree_list.append(a)

tree_list = sorted(tree_list)
min_count = 10 ** 9
max_num = 0
min_num = 0


for i in range(len(tree_list) - K + 1):
    max_num = tree_list[i + K - 1]
    min_num = tree_list[i]
    if max_num - min_num < min_count:
        min_count = (max_num - min_num)

print(min_count)

Submission status: Accepted

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

trees.sort()

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

print(m)

Submission status: Accepted

Solution code:
import sys

N, K, *h = map(int, sys.stdin.read().split())

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

Submission status: Accepted

Solution code:
import itertools

def q49(n, k, h):
    h.sort()
    a = [h[i+k-1]-h[i] for i in range(n-k+1)]
    return min(a)
            
N, K = [int(i) for i in input().split()]
h = []
for _ in range(N):
    h.append(int(input()))
print(q49(N, K, h))

Submission status: Accepted

Solution code:
def solve():
    N,K = [int(s) for s in input().split() if s]
    h = [int(input()) for _ in range(N)]
    h = sorted(h)
    result = 10**9+1
    # print(h)
    for i in range(N-K+1):
        diff = h[i+K-1]-h[i]
        # print(h[i+K-1],h[i],diff)
        if result > diff:
            result = diff
    print(result)
    
solve()

Submission status: Accepted

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

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

Submission status: Accepted

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

ans = 3e9
for k in range(N - K + 1):
    left = H[k]
    right = H[k + K - 1]
    ans = min(ans, right - left)
print(ans)

Submission status: Accepted

Solution code:
import math
from functools import reduce
from collections import deque
import sys
sys.setrecursionlimit(10**7)

def s(generator, splitter, mapper):
    return [ mapper(s) for s in generator().split(splitter) ]

# スペース区切りの入力を読み込んで数値リストにして返します。
def get_nums_l():
    return [ int(s) for s in input().split(" ")]

# 改行区切りの入力をn行読み込んで数値リストにして返します。
def get_nums_n(n):
    return [ int(input()) for _ in range(n)]

n,k = get_nums_l()
hs = sorted(map(int, sys.stdin.readlines()))

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

print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
#ABC115C
import sys

tmp = input().split()
hoge = list(map(lambda a: int(a), tmp))
n = hoge[0]
k = hoge[1]

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

h.sort()
#print(h)
#print(n)
#print(k)

res=h[k-1]-h[0]
#print(res)

for i in range(1,n-k+1):
	res = min(res,h[k+i-1]-h[i])
	#print("i:{} res:{}".format(i,res))
print(res)

Submission status: Accepted

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

Submission status: Accepted

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

height = []
for i in range(N):
  height.append(int(input()))
                
height.sort()
dif = [0]*(N-1)
for i in range(N-1):
  dif[i] = height[i+1]-height[i]

diflist = [dif[0]]*(N-1)
for i in range(1,N-1):
  diflist[i] = diflist[i-1] + dif[i]

diflist.insert(0,0)
ans = 10**10
for i in range(N-K+1):
  pro = diflist[i+K-1]-diflist[i]
  ans = min(ans,pro)
  
print(ans)

Submission status: Accepted

Solution code:
# 2019/07/25

input=open(0).readline

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

h.sort()
# print(h)
tmp = 10 ** 10

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

print (ans)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
ls = sorted([int(input()) for _ in range(n)])
ans = float('inf')

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

Submission status: Accepted

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

df=[]

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

Submission status: Accepted

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

h_sorted = sorted(h,reverse=True)
min_diff = 10**10

for i in range(len(h)-k+1):
  diff = h_sorted[i] - h_sorted[i+k-1]
  if min_diff > diff:
    min_diff = diff  

print(min_diff)

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

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n , k = list(map(int, input().split()))
h = []
for i in range(n):
    h.append(int(input()))
h.sort()
min = h[-1]
for i in range(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:
#Coding:Utf-8

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

a.sort()

min=a[N-1]-a[0]
for l in range(N-K+1):
    if min>a[l+K-1]-a[l]:
        min=a[l+K-1]-a[l]

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

Submission status: Accepted

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

Submission status: Accepted

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

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

H = sorted(H)
min_diff = float('inf')
for i in range(0, 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.sort()

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

Submission status: Accepted

Solution code:
def smaller(x, y):
	if x < y:
		return x
	return y

N, K = map(int, input().split())
H = []
for i in range(0, N):
	H.append(int(input()))
H.sort()
ans = 5000000000000000
for i in range(0, N-K+1):
	ans = smaller(ans, H[i+K-1]-H[i])
print(ans)

Submission status: Accepted

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

diff = heights[K-1] - heights[0]
ans = diff

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

Submission status: Accepted

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

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

    print(ans)


main()

Submission status: Accepted

Solution code:
n, k = map(int,input().split())
li = list(int(input()) for i in range(n))

li.sort()
num = 0
li2 = []
for i in range(len(li)-k+1):
    li2.append(li[i+k-1] - li[i])

print(min(li2))

Submission status: Accepted

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

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

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

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
n,k = map(int,input().split(' '))
v =[]
for i in range(0,n):
    x = int(input())
    v.append(x)
v.sort()
ans = 20000000000000
for i in range(0,n):
    if i >= k - 1:
        ans = min(ans,abs(v[i] - v[i-k+1]))
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
#ABC115 C
n,k = map(int,input().split())
h = [int(input()) for _ in range(n)]
h.sort()
s = [h[i+k-1] - h[i] for i in range(n-k+1)]
s.sort()
print(s[0])

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys 
import numpy as np

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

h = np.zeros(N)

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

h = np.sort(h)

m = h[N-1] - h[0]

for n in range(0,N-K+1):
    d = h[n+K-1] - h[n]
    if d < m:
        m = d

print(int(m))

Submission status: Accepted

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

N, K = map(int, input().split())
arr = [int(input()) for i in range(N)]
arr.sort()
num = 0
min_num = pow(10,9)

for i in range(N - K + 1):
    num = arr[i + K - 1] - arr[i]
    if min_num > num:
        min_num = num
print(min_num)

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()
min = h_list[-1]
for j in range(n-k+1):
    if min > h_list[j+k-1] - h_list[j]:
        min = h_list[j+k-1] - h_list[j]

print(min)

Submission status: Accepted

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

Submission status: Accepted

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

h = sorted(h)

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

print(ans)

Submission status: Accepted

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

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

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(0,n-k+1):
    h_max = h[i+k-1]
    h_min = h[i]
    c = h_max - h_min
    if c < ans:
        ans = c
print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
'''
allinputs = iter(input().splitlines())
input = lambda : next(allinputs)
#'''
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):
	cost = h[i + K - 1] - h[i]
	if cost < ans:
		ans = cost
		
print(ans)

Submission status: Accepted

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

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

    mn = 10**10

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

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
tree = [int(input()) for _ in range(n)]
tree.sort()
result = float('INF')
cumulative = [0]
for i in range(n - 1):
    cumulative.append(tree[i + 1] - tree[i] + cumulative[i])
for i in range(n - k + 1):
    result = min(result, cumulative[i + k - 1] - cumulative[i])
print(result)

Submission status: Accepted

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

hlist=[]
for i in range(N):
  hi=int(input())
  hlist.append(hi)
  
hlist.sort()
#print(hlist)

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

Submission status: Accepted

Solution code:
n, k = map(int, raw_input().split())
H = [int(raw_input()) for i in range(n)]
H.sort()
min_diff = 10**9 + 1
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())

# 入力をすべてソートしたあと、後の操作のために末尾にm個の要素を付け加える
m = k - 1
height = sorted([int(input()) for i in range(n)]) + [0] * m   # 0であれば他の操作に影響しない
ans = min([height[i+m] - height[i] for i in range(n)][0:-m])  # スライスで末尾２つを省略

# print(height)  # 確認用
print(ans)

Submission status: Accepted

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

h = sorted(h)

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

print(ans)

Submission status: Accepted

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

mn = 10**9
for i in range(n-k+1):
  mn = min(a[i+k-1]-a[i], mn)
print(mn)

Submission status: Accepted

Solution code:
a, b = map(int, input().split())
c = [int(input()) for _ in range(a)]
c.sort()
d = -1
for i in range(b-1, a):
    if d == -1:
        d = abs(c[i] - c[i-b+1])
    else:
        if abs(c[i] - c[i-b+1]) < d:
            d = abs(c[i] - c[i-b+1])
print(d)

Submission status: Accepted

Solution code:
from collections import deque

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

hq = deque() 
dif = float("inf")
for i in range(n) :
    hmax = hl[i]
    hq.append(hmax)
    if len(hq) == k :
        hmin = hq.popleft()
        dif = min(dif, hmax - hmin)

print(dif)

Submission status: Accepted

Solution code:
n,k,*h=map(int,open(0).read().split())
h.sort()
print(min(y-x for x,y in zip(h,h[k-1:])))

Submission status: Accepted

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

Submission status: Accepted

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

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

def h_find(list,n):
    k=list[n-1]-list[0]
    for i in range(1,len(list)-n+1):
        k2=list[i+n-1]-list[i]
        if k2<k:
            k=k2
        if k==0:
            break
    return k

print(h_find(h,K))

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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