Problem:


Score : 300 points

Problem Statement

In some other world, today is Christmas Eve.

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

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

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

Constraints

2 \leq K < N \leq 10^5

1 \leq h_i \leq 10^9

h_i is an integer.

Input

Input is given from Standard Input in the following format:

N K
h_1
h_2
:
h_N

Output

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

Sample Input 1

5 3
10
15
11
14
12

Sample Output 1

2

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

Sample Input 2

5 3
5
7
5
7
7

Sample Output 2

0

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

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



Sample input:
5 3
10
15
11
14
12


Sample output:
2

Submission status: Accepted

Solution code:
def main():
    N, K = map(int, input().split())
    H = [int(input()) for i in range(N)]
    H.sort()
    mi = 10**9
    for i in range(N - K + 1):
        diff = H[i+K-1] - H[i]
        if diff < mi:
            mi = diff
    print(mi)
if __name__ == "__main__":
    main()

Submission status: Accepted

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

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

Submission status: Accepted

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

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):
  d=h[i+k-1]-h[i]
  if d<ans:
    ans=d
print(ans)

Submission status: Accepted

Solution code:


def solve():

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

    # sortして走破？
    hlist.sort()
    l = []

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

    print(min(l))


if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N, K = MAP()
h = [INT() for _ in range(N)]
h.sort()

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

print(a)

Submission status: Accepted

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

h.sort()

left = h[0]
right = h[K - 1]
min_diff = right - left
for i in range(len(h)):
    if i <= K - 1: continue

    left = h[i - K + 1]
    right = h[i]
    diff = h[i] - left

    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(reverse=True)

ans = h[0]

for i, j in zip(h[:-K + 1], h[K - 1:]):
    div = i - j
    if div == 0:
        ans = 0
        break
    if div < ans:
        ans = div

print(ans)

Submission status: Accepted

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

min = 1000000000
for i in range(n-k+1):
  dif = array[i+k-1] - array[i]
  if min>dif:
    min = dif
    
print(min)

Submission status: Accepted

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

h.sort()

result = 10**9

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

Submission status: Accepted

Solution code:
# solution

nim, mike = map(int, input().split())
array = [int(input()) for _ in range(nim)]

array.sort()
m = sum(array)

for i in range(nim - mike + 1):
    m = min(array[i + mike - 1] - array[i], m)
print(m)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,k = map(int, input().split())
h = sorted([int(input()) for i in range(n)])
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:
# from math import factorial,sqrt,ceil,gcd
# from itertools import permutations as permus
# from collections import deque,Counter
# import re
# from functools import lru_cache # 簡単メモ化 @lru_cache(maxsize=1000)
# from decimal import Decimal, getcontext
# # getcontext().prec = 1000
# # eps = Decimal(10) ** (-100)

# import numpy as np
# import networkx as nx
# from scipy.sparse.csgraph import shortest_path, dijkstra, floyd_warshall, bellman_ford, johnson
# from scipy.sparse import csr_matrix
# from scipy.special import comb

# slist = "abcdefghijklmnopqrstuvwxyz"
N,K = map(int,input().split())
arrH = [int(input()) for _ in range(N)]
arrH.sort()

ans = 10**9+1
for i in range(N-K+1):
    num = arrH[i+K-1] - arrH[i] 
    ans = min(ans,num)

print(ans)
# print(*ans)   # unpackして出力。間にスペースが入る
# for row in board:
#     print(*row,sep="")    #unpackして間にスペース入れずに出力する
# print("{:.10f}".format(ans))
# print("{:0=10d}".format(ans))

Submission status: Accepted

Solution code:
def cChristmas(n, k, hl):


    hl.sort()
    ans = 10**10

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

    return ans

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

if __name__ == '__main__':
    main()

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

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
li = []
for _ in range(n):
    i = int(input())
    li.append(i)
li.sort()
INF = float('inf')
diff = INF
for i,l in enumerate(li):
    if i + k - 1 > len(li)-1:
        break
    if li[i+k-1] - li[i] < diff:
        diff = li[i+k-1] - li[i]
print(diff)

Submission status: Accepted

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

ans=10000000000
tmp=0
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:
import bisect,collections,copy,heapq,itertools,math,operator,string
def I(): return int(input())
def S(): return input()
def LI(): return list(map(int,input().split()))
def LS(): return list(input().split())
##################################################
N,K = LI()
h = sorted([I() for _ in range(N)])
ans = float('INF')
for i in range(N-K+1):
    ans = min(h[i+K-1]-h[i],ans)
print(ans)

Submission status: Accepted

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

ans=1000000000000000

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

Submission status: Accepted

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

a = 0
b = 0
_min = 10 ** 9

h.sort()

for i in range(K-1,len(h)):
    if _min > h[i] - h[i-K+1]:
        _min = h[i] - h[i-K+1]

print(_min)

Submission status: Accepted

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

minvalue = 10**9
h.sort()
for i in range(N-K+1):
    if abs(h[K-1+i]-h[i])<minvalue:
        minvalue = abs(h[K-1+i]-h[i])

print(minvalue)

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
h = sorted([int(input()) for _ in range(n)])
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=list(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:
def myAnswer(N:int,K:int,H:list) -> int:
   H.sort()
   ans = 10**9
   i = 0
   for j in range(K-1,len(H)):
      ans = min(ans,H[j] - H[i])
      i +=1
   return ans


def modelAnswer():
   tmp=1
def main():
   N, K = map(int,input().split())
   H = []
   for _ in range(N):
      H.append(int(input()))
   print(myAnswer(N,K,H[:]))

if __name__ == '__main__':
   main()

Submission status: Accepted

Solution code:
import numpy as np
import sys

n, k = map(int, input().split())
h = []
for i in range(n):
	p = int(input())
	h.append(p)
h.sort()
m = 10000000000
for i in range(n - k + 1):
	if(h[k + i - 1] - h[i] < m):
		m = h[k + i - 1] - h[i]
print(m)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n, k, *h = map(int, open(0).read().split())
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 i in range(N):
    h.append(int(input()))

h.sort()

m = h[-1] #現時点での最大

for i in range(N-K+1):
    x = h[K+i-1] - h[i]
    if m > x:
        m = x
print(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 = [int(input()) for i 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=[0]*n
for i in range(n):
  h[i]=int(input())
h=sorted(h,reverse=True)
#print(h)
ans=10**9
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())
k = []
for i in range(N):
  h = int(input())
  k.append(h)
k.sort()
k.reverse()
minimum = 10**10
for i in range(N-K+1):
  if k[i]-k[i+K-1] < minimum:
    minimum = k[i]-k[i+K-1]
print(minimum)

Submission status: Accepted

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

h_ls.sort()
ans = float('inf')
for i in range(n-k+1):
    ans = min(ans, h_ls[i+k-1] - h_ls[i])
print(ans)

Submission status: Accepted

Solution code:
import sys
import numpy as np

n, k = map(int, sys.stdin.readline().split())
h = np.array(sys.stdin.read().split(), dtype=np.int64)
h.sort()

def main():
    res = np.amin(h[k-1:] - h[:-k+1])
    print(res)

if __name__ ==  '__main__':
    main()

Submission status: Accepted

Solution code:
#from statistics import median
#import collections
#aa = collections.Counter(a) # list to list
#from itertools import combinations # (string,3) 3回

mod = 10**9 + 7

def readInts():
  return list(map(int,input().split()))
def main():
    n,k = readInts()
    A = [int(input()) for _ in range(n)]
    A = sorted(A)
    min_ = float('inf')
    for i in range(n-k+1):
        min_ = min(min_,abs(A[i] - A[i+k-1]))
    print(min_)



if __name__ == '__main__':
  main()

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
l = []
for i in range(n):
    l.append(int(input()))
l.sort()    
ans = 999999999999
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())
a = [int(input()) for i in range(n)]

a.sort(reverse=True)
s = 10000000000
for i in range(k, n + 1):
    s = min(a[i - k] - a[i - 1], s)

print(s)

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

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
ans = 10**9

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

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

print(ans)

Submission status: Accepted

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

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

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

h.sort()
dif_min =max(h)
for i in range(n-k+1):
    dif = h[i+k-1]-h[i]
    #print(str(h[i])+" "+str(h[i+k-1]))
    if dif < dif_min:
        dif_min = dif

print(dif_min)

Submission status: Accepted

Solution code:
def ii():return int(input())
def iim():return map(int,input().split())
def iil():return list(map(int,input().split()))
def ism():return map(str,input().split())
def isl():return list(map(str,input().split()))

n,k = iim()
H = []
for _ in range(n):
    H.append(ii())
H.sort()

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

Submission status: Accepted

Solution code:
import sys
from collections import Counter
from itertools import accumulate
def input(): return sys.stdin.readline().rstrip()
def main():
    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)
if __name__=='__main__':
    main()

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def S(): return input().rstrip()
def LS(): return S().split()
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")

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


#main
if __name__ == '__main__':
    solve()

Submission status: Accepted

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

for i in range(len(h)-k+1):
    if h[i+k-1] - h[i] < ans:
        ans = h[i+k-1] - h[i]
        if ans == 0:
            break

print(ans)

Submission status: Accepted

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip()

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

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

h.sort()

ans = float("inf")

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

print(ans)

Submission status: Accepted

Solution code:
import heapq

def main():
    n,k = map(int,input().split())
    tree_heights = []

    for i in range(n):
        tree_heights.append(int(input()))
    tree_heights.sort()
    #print(tree_heights)

    mn = 1000000000000000000000000000000
    for i in range(n-k+1):
        #print(i,abs(tree_heights[i]-tree_heights[i+k-1]))
        mn = min(abs(tree_heights[i]-tree_heights[i+k-1]),mn)
    print(mn)

    #print(tree_heights)
    #print(tree_diffs)
    #print(sum(tree_diffs[0:k]))





if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:

url = "https://atcoder.jp//contests/abc115/tasks/abc115_c"

def main():
    tree_n, sl = list(map(int, input().split()))
    trees = [int(input()) for i in range(tree_n)]
    trees.sort()
    tmp = trees[:sl]
    print(min(trees[i + sl - 1] - trees[i] for i in range(tree_n - sl + 1)))


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
H=[int(input()) for i in range(n)]
H.sort()
ans=10**10
for i in range(n-k+1):
    ans=min(H[i+k-1]-H[i],ans)
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))) # i番目からK個めまで見た時の最小値をウインドウをスライドさせて見ていく

Submission status: Accepted

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

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

main()

Submission status: Accepted

Solution code:
from sys import stdin

N, K = [int(x) for x in stdin.readline().rstrip().split()]
h = [0]*N
for i in range(N):
    h[i] = int(input())
h.sort()
# print(h)
ans = 10**9
for i in range(N-(K-1)):
    if h[i+(K-1)]-h[i] < ans:
        ans = h[i+(K-1)]-h[i]
print(ans)

Submission status: Accepted

Solution code:

N, K = 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())
hlist = sorted([int(input())for i in range(n)])

ans = 10**9
i = 0

while i+(k-1) <= n-1:
  ans = min(ans, hlist[k+i-1]-hlist[i])
  i += 1

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

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

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

Submission status: Accepted

Solution code:
import sys

readline = sys.stdin.readline
read = sys.stdin.read
n, k = map(int, readline().split())
h = list(map(int, read().split()))
h.sort()
print(min(h[i+k-1] - h[i] for i in range(n-k+1)))

Submission status: Accepted

Solution code:
#coding:utf-8
import sys,os
from collections import defaultdict, deque
from fractions import gcd
from math import ceil, floor
sys.setrecursionlimit(10**6)
write = sys.stdout.write
dbg = (lambda *something: print(*something)) if 'TERM_PROGRAM' in os.environ else lambda *x: 0
def main(given=sys.stdin.readline):
    input = lambda: given().rstrip()
    LMIIS = lambda: list(map(int,input().split()))
    II = lambda: int(input())
    XLMIIS = lambda x: [LMIIS() for _ in range(x)]
    YN = lambda c : print('Yes') if c else print('No')
    MOD = 10**9+7

    N,K = LMIIS()
    H = [0]*N
    for i in range(N):
        H[i] = II()
    H.sort(reverse=True)
    mindiff = 10**9
    for i in range(N-K+1):
        mindiff = min(H[i] - H[i+K-1], mindiff)
    print(mindiff)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

h.sort()

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

print(min_d)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

INF = 10**9+1

h.sort()
ans = INF

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

H.sort()

ans = 1e+9

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

print(ans)

Submission status: Accepted

Solution code:
import numpy as np
N, K = list(map(int,input().split()))
a = [int(input()) for i in range(N)]
a = sorted(a, reverse = True)
ans = np.zeros(len(a)-K+1)
for i in range(len(a)-K+1):
    ans[i] = a[i]-a[i+K-1]
print(int(ans.min()))

Submission status: Accepted

Solution code:
from collections import deque
from collections import Counter
from itertools import product, permutations,combinations
from operator import itemgetter
from heapq import heappop, heappush
from bisect import bisect_left, bisect_right, bisect
#pypyではscipy, numpyは使えない
#from scipy.sparse.csgraph import shortest_path, floyd_warshall, dijkstra, bellman_ford, johnson, minimum_spanning_tree
#from scipy.sparse import csr_matrix, coo_matrix, lil_matrix
import numpy as np
from fractions import gcd
from math import ceil,floor, sqrt, cos, sin, pi, factorial
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10**8)
INF = float('inf')
MOD = 10**9+7

def main():
  n, k = map(int, readline().split())
  h = list(int(readline()) for i in range(n))
  h.sort()
  ans = INF
  for i in range(n-k+1):
    ans = min(ans,h[i+k-1]-h[i])
  print(ans)
  
  
if __name__ == '__main__':
  main()

Submission status: Accepted

Solution code:

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

t = []

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

Submission status: Accepted

Solution code:
from itertools import combinations

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

'''
res = 10**10
for i in combinations(h,K):
    tmp = max(i) - min(i)
    res = min(res,tmp)
'''

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

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
H = sorted([int(input()) for n in range(N)])
a = 1e+9

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

print(a)

Submission status: Accepted

Solution code:
import math
N,K=map(int,input().split())
tree=[]
ans=0
for i in range(N):
    tree.append(int(input()))
tree.sort()
#print(tree)
for i in range(N-K+1):
    if i==0:
        ans=tree[K+i-1]-tree[i]
        #print(ans)
    elif ans>=tree[K+i-1]-tree[i]:
        ans = tree[K + i - 1] - tree[i]
        #print(ans)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n, k, *H = map(int, open(0).read().split())
H.sort()
ans = 10 ** 10
for h0, h1 in zip(H, H[k-1:]):
  ans = min(ans, h1-h0)
print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
# int(input()) # 入力が1つ
n, k = map(int, input().split()) # 入力が複数
tree = []
for _ in range(n):
    h = int(input())
    tree.append(h)
tree.sort()

diff = float('inf')
s = 0
e = k - 1
while e < n:
    d = tree[e] - tree[s]
    diff = min(diff, d)
    s += 1
    e += 1
print(diff)

Submission status: Accepted

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

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

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

Submission status: Accepted

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

Submission status: Accepted

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

hs.sort()

value_list = []

for i in range(len(hs)):
    try:
        value_list.append(hs[i+k-1] - hs[i])
    except:
        break

print(min(value_list))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
h = [int(input()) for _ in range(n)]
h.sort()
ans = 10 ** 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_list = sorted([int(input()) for _ in range(n)])
mi = 10 ** 9
for i in range(k - 1, n):
    mi = min(mi, h_list[i] - h_list[i - (k - 1)])
print(mi)

Submission status: Accepted

Solution code:
n, k = list(map(int, input().split()))
h = [int(input()) for i in range(n)]
h = sorted(h)
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 = sorted(h)
r = 10**10

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

Submission status: Accepted

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

Submission status: Accepted

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