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

Submission status: Accepted

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

def one_int():
    return int(input())

def one_str():
    return input()

def many_int():
    return list(map(int, input().split()))

N,K = two_int()

trees = []
for i in range(N):
    trees.append(one_int())

sort_trees = sorted(trees)

mins = 10**10
for i in range(N-K+1):
    if sort_trees[i+K-1] - sort_trees[i] < mins:
        mins = sort_trees[i+K-1] - sort_trees[i]

print(mins)

Submission status: Accepted

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

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())
a = []
for i in range(N):
    a.append(int(input()))
a.sort()
ans = 10**9 + 7
for i in range(N-K+1):
    if ans > a[K-1+i] - a[i]:
        ans = a[K-1+i] - a[i]
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

H.sort()

answer = 10 ** 15
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 = [int(input()) for i in range(N)]
h.sort()
mini = 10**10
for i in range(N - K + 1):
    mini = min(mini, h[i+K-1] - h[i])
print(mini)

Submission status: Accepted

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

height.sort()

diff = [height[i+k-1] - h for i, h in enumerate(height) if i+k-1 < len(height)]
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 = 1000000000

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=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()
saisho=10**12
for i in range(n-k+1):
    saisho=min(saisho,abs(H[i+k-1]-H[i]))
print(saisho)

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

Submission status: Accepted

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

ans = 1<<60
for i in range(N-K+1):
  x = A[i+K-1] - A[i] # max-min
  ans = min(ans, x)
print(ans)

Submission status: Accepted

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

h = sorted(h)
ans = []

for i in range(len(h)-K+1):
  ans.append(h[i+K-1]-h[i])
  if ans[-1] == 0:
    print(0)
    exit()

print(min(ans))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
h = []
for i in range(n):
    h.append(int(input()))
h.sort()
min_number = 10**9+1
for i,j in zip(h[0:n-k+1],h[k-1:]):
    min_number = min(min_number,j-i)
    #print('{} {}'.format(i,j))
print(min_number)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
n,k = map(int,readline().split())
p = [int(input()) for i in range(n)]
p.sort()
ans = 10**9 
for i in range(n-k+1):
    if p[i+k-1]-p[i] < ans: ans =  p[i+k-1]-p[i]
print(ans)

Submission status: Accepted

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

trees = []
for i in range(N):
    trees.append(int(input()))

trees = sorted(trees)

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
import os
import itertools
import _collections
import string

class cin():
    def int():
        return int(sys.stdin.readline().rstrip())
    def string():
        return sys.stdin.readline().rstrip()
    def mapInt():
        return [int(x) for x in cin.string().split()]
    def stringList(n):
        return [input() for i in range(n)]
    def intListList(n):
        return [cin.mapInt() for i in range(n)]
    def intColsList(n):
        return [int(input()) for i in range(n)]

class Util():
    def roundUp(a,b):
        return -(-a // b)
    def toUpperMultiple(a,x):
        return Util.round_up(a,x) * x
    def toLowerMultiple(a,x):
        return (a // x) * x
class Math():
    def gcd(a,b):
        if b == 0:
            return a
        return Math.gcd(b,a % b)

def main():
    N,K = cin.mapInt()
    H = cin.intColsList(N)
    H.sort()
    ans = int(1e09)
    for i in range(N - K + 1):
        ans = min(ans,H[i + K - 1] - H[i])
    print(ans)
    return

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 = sorted(h)

minval = int(1e10)

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

Submission status: Accepted

Solution code:
def main():
    N, K = map(int, input().split(" "))
    tree_height = []
    for _ in range(N):
        tree_height.append(int(input()))
    tree_height.sort()
    result = 10 ** 10
    for i in range(N - K + 1):
        result = min(result, tree_height[i + K - 1] - tree_height[i])
    print(result)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
# from fractions import gcd
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from functools import reduce
from bisect import bisect_left, insort_left
from heapq import heapify, heappush, heappop

INPUT = lambda: sys.stdin.readline().rstrip()
INT = lambda: int(INPUT())
MAP = lambda: map(int, INPUT().split())
S_MAP = lambda: map(str, INPUT().split())
LIST = lambda: list(map(int, INPUT().split()))
S_LIST = lambda: list(map(str, INPUT().split()))

sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7


def main():
    N, K = MAP()
    H = [INT() for i in range(N)]
    H = sorted(H)

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

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

H = [int(x) for x in sys.stdin.readlines()]

H.sort()

ans = 10 ** 10
for i in range(K - 1, N):
    left = H[i - (K - 1)]
    right = H[i]
    ans = min(ans, right - left)

print(ans)

Submission status: Accepted

Solution code:
#coding: utf-8
import math
import heapq
import bisect
import numpy as np
#from scipy.misc import comb

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

# trees = []
# for _ in range(N)
#    trees.append(int(input()))
trees = [int(input()) for _ in range(N)]

new_trees = sorted(trees)  # .sort()は破壊型なので避ける

d = 10 ** 9
for idx in range(N - K + 1):
    need_d = new_trees[idx + K - 1] - new_trees[idx]
    if d > need_d:
        d = need_d
print(d)

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 = float('INF')
for i in range(n-k+1):
    # print(h[i+k-1], i+k-1)
    if h[i+k-1] - h[i] < ans:
        ans = h[i+k-1] - h[i]
print(ans)

Submission status: Accepted

Solution code:
import sys
 
stdin = sys.stdin
 
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()
nas = lambda: stdin.readline().split()

n, k = na()
h = [ni() for i 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:
n,k = map(int,input().split())
l = [int(input()) for i in range(n)]
l.sort()
ans = l[-1]
for i in range(n-k+1):
  ans = min(ans,l[i+k-1]-l[i])
  if ans == 0:
    print(0)
    exit()
  
print(ans)

Submission status: Accepted

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

A=sorted(A)
t=10**9
for i in range(N-K+1):
    t=min(t,abs(A[K-1+i]-A[i]))
print(t)

Submission status: Accepted

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

Submission status: Accepted

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

h.sort()
m = sum(h)

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

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
hn = []
for i in range(N):
  hn.append(int(input()))
hn = sorted(hn,reverse=True)
min = 10**9+7
for i in range(N-K+1):
#  print("hn[i] is {} , hn[K+i] is {}".format(hn[i],hn[K+i-1]))
  if min > (hn[i]-hn[K+i-1]):
    min = hn[i]-hn[K+i-1]
print(min)

Submission status: Accepted

Solution code:
import sys

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

    h = sorted(h)

    ave = []
    for j in range(0,N-K+1):
        ave.append(abs(h[j]-h[j+K-1]))

    print(min(ave))

main()

Submission status: Accepted

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

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

print(min_diff)

Submission status: Accepted

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

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

print(min_diff)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

n, k, *h = map(int, sys.stdin.read().split())

def main():
    h.sort()
    
    res = float('inf')
    for i in range(n-k+1):
        res = min(res, h[i+k-1] - h[i])
    
    return res

if __name__ == '__main__':
    ans = main()
    print(ans)

Submission status: Accepted

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

h.sort()

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

print(min(d))

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
def input():
    return sys.stdin.readline().rstrip()

def main():
    N,K=map(int,input().split())
    h=[int(input()) for _ in range(N)]+[INF]*K
    h.sort()
    ans=INF
    for i in range(N):
        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())
L=sorted([int(input()) for i in range(n)])
r=10**9
for i in range(n-k+1):
    r=min(r,L[i+k-1]-L[i])
print(r)

Submission status: Accepted

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

H.sort()
minv = float('inf')

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

print(minv)

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):
    #print(i,i+k-1)
    ans = min(ans,h[i+k-1]-h[i])
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):
    if ans>h[i+K-1]-h[i]:
        ans=h[i+K-1]-h[i]
    else:
        continue
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_tree=max(h)
for i in range(N-K+1):
  if h[i+K-1]-h[i]<min_tree:
    min_tree=h[i+K-1]-h[i]
print(min_tree)

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
H=[int(input()) for i in range(N)]
A=sorted(H)[::-1]
ans=10**9
for i in range(N-K+1):
  a=A[i]-A[i+K-1]
  ans=min(ans,a)
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:
import sys
input = sys.stdin.readline


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

    ans = float("inf")
    h.sort()
    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()) for _ in range(n)],reverse=True)
ans = h[0] - h[k-1]
for i in range(0,n-k+1):
    ans = min(ans, h[i] - h[i+k-1])
print(ans)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
h=[int(input()) for _ in range(n)]
h.sort()
min1=10**9*1
soezi=0
count=0
for i in range(n-(k-1)):
    count=h[i+(k-1)]-h[i]
    if min1>count:
        min1=count
print(min1)

Submission status: Accepted

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

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

print(count)

Submission status: Accepted

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

h.sort()
min = h[-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 = sorted(int(input()) for _ in range(N))
print(min(h1-h0 for h0,h1 in zip(H, H[K-1:])))

Submission status: Accepted

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

ans = float("inf")
for i, h_i in enumerate(h):
    if i + K - 1 == N:
        break
    ans = min(ans, h[i + K - 1] - h_i)
print(ans)

Submission status: Accepted

Solution code:
# coding: utf-8
N, K = map(int, input().split())
H = []
for i in range(N):
    h = int(input())
    H.append(h)
H.sort()
L = []
ans = float("inf")
for i in range(N-K+1):
    ans = min(ans, abs(H[i] - H[i+K-1]))
print(ans)

Submission status: Accepted

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

trees = []
for _ in range(0, n):
  trees.append(int(input()))
trees.sort()
trees.reverse()
ans = 100000000000
for i in range(0, n-k+1):
  ans = min(ans, trees[i] - trees[i+k-1])
print(ans)

Submission status: Accepted

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

for i in range(n):
    h.append(int(input()))
h.sort()
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, k = map(int, input().strip().split())
hs = sorted([int(input()) for _ in range(n)])

ans = 1e15

for i in range(n - k + 1):
    h_max = hs[i + k - 1]
    h_min = hs[i]
    ans = min(h_max - h_min, 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):
  try:
    ans=min(ans,h[k+i-1]-h[i])
  except:
    pass
print(ans)

Submission status: Accepted

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

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

l = [0]
h.sort()


t = max(h)

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

print(t)

Submission status: Accepted

Solution code:
# ABC115 C - Christmas Eve
# 案1. nCk通りについてhmax - hminを網羅的に計算する(TLE)
# 案2. ソートして、1〜Kまで、2〜K+1まで、N-K+1〜Nを調べる

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

h = sorted(h)
ans = 9999999999
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 =sorted(H)

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

def main():
    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[K+i-1]-h[i])
        
    print(ans)
    
if __name__ == "__main__":
    main()

Submission status: Accepted

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

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

min = float("inf")
for i in range(N-K+1):
    temp = h[K+i-1]-h[i]
    if min > temp:
        min = temp

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import os
import sys

import numpy as np

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353


N, K = list(map(int, sys.stdin.buffer.readline().split()))
H = [int(sys.stdin.buffer.readline()) for _ in range(N)]

H = np.array(H, dtype=int)

H.sort()
d = N - K + 1
a = H[-d:] - H[:d]
print(a.min()
      )

Submission status: Accepted

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

ls.sort(reverse=True)

ans = 10 ** 9

for i in range(n - k + 1):
	sa = ls[i] - ls[i + k - 1]
	if sa <= ans:
		ans = sa

print(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:
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 i in range(N)]

H.sort()

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

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
h=[int(input()) for _ in range(n)]
h.sort(reverse=True)
r=n-(k-1)
ans= float('inf')
for i in range(r):
    if ans>h[i]-h[i+k-1]:
        ans=h[i]-h[i+k-1]
print(ans)

Submission status: Accepted

Solution code:

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

H = sorted(H)
ans = 999999999999999999999
for i in range(N-K+1):
  ans = min(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())
H=[0]*N
for i in range(N):
    H[i]=int(input())
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()
mindh = 10**9

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

ans = mindh
print(ans)

Submission status: Accepted

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

h.sort()
dh = [h[i+1]-h[i] for i in range(0,n-1)]

mindh = dhsum = sum(dh[:k-1])
for i in range(1,n-k+1):
    dhsum = dhsum-dh[i-1]+dh[i+k-2]
    mindh = min(dhsum,mindh)

ans = mindh
print(ans)

Submission status: Accepted

Solution code:
def solve():
    N, K, *h = map(int, open(0).read().split())
    h.sort(reverse=True)
    
    ans = 10 ** 9
    for i in range(N - K + 1):
        ans = min(ans, h[i] - h[i + K - 1])
    print(ans)


if __name__ == '__main__':
    solve()

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:

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

trees = []

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


trees.sort()

ans = 1000000000
for i in range(N-K+1):
    aa = trees[i+K-1]-trees[i]
    if aa < ans:
        ans = aa

print(ans)

Submission status: Accepted

Solution code:
n,s=map(int, input().split())
N=[]
for _ in range(n):
    N.append(int(input()))
N=sorted(N)
diff_list=[]
for i in range(s-1,len(N)):
    diff_list.append(N[i]-N[i-s+1])
print(min(diff_list))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
from sys import stdin
n,k = map(int,input().split())
l = [int(stdin.readline()) for _ in [0]*n]
l.sort()
difl = [0]*(n-k+1)
for i in range(n-k+1):
  difl[i] = l[i+k-1]-l[i]
print(min(difl))

Submission status: Accepted

Solution code:
import sys
import os
if sys.platform=="darwin":
	base = os.path.dirname(os.path.abspath(__file__))
	name = os.path.normpath(os.path.join(base, '../Documents/input.txt'))
	#print(name)
	sys.stdin = open(name)

input = sys.stdin.readline

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

#print(l)

l = sorted(l)

#print(l)

ansmin = float("inf")
for i in range(n-k+1):
	if ansmin > l[i+k-1]-l[i]:
		ansmin = l[i+k-1]-l[i]

print(ansmin)

Submission status: Accepted

Solution code:
def main():
    N, K, *h = map(int, open(0).read().split())
    h.sort()
    print(min(j - i for i, j in zip(h, h[K - 1:])))
    return


main()

Submission status: Accepted

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

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())
lst=[int(input()) for _ in range(n)]
lst=sorted(lst)
ans=float("inf")
for i in range(n+1-k):
  ans=min(ans,lst[k-1+i]-lst[i])
print(ans)

Submission status: Accepted

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

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

DR = [1, -1, 0, 0]
DC = [0, 0, 1, -1]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
     
def main():
    N, K = LI()
    h = []
    for i in range(N):
        H = I()
        h.append(H)
    h = sorted(h)
    minGap = inf
    for i in range(N):
        st = i
        en = i + K - 1
        if en >= N:
            break
        minGap = min(minGap, h[en] - h[st])
    print(minGap)

main()

Submission status: Accepted

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

Submission status: Accepted

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

a = sorted(h)
d = [a[i] - a[i - (K - 1)] for i in range(K - 1, N)]
ans = min(d)

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

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
treelist = []
difflist = []
append = treelist.append
#print(treelist)
append_diff = difflist.append
for i in range(N):
    tree = int(input())
    #print(tree)
    append(tree)
treelist.sort()
#print(treelist)
for i in range(K-1,N):
    diff = treelist[i] - treelist[i-K+1]
    #print(diff)
    append_diff(diff)
print(min(difflist))

Submission status: Accepted

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

Submission status: Accepted

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

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 = list(map(int, input().split()))
# print(N)
# print(K)
h_list = [0]*100000
i = 0
for i in range(N):
    x = int(input())
    h_list[i] = x
    x = 0
j = 0
min = 1000000000
for i in range(100000 - N):
    h_list.remove(0)
# print(h_list)
h_list.sort()
# print(h_list)
y = 0
q = 0
while q < N-K+1:
    y = h_list[q + K-1] - h_list[q]
#    print(q)
#    print(h_list[q+K-1], h_list[q])
    if y < min:
        min = y
        y = 0
    else:
        y = 0
    q += 1
print(min)

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