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).

Solution (Python):


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

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

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

H.sort()

ans = 1000000009

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

print(ans)

Solution (Python):

import sys
input=sys.stdin.readline
import numpy as np

def main():
    numbers=[]
    n,m = map(int,input().split())
    [numbers.append(int(input())) for _ in range(n)]
    n_numbers=np.array(numbers,dtype=int)
    n_numbers.sort()
    print(min(n_numbers[i+m-1] - n_numbers[i] for i in range(n-m+1)))

if __name__=="__main__":
    main()

Solution (Python):

import sys
input=sys.stdin.readline

def ngram(numbers,n):
    return list([numbers[i:i+n] for i in range(len(numbers)-2)])

def main():
    numbers=[]
    n,m = map(int,input().split())
    [numbers.append(int(input())) for _ in range(n)]
    numbers.sort()
    print(min(numbers[i+m-1] - numbers[i] for i in range(n-m+1)))

if __name__=="__main__":
    main()

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

ans = float("inf")
for i in range(n-k+1):
    sa = h[i+k-1] - h[i]
    if ans > sa:
        ans = sa
        
print(ans)

Solution (Python):
N,K=map(int,input().split())
L=[]
for i in range(N):
    L.append(int(input().rstrip()))
L.sort()
M=[]
for i in range(N-K+1):
    M.append(L[K+i-1]-L[i])
M.sort()
print(M[0])

Solution (Python):
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)

Solution (Python):
N,K = map(int,input().split())
high = [int(input()) for _ in range(N)]
s_high = sorted(high)
ans = 10**9

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

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

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

Solution (Python):
def main():
    N, K = map(int, input().split())
    h = []
    for _ in range(N):
        h.append(int(input()))
    h.sort()
    ans = pow(10, 10)
    for k in range(N-K+1):
        ans = min(ans, abs(h[k]-h[K-1+k]))
    print(ans)

if __name__ == "__main__":
    main()

Solution (Python):



def main():
    N, K = map(int, input().split())
    H = sorted([int(input()) for _ in range(N)])
    ans = min([H[i + K - 1] - H[i] for i in range(N - K + 1)])

    print(ans)

if __name__ == "__main__":
    main()

Solution (Python):
n,k = map(int,input().split())
h = [0]*n
for i in range(n):
  h[i]=int(input())
h = sorted(h)
answer = 100000000000000
for j in range(n+1-k):
  answer = min(answer,h[j-1+k]-h[j])
print(answer)

Solution (Python):
import math
import collections
from sys import exit
from itertools import product

ii = lambda : int(input())
mi = lambda : map(int,input().split())
li = lambda : list(map(int,input().split()))

n, k = mi()
h = [ii() for i in range(n)]
h.sort()
ans = 10**12
for i in range(n):
    last = i + k - 1
    if last >= n:
        break
    ans = min(ans, h[last]-h[i])
print(ans)

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

Solution (Python):
n, k = map(int,input().split())
h_l = sorted([ int(input()) for _ in range(n)  ])
ans = float('inf')
for i in range(n-k+1):
    ans = min(h_l[i+k-1]-h_l[i],ans)
print(ans)

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

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

h.sort()

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

print(min(l))

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

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

print(result)

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

Solution (Python):
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))

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

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

H.sort()

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

print(min(sa))

Solution (Python):
n,k = map(int,input().split())
L = sorted(list(int(input()) for _ in range(n)))
m = 10**9

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

print(m)

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

    h.sort()

    temp = []
    for i in range(n-m+1):
        temp.append(h[i+m-1]-h[i])

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


if __name__ == '__main__':
    main()

Solution (Python):
from sys import stdin
from collections import defaultdict
input = stdin.readline

T = 1
for t in range(1,T + 1):
	n,k = map(int,input().split())
	_input = []
	for i in range(n):
		_input.append(int(input()))
	_input.sort()
	min_ans = 10000000000
	for i in range(k - 1,len(_input)):
		min_ans = min(min_ans,_input[i] - _input[i - k + 1])
	print(min_ans)

Solution (Python):
def main():
    n, k = map(int, input().split(" "))
    data = sorted(int(input()) for _ in range(n))
    print(min([data[i+k-1] - data[i] for i in range(n-k+1)]))
main()

Solution (Python):








def solve(N, K, h):
    h.sort()
    ans = 10 ** 9
    for i in range(K - 1, N):
        ans = min(ans, h[i] - h[i - (K - 1)])
    print(ans)


if __name__ == '__main__':
    N, K = map(int, input().split())
    h = [int(input()) for _ in range(N)]
    solve(N, K, h)

Solution (Python):
import math
from math import gcd,pi,sqrt
INF = float("inf")

import sys
sys.setrecursionlimit(10**6)
import itertools
from collections import Counter,deque
def i_input(): return int(input())
def i_map(): return map(int, input().split())
def i_list(): return list(i_map())
def i_row(N): return [i_input() for _ in range(N)]
def i_row_list(N): return [i_list() for _ in range(N)]
def s_input(): return input()
def s_map(): return input().split()
def s_list(): return list(s_map())
def s_row(N): return [s_input for _ in range(N)]
def s_row_str(N): return [s_list() for _ in range(N)]
def s_row_list(N): return [list(s_input()) for _ in range(N)]


def main():
    n, k = i_map()
    h = [i_input() for i in range(n)]
    h.sort()
    print(min(h[i+k-1] - h[i] for i in range(n-k+1)))


if __name__=="__main__":
    main()

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

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

h.sort()

ans = 1000000000

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

print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

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

Solution (Python):
import bisect,collections,copy,itertools,math,string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())
def main():
    n, k = LI()
    h = [I() for _ in range(n)]
    h.sort()
    ans = float("inf")

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

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

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

Solution (Python):
from sys import stdin
input = stdin.buffer.readline

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

print(ans)

Solution (Python):
n,k=map(int,input().split())
h=list(int(input()) for i in range(n))
h.sort()

c=[]

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

print(min(c))

Solution (Python):

"""
Created on Sun Sep 13 03:08:56 2020

@author: liang
"""

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

Solution (Python):
import sys
input = sys.stdin.readline

n,k = map(int, input().split())
h_list = []

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

sort_h_list = sorted(h_list)

L_index = 0
R_index = 0
L_value = sort_h_list[0]
R_value = sort_h_list[0]
count = 0
ans = 10**9


while (R_index < n):
    if (count < k):
        count += 1
        R_value = sort_h_list[R_index]
        if count == k:
            ans = min(ans,R_value - L_value)
        R_index += 1

    else:
        count -= 1
        L_index += 1
        L_value = sort_h_list[L_index]

print(ans)

Solution (Python):
import sys



def input():
    return sys.stdin.readline().rstrip()


def main():
    N, K = map(int, input().split())


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

    H.sort()
    mm = 10**9+7
    for i in range(N-K+1):
        mm=min(mm,H[i+K-1] -H[i] )

    print(mm)


if __name__ == "__main__":
    main()

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

Solution (Python):
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[:len(shift)]
ans = min(diff)
print(ans)

Solution (Python):
import sys



def input():
    return sys.stdin.readline().strip()


def input_int():
    return int(input())


def input_int_list():
    return [int(i) for i in input().split()]


def main():

    n, k = input_int_list()
    H = []
    for _ in range(n):
        H.append(input_int())
    H = sorted(H)
    ans = float("inf")
    for i in range(n - k + 1):
        ans = min(ans, H[i + k - 1] - H[i])
    print(ans)

    return


if __name__ == "__main__":
    main()

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

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

ls = []

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

print(min(ls))

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

sab = sorted(ab)

res = 10**10

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

print(res)

Solution (Python):
import sys
input = sys.stdin.readline

def main():
    N, K = map(int, input().split())
    H = sorted([int(input()) for _ in range(N)])
    ans = 10 ** 10
    for i in range(N - K + 1):
        if H[i + K - 1] - H[i] < ans:
            ans = H[i + K - 1] - H[i]
    print(ans)


if __name__ == '__main__':
    main()

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

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

print(result)

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

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

Solution (Python):




def resolve():
    import sys
    input = sys.stdin.readline

    n, k = [int(x) for x in input().rstrip().split(" ")]

    h_list = [int(input().rstrip()) for _ in range(n)]
    h_list.sort()
    ans = 1000000000
    for i in range(n - k + 1):

        val = h_list[i+k-1] - h_list[i]
        if val < ans:
            ans = val
    print(ans)


if __name__ == "__main__":
    resolve()

Solution (Python):
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))

Solution (Python):
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))

Solution (Python):
N, K = map(int, input().split())
L = []
mini = float('inf')

for i in range(N):
    h = int(input())
    L.append(h)

L.sort(reverse=True)

for i in range(len(L)-K+1):
    if L[i] - L[i+K-1] < mini:
        if L[i] - L[i+K-1] == 0:
            print(0)
            exit()
        else:
            mini = L[i] - L[i+K-1]

print(mini)

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

hl.sort()
ans = 10000000001

for j in range(n-k+1):
    s = hl[j+k-1] - hl[j]
    if s < ans:
        ans = s
    else:
        pass
print(ans)

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

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

print(min(H))

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

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

print(m)

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

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

Solution (Python):
import sys

def solve():
    N, K = [int(i) for i in input().split()]
    H = []
    for _ in range(N):
        H.append(int(input()))
    H.sort()
    ans = sys.maxsize
    for i in range(N - K + 1):
        ans = min(ans, H[i + K - 1] - H[i])
    print(ans)

if __name__ == "__main__":
    solve()

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

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

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)

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

Solution (Python):
n, k = map(int, input().split())
lis = []
ans = 1000000000000000000

for _ in range(n):
  lis.append(int(input()))

lis.sort()

for i in range(n):
  if i+k-1 >= len(lis):
    break
  else:
    mi = abs(lis[i] - lis[i+k-1])
    ans = min(ans, mi)

print(ans)

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

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

Solution (Python):
def main():
    n, k = map(int, input().split())
    h_list = [int(input()) for _ in range(n)]
    h_list.sort(reverse=True)

    min_diff = 10 ** 9

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

    print(min_diff)


if __name__ == "__main__":
    main()

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

Solution (Python):
n,k=map(int,input().split())
listh=[]
for i in range(n):
  listh.append(int(input()))
listh.sort()
ans=10**9
for i in range(n-k+1):
  high=listh[i+k-1]-listh[i]
  if high<ans:
    ans=high

print(ans)

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

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

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

a.sort()
ans=100000000000000000000000

for j in range(N-K+1):
    if a[j+K-1]-a[j]<ans:
        ans=a[j+K-1]-a[j]


print(ans)

Solution (Python):
N, K = map(int, input().split())
h = []
ans = 1000000000

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

h = sorted(h)

for j in range(N-K+1):
    if ans > h[j+K-1]-h[j]:
        ans = h[j+K-1]-h[j]
print(ans)

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

hs = sorted(hs)

diff = 10**12
for i in range(n-k+1):
    diff = min(diff, hs[i+k-1] - hs[i])

print(diff)

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

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

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

print(min(list1))

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

ans=[]
for i in range(N-K+1):
    ans.append(A[i+K-1]-A[i])

print(min(ans))

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

Solution (Python):
N, K = map(int,input().split())
H = []
min_dif = 1000000000000000000000000

for n in range(N):
  h = int(input())
  H.append(h)

H.sort()

for i in range(N-K+1):
  dif = H[i+K-1] - H[i]
  if dif < min_dif:
    min_dif = dif
    
print(min_dif)

Solution (Python):

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

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

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

Solution (Python):
n, k = map(int, input().split( ))
arr = []
for _ in range(n):
  arr.append(int(input()))
a = sorted(arr)
m = 10**9
for i in range(n-k+1):
  ma = a[i+k-1] - a[i]
  m = min(m, ma)
print(m)

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

print(ans)

Solution (Python):
N,K = map(int,input().split())
H = sorted([int(input()) for n in range(N)])
ans = H[-1]-H[0]

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

Solution (Python):
N,K=map(int,input().split())
H=[]
for _ in range(N):
    H.append(int(input()))
H.sort()
min = 1e12
for i in range(N-(K-1)):
    diff = H[i+K-1] - H[i]
    if diff < min:
        min = diff

print(min)

Solution (Python):
N, K = map(int,input().split())
h = [int(input()) for i in range(N)]
s_h = sorted(h)
tmp = []
ans1 = 0
ans2 = 0
ans3 = 0
for i in range(N-K+1):
    ans1 = s_h[i+K-1]
    ans2 = s_h[i]
    ans3 = ans1 - ans2
    tmp.append(ans3)

print(min(tmp))

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

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

Solution (Python):
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+K-1]-tree[i])
print(d)

Solution (Python):
S,K=list(map(int, input().split()))
H=[]
for _ in range(S):
  H.append(int(input()))
H.sort()
INF=float('inf')
a=[INF]*(S-K+1)
for i in range(S-K+1):
  a[i]=H[i+K-1]-H[i]
print(min(a))

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

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

h.sort()

dh = [int(1e9)]*n
for i in range(k-1,n):
    dh[i]=h[i]-h[i-k+1]
    
print(min(dh))

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

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

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

min_h = float('inf')
for i in range(N-K+1):
    temp = H[i] - H[i+K-1]
    min_h = min(min_h, temp)


print(min_h)

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

Solution (Python):

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

diff = 10**9
for i in range(N-K+1):
    a = h[i]-h[i+K-1]
    if diff > a:
        diff = a
    if diff == 0:
        break
print(diff)

Solution (Python):
n,k=map(int,input().split())
h=[]
ma=10**9
for i in range(n):
  a=int(input())
  h.append(a)
h.sort()

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

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

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

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

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

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

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

Solution (Python):
N, K = map(int, input().split())
A = [int(input()) for _ in range(N)]
B = sorted(A)
ans = []
flag = 0
for i in range(N-K+1):
  ans.append(B[i+K-1]-B[i])
  flag = 1
print(min(ans) if flag == 1 else "0")

Solution (Python):
N,K = map(int,input().split())
h = [int(input()) for i in range(N)]
h.sort(reverse = True)
minimum = 10**9
for i in range(N-K+1):
    minimum = min(minimum,h[i] - h[i+K-1])
print(minimum)

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

Solution (Python):
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    N, K, *H = map(int, read().split())

    H.sort()

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


if __name__ == '__main__':
    main()

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

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

h = sorted(h)
ans = 10**9
tmp = 0

for i in range(k-1, n):
    tmp = h[i] - h[i-(k-1)]
    if tmp < ans:
        ans = tmp

print(ans)

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

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

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

h.sort()

s = h[-1] - h[1]

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

print(s)

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

nums = [0]*(n-1)
for i in range(n-1): nums[i] = h[i+1]-h[i]

ans = sum(nums[:k-1])
t = sum(nums[:k-1])
for i in range(k-1,n-1):
  t += nums[i] - nums[i-k+1]
  if t < ans: ans = t
print(ans)

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

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

Solution (Python):
N, K = map(int,input().split())
H = []
for i in range(N):
    h = int(input())
    H.append(h)
A = []
H = sorted(H)
for i in range(N - K + 1):
    x = H[i + K - 1] - H[i]
    A.append(x)

A = sorted(A)
ans = A[0]
print(ans)

Solution (Python):
N, K = list(map(lambda k: int(k), input().split(" ")))
H = [int(input()) for i in range(N)]
H.sort()

print(min([H[i + K - 1] - H[i] for i in range(len(H) - K + 1)]))

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

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

print(min(l))

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

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

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

print(ans)

Solution (Python):

n,k=map(int,input().strip().split(" "))
ar=[]
for i in range(n):
    ar.append(int(input()))
ar.sort()
m=999999999999999

for i in range(n-k+1):

    c=ar[i+k-1]-ar[i]
    if c<m:
        m=c
print(m)

Solution (Python):
N, K = map(int, input().split())
h = [int(input()) for _ in range(N)]
h.sort()
ans = max(h)
for i in range(N-K+1):
    if h[i+K-1]-h[i] < ans:
        ans = h[i+K-1]-h[i]
print(ans)

Solution (Python):
import numpy as np

N,K,*H = map(int,open(0).read().split())
H =np.array(sorted(H))

H = np.diff(H,n=1)
minH = 1e10

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

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

print(ans)

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

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

Solution (Python):
import sys


inint = lambda: int(sys.stdin.readline())
inintm = lambda: map(int, sys.stdin.readline().split())
inintl = lambda: list(inintm())
instrm = lambda: map(str, sys.stdin.readline().split())
instrl = lambda: list(instrm())

n, k = inintm()

h = []
ans = 10**10

for _ in range(n):
    h.append(inint())

h.sort()

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

print(ans)

Solution (Python):
import sys
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
read = sys.stdin.buffer.read
sys.setrecursionlimit(10 ** 7)
INF = float('inf')

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

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

print(ans)

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

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

hmax = 0
hmin = 0
ans = 10**10
for i in range(N - K + 1):
    hmax = h[i + K - 1]
    hmin = h[i]
    if hmax - hmin < ans:
        ans = hmax - hmin
print(ans)

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

hmax = 0
hmin = 0
ans = 10**10
for i in range(N - K + 1):
    hmax = h[i + K - 1]
    hmin = h[i]
    if hmax - hmin < ans:
        ans = hmax - hmin
print(ans)

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

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

print(min(salist))

Solution (Python):
n,k = map(int,input().split())
l = []
for _ in range(n):
    a = int(input())
    l.append(a)

ans = 10**9
l.sort()

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

print(ans)

Solution (Python):
n, k = map(int, input().split())
h = sorted(int(input()) for _ in range(n))
print(min(r - l for l, r in zip(h[: 1 - k], h[k - 1 :])))

Solution (Python):
import sys

input = sys.stdin.readline
n, k = map(int, input().split())
h = sorted(int(input()) for _ in range(n))
print(min(r - l for l, r in zip(h[: 1 - k], h[k - 1 :])))

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

Solution (Python):
if __name__ == '__main__':

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

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

	B = sorted(A,reverse=True)

	ans = 10**9
	for j in range(n-k+1):
		tmp = B[j] - B[j+k-1]
		ans = min(ans,tmp)
	print(ans)

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

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

Solution (Python):



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

TREES=[]

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


TREES.sort()


diff=[0]
for i in range (1,N):
    if i <K:
        d=abs(TREES[i]-TREES[0])
    else:
        d=abs(TREES[i]-TREES[i-K+1])
    diff.append(d)


print(min(diff[K-1:]))