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

answer = int(10e9)
for i in range(n - k + 1):
    d = h[i + k - 1] - h[i]
    if d < answer:
        answer = d

print(answer)

Submission status: Accepted

Solution code:
from sys import stdin

def main():
    N, K = map(int, input().split())
    H = list(map(int, stdin.read().splitlines()))

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

input = lambda: stdin.readline()
main()

Submission status: Accepted

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

print(min)

Submission status: Accepted

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

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

ans=10**9

for i in range(N-(K-1)):
  if ans > abs(H[i]-H[i+(K-1)]):
    ans = abs(H[i]-H[i+(K-1)])
print(ans)

Submission status: Accepted

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

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

h_list.sort()

answer = 1e+9

for i in range(N-K+1):
	h_min = h_list[i]
	h_max = h_list[i+K-1]

	if answer > (h_max - h_min):
		answer = h_max - h_min

print(answer)

Submission status: Accepted

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

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

    print(diff)


abc115_c()

Submission status: Accepted

Solution code:
a = input().split()
a = [int(i) for i in a]
h = []
for i in range(a[0]):
	h.append(int(input()))
	
h.sort()
miner = h[-1] - h[0]
for i in range(a[0] - a[1] + 1):
	miner = min(miner, h[a[0] - 1 - i] - h[a[0] - i - a[1]])
	
print(miner)

Submission status: Accepted

Solution code:
def solve(N, K, Hs):
    Hs.sort()
    return min([b - a for (a, b) in zip(Hs[:], Hs[K - 1:])])


if __name__ == "__main__":
    N, K = tuple(map(int, input().split(" ")))
    Hs = [int(input()) for _ in range(N)]
    print(solve(N, K, Hs))

Submission status: Accepted

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

Submission status: Accepted

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

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

H = sorted(H)

tmp = 0
ans = H[-1]
for i in range(N-K+1):
    tmp = H[i+K-1] - H[i]
#    print(H[i:i+K])

    ans = min(ans, tmp)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, K = map(int, raw_input().split())
h = [0]*N
for i in range(N):
  h[i], = map(int, raw_input().split())
h.sort()
r = h[-1]-h[0]
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 = [int(input()) for i in range(N)]
H.sort()
ans = H[-1]
for i in range(N-K+1):
    min_h = H[i]
    max_h = H[i+K-1]
    dif = max_h - min_h
    if dif < ans:
        ans = dif
print(ans)

Submission status: Accepted

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


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

print(result)

Submission status: Accepted

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

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

tree.sort()
ans = 10 ** 9
# print(tree)
for i in range(k - 1, n):
    # print(i, i - k + 1)
    tmp = tree[i] - tree[i - k + 1]
    ans = min(ans, tmp)

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

Submission status: Accepted

Solution code:
a=list(map(int,input().split()))
N=a[0]
K=a[1]
h=[]
for i in range(N):
    h.append(int(input()))
h.sort()
min=abs(h[K-1]-h[0])
for i in range(N-K+1):
    if abs(h[i]-h[i+K-1]) < min:
        min=abs(h[i]-h[i+K-1])
print(str(min))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
d = []
for i in range(n):
    d.append(input())
# [d1, d2, d3, ..., dN]
h = [int(m) for m in d]#int型に直す
h.sort(reverse=True)
li = []
for i in range(n):
    if i + k <= n:
        li.append(h[i] - h[i + k - 1])
    else:
        break
print(min(li))

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

high_diff = []

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

print(min(high_diff))

Submission status: Accepted

Solution code:
nk = list(map(int, input().split()))
n = nk[0]
k = nk[1]
h = list()
for i in range(n):
    h.append(int(input()))
h.sort()
min_h = 10 ** 10
for i in range(n - k + 1):
    dis_h = h[i + k - 1] - h[i]
    if dis_h < min_h:
        min_h = dis_h
print(min_h)

Submission status: Accepted

Solution code:
#JMD
#Nagendra Jha-4096

 
import sys
import math

#import fractions
#import numpy
 
###File Operations###
fileoperation=0
if(fileoperation):
    orig_stdout = sys.stdout
    orig_stdin = sys.stdin
    inputfile = open('W:/Competitive Programming/input.txt', 'r')
    outputfile = open('W:/Competitive Programming/output.txt', 'w')
    sys.stdin = inputfile
    sys.stdout = outputfile

###Defines...###
mod=1000000007
 
###FUF's...###
def nospace(l):
    ans=''.join(str(i) for i in l)
    return ans
 
 
 
##### Main ####
t=1
for tt in range(t):
    #n=int(input())
    #a=list(map(int,sys.stdin.readline().split(' ')))
    n,k= map(int, sys.stdin.readline().split(' '))
    h=[]

    for jj in range(n):
        p=int(input())
        h.append(p)

    ans=mod
    h.sort()

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

    
    
#####File Operations#####
if(fileoperation):
    sys.stdout = orig_stdout
    sys.stdin = orig_stdin
    inputfile.close()
    outputfile.close()

Submission status: Accepted

Solution code:
ans=10**9
A=[]
n,k=[int(i) for i in input().split()]
for i in range(n):
    j=int(input())
    A.append(j)
A.sort(reverse=True)
for i in range(n-k+1):
    ans=min(ans,A[i]-A[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()

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

L.sort()
res = 10 ** 10
for i in range(N-K+1):
    res = min(L[i+K-1] - L[i], res)
print(res)

Submission status: Accepted

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

    l = []

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

    l.sort()

    m = 10**10
    for i in range(n-k+1):
        if l[i+k-1] - l[i] < m:
            m = l[i+k-1] - l[i] 
    print(m)

if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
h=[]
for i in range(n):
    h.append(int(input()))
h.sort(reverse=True)
ans=h[0]-h[n-1]
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())
p = [int(input()) for i in range(n)]

p.sort()
ans = 10 ** 20

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

Submission status: Accepted

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

h_sort = sorted(h, reverse=True)

max_res = 10000000000
for i in range(n-k+1):
    if h_sort[i] - h_sort[i+k-1] < max_res:
        max_res = h_sort[i] - h_sort[i+k-1]

print(max_res)

Submission status: Accepted

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


def solve():
    n, k = map(int, input().split())
    hl = [int(input()) for _ in range(n)]
    hl.sort()
    diff = hl[k - 1] - hl[0]
    for i in range(0, n - k + 1):
        td = hl[k - 1 + i] - hl[i]
        diff = min(td, diff)
    print(diff)


if __name__ == "__main__":
    solve()

Submission status: Accepted

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

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

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

h.sort()

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

print(min(h_diff))

Submission status: Accepted

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

ans=1000000000
for j in range(N-K+1):
    if abs(A[j]-A[j+K-1])<ans:
        ans=abs(A[j]-A[j+K-1])
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# encoding: utf-8
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):
    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 = list(map(int, [input() for i in range(n)]))
h.sort()
res = 10**10
for i in range(k-1, n):
    res = min(res, h[i]-h[i-k+1])
print(res)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

h.sort()
ans = 100000000000000000000

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

print(ans)

Submission status: Accepted

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

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

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

Submission status: Accepted

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


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
(n, k) = list(map(int, input().split()))
h = [int(input()) for _ in range(n)]
h.sort()
l = 0
k -= 1
minh = 10000000000
for i in range(k, n):
    minh = min(minh, h[i] - h[i - k])
print(minh)

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

hs.sort()

ans = 10 ** 10
for i in range(N - K + 1):
    ans = min(ans, hs[i + K - 1] - hs[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**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 = []
for i in range(n):
    temp = int(input())
    h.append(temp)
h.sort()
i = 0
min = h[k-1]-h[0]
while i+k <= n:
    if min > (h[k+i-1]-h[i]):
        min = h[k+i-1]-h[i]
    i += 1
print(min)

Submission status: Accepted

Solution code:
N, K = [int(a) for a in input().split()]
K -= 1
num_list = []
for i in range(N):
    num_list.append(int(input()))

num_list.sort()
beaut_list = [b-a for a, b in zip(num_list[:-K], num_list[K:])]

print(str(min(beaut_list)))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
H = [int(input()) for _ in range(N)]
H.sort()
ans = float('inf')
for i in range(N-K+1):
  d = H[i+K-1] - H[i]
  ans = ans if ans < d else d
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

import array
from bisect import *
from collections import *
import fractions
import heapq 
from itertools import *
import math
import random
import re
import string
import sys

N, K = map(int, input().split())
Hs = []
for n in range(N):
    Hs.append(int(input()))
Hs.sort()

diff_min = 1e10
for i in range(N - (K - 1)):
    j = i + (K - 1)
    x = Hs[i]
    y = Hs[j]
    mn = min(x, y)
    mx = max(x, y)
    diff = mx - mn
    if diff < diff_min:
        diff_min = diff
print(diff_min)

Submission status: Accepted

Solution code:
def C():
    N, K = map(int, input().split())
    h = [int(input()) for i in range(N)]
    h.sort()
    hmin = h[0]
    hmax = h[K - 1]
    ans = 100000000000000
    for i in range(K - 1, N):
        hmin = h[i - K + 1]
        hmax = h[i]
        if ans > hmax - hmin:
            ans = hmax - hmin
    print(ans)
C()

Submission status: Accepted

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

h.sort()

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

print(ans)

Submission status: Accepted

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

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:
# -*- coding: utf-8 -*-
n,k = map(int, input().split())
hl = []
for _ in range(n):
    hl.append(int(input()))
hl.sort()

res = 10**9
for i in range(n-k+1):
    hmin,hmax = hl[i],hl[i+k-1]
    res = min(res, hmax-hmin)

print(res)

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

Submission status: Accepted

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

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

ans=10**20
for i in range(N-K+1):
    if ans > arr[i+K-1]-arr[i]:
        ans=arr[i+K-1]-arr[i]

print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
def inpl(): return list(map(int, input().split()))
N, K = inpl()
H = sorted([int(input()) for _ in range(N)])

ans = 10**9
for i in range(K-1, N):
    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 = sorted(h)

d = float('inf')
i = 0
while i + k - 1 < n:
    d = min(h[i + k - 1] - h[i], d)
    i += 1

print(d)

Submission status: Accepted

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

H.sort()
ans = sum(H)

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

print(ans)

Submission status: Accepted

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

H.sort()
ANS=10**10
for i in range(K-1,N):
    if ANS>H[i]-H[i-K+1]:
        ANS=H[i]-H[i-K+1]
print(ANS)

Submission status: Accepted

Solution code:
def getN():
    return int(input())

def getMN():
    a = input().split()
    b = [int(i) for i in a]
    return b[0],b[1]

def getlist():
    a = input().split()
    b = [int(i) for i in a]
    return b

N, K  = getMN()

height = []
for i in range(N):
    height.append(getN())

height.sort()
ans = 10000000000
for i in range(N-K+1):

    if ans > abs(height[i] - height[i+K-1]):
        ans = abs(height[i] - height[i+K-1])

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,m = sorted(h),10**10
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 = [int(_) for _ in 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 = float('inf')

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

print(ans)

Submission status: Accepted

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


N, K = [int(elem) for elem in fin().split()]
h_list = [int(fin()) for _ in range(N)]
h_list.sort()
min_dist = float('inf')
for i in range(N - K + 1):
    dist = h_list[i + K - 1] - h_list[i]
    min_dist = min(min_dist, dist)
print(min_dist)

Submission status: Accepted

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

Ans = H[K-1] - H[0]
for i in range(1, 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()
sub = []

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

print(sub[0])

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,K = map(int, raw_input().split())
h = [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 = 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: Runtime Error

Solution code:
def main():
    n, k = Input()
    data = sorted(int(input()) for _ in range(n))
    print(min([data[i+k-1] - data[i] for i in range(n-k+1)]))
main()

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
import numpy as np

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

sH = np.array(sorted(H), dtype=np.int64)
shift = sH[K-1:]
diff = shift - sH[:K]
ans = min(diff)
print(ans)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

trees = []
for _i in range(n):
  trees.append(int(input())

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

for i in range(n - k + 1):
  diff = trees[i + k - 1] - trees[i]
  if ans > diff:
    ans = diff

print(ans)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
import itertools

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

hmin = 10**18

c = itertools.combinations(N,K)

for v in c:
    hmin = min(max(v)-min(v),hmin)
print(hmin)

Submission status: Runtime Error

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

for i in range(N-K+1):
  d = min(d,tree[i+2]-tree[i])
if d>0:
  print(d)
else:
  print(d)

Submission status: Runtime Error

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

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

Submission status: Runtime Error

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_ = h[i + 2] - h[i]
    ans = min(ans, ans_)

print(ans)

Submission status: Runtime Error

Solution code:
n,m=[int(i) for i in input().split()]
l=[]
c=[input() for i in range(n)]
l=sorted(c)
b=l[0:m]
maxs=max(b)-min(b)
for i in range(0,(n-m)):
    b.pop(0)
    b.append(l[i+m])
    f=(max(b)-min(b))
    if(f<maxs):
        maxs=f
print(maxs)

Submission status: Runtime Error

Solution code:
n,m=[int(i) for i in input().split()]
l=[]
c=[input() for i in range(n)]
l=sorted(c)
b=l[0:m]
maxs=max(b)-min(b)
for i in range(0,(n-m)):
    b.pop(0)
    b.append(l[i+m])
    c=(max(b)-min(b))
    if(c<maxs):
        maxs=c
print(maxs)

Submission status: Runtime Error

Solution code:
n,m=[int(i) for i in input().split()]
l=[]
for i in range(n):
    a=int(input())
    l.append(a)
l=sorted(l)
b=l[0:m]
print(b)
max=10**9
for i in range(0,(n-m)):
    b.pop(0)
    b.append(l[i+m])
    c=(max(b)-min(b))
    if(c<max):
        max=c
print(max)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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, (max(a[i:i+k])-min(a[i:i+k])))
print(ans)

Submission status: Runtime Error

Solution code:
,k=map(int,input().split())
a=[]
for x in range(n):
  a.append(int(input()))
ans=1000000009
a=sorted(a)
for y in range(n-k+1):
  ans=min(a[y+k-1]-a[y],ans)
  
print(ans)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
a=[]
for x in range(n):
  a.append(int(input()))
ans=1000000009
sorted(a)
for y in range(n-k):
  ans(a[y+k-1]-a[y],ans)
  
print(ans)

Submission status: Runtime Error

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

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

print(ans)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
N,K=map(int,input().split())
h=[int(input()) for i in range(N)]
h.sort()
a=1000
for i in range(N-K):
  if h[i+K]-h[i]<a:
    a=h[i+k]-h[i]
print(a)

Submission status: Runtime Error

Solution code:
N,K=map(int,input().split())
h=[int(input()) for i in range(N)]
h.sort()
a=1000
for i in range(N-K):
  if h[i+K]-h[i]<a:
    a=h[i+k]-h[i]
ptint(a)

Submission status: Runtime Error

Solution code:
# coding:utf-8
n, k = map(int, input().split())
h = [int(input()) for _ in range(n)]
h.sort()
ans = h[0]

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

Submission status: Runtime Error

Solution code:
N,K = map(int,input().split())
h = [0]*N
hmax = 0
hmin = 0
H = [0]*(N//K+2)
for i in range(N):
  h[i] = int(input())
h.sort()
for i in range(N//K+2):
  hmin = h[i]
  hmax = h[i+K-1]
  H[i] = hmax-hmin
print(min(H))

Submission status: Runtime Error

Solution code:
import collections
n,k = map(int, raw_input().split(' '))
heigths = [int(raw_input()) for _ in range(n)]
heigths.sort()
q = collections.deque([])
r =  float('inf') 
for h in heights:
	q.append(h)
	if len(q) == k+1: q.popleft()
	if k and len(q) == k:
		r =min(r, q[-1] - q[0])
print r if k else 0

Submission status: Runtime Error

Solution code:

import collections
n,k = map(int, raw_input().split(' '))
heigths = [int(raw_input()) for _ in range(n)]
heigths.sort()
q = collections.deque([])
r =  float('inf') 
for h in heights:
	q.append(h)
	if len(q) == k+1: q.popleft()
	if len(q) == k:
r =min(r, q[-1] - q[0])
print r

Submission status: Runtime Error

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

Submission status: Runtime Error

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

inf = 10*9
H = [inf]*inf
for i in range(N):
    H[i] = int(input())

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

print(ans)

Submission status: Runtime Error

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

for i in range(1, K):
    new_h = H[i+K-1]-H[i]
    if new_h < min_h:
        min_h = new_h

print(min_h)

Submission status: Runtime Error

Solution code:
N, K, *h = map(int, open(0).read().split())
h = sorted(h)
ans=float("inf")
for i in range(len(h)-k+1):
	ma = h[i+k-1]
	mi = h[i]
	ans = min(ans,ma-mi)

print(ans)

Submission status: Runtime Error

Solution code:
N, K, *h = map(int, open(0).read().split())
h.sort()
ans=float("inf")
for i in range(len(h)-k+1):
	ma = h[i+k-1]
	mi = h[i]
	ans = min(ans,ma-mi)

print(ans)