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 = []
for i in range(n):
  H.append(int(input()))
H.sort()
m = 10**9
for i in range(0, n-k+1):
  if H[i+k-1] - H[i] < m:
    m = H[i+k-1] - H[i]
print(m)

Solution (Python):
N,K=map(int,input().split())
import numpy as np
H=np.array([int(input()) for _ in range(N)])
H.sort()
print((H[K-1:]-H[:-K+1]).min())

Solution (Python):
N,K=map(int,input().split())
H=[int(input()) for _ in range(N)]
H.sort()
import numpy as np
H=np.array(H)
print((H[K-1:]-H[:-K+1]).min())

Solution (Python):

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

Solution (Python):
n,k=map(int,input().split())
h=[int(input()) for i in range(n)]
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 = [int(input()) for _ in range(n)]
H.sort()
min_ = float('inf')
for i in range(n-k+1):
    d = H[i+k-1] - H[i]
    min_ = min(min_, d)
print(min_)

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

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

mod = 10**9 + 7

ns = lambda: stdin.readline().rstrip()
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))

N, K = na()
h = list()
for _ in range(N):
    h.append(ni())

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

Solution (Python):
def main():
    N,K=map(int,input().split())
    h=[]
    for i in range(N):
        a=int(input())
        h.append(a)
    
    h.sort()
    result=10**10
    count=0
    for i in range(N-K+1):
        count=h[i+K-1]-h[i]
        result=min(result,count)
    print(result)
if __name__=="__main__":
    main()

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

Solution (Python):
import sys
inf = sys.maxsize // 2
n,k = map(int,input().split())
h = []
for i in range(n):
    h.append(int(input()))
h.sort()

ans = 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())
L=[]
ans=float("inf")
for i in range (N):
	L.append(int(input()))
S=sorted(L)
for i in range(N-K+1):
	ans=min(ans,S[i+K-1]-S[i])
print(ans)

Solution (Python):

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

ans = 10 ** 18
h.sort()
for i in range(n - k + 1):
    if ans > h[i + k - 1] - h[i]:
        ans = h[i + k - 1] - h[i]
print(ans)

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

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

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

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

Solution (Python):
n, k, *a = map(int, open(0).read().split())
a.sort()

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

Solution (Python):











import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,sqrt,factorial,hypot,log 
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from fractions import gcd
from random import randint
def ceil(a,b): return (a+b-1)//b
inf=float('inf')
mod = 10**9+7
def pprint(*A): 
    for a in A:     print(*a,sep='\n')
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    N,K=MI()
    H=LIN(N)
    H.sort()
    ans = inf
    for l in range(N):
        r = l+K-1
        if r>=N:
            break
        ans = min(ans, H[r]-H[l])
    print(ans)
if __name__ == '__main__':
    main()

Solution (Python):
from collections import Counter

N, K = list(map(int,input().split()))
H = []
for i in range(N):
    H.append(int(input()))
H = sorted(H,reverse = False)
C = Counter(H)
CH = list(C.values()) 

cnt = 0
for i in range(len(CH)):
    if CH[i] >= K:
        cnt += 1
        break
ans = 999999999999999
for i in range(N-K+1):
    ans = min(ans,H[K-1+i] - H[i]) 

if cnt != 0:
    print('0')
else:
    print(ans)

Solution (Python):
N,K=map(int,input().split())
L=[]
for i in range(N):
	L.append(int(input()))
M=sorted(L)
ans=float("inf")
for i in range(N-K+1):
	ans=min(ans,M[i+K-1]-M[i])
print(ans)

Solution (Python):
def main():
    N, K = (int(i) for i in input().split())
    H = [int(input()) for i in range(N)]
    ans = 10**12
    H.sort()
    for i in range(N-K+1):
        ans = min(ans, H[i+K-1] - H[i])
    print(ans)


if __name__ == '__main__':
    main()

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

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

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

Solution (Python):

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

h.sort()
"""
ans = 0
for i in range(k):
    ans += h[i]

tmp = ans
for i in range(n - k):
    tmp = tmp - h[i] + h[i + k]
    if tmp < ans:
        ans = tmp
print(ans)
"""
ans = 10 ** 18
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
input = sys.stdin.readline
N, K = map(int, input().split())
H = [int(input()) for _ in range(N)]
H.sort()

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

print(ans)

Solution (Python):
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 i in range(N)]
    h.sort()
    ans=INF
    for i in range(N-K+1):
        ans=min(ans,h[i+K-1]-h[i])
    print(ans)

if __name__ == '__main__':
    main()

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

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

mod = 10**9 + 7

ns = lambda: stdin.readline().rstrip()
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))

n, k = na()
h = list()
for _ in range(n):
    h.append(ni())
h.sort()
inf = 10**10
ans = inf
for i in range(n - k + 1):
    ans = min(ans, h[k+i-1]-h[i])
print(ans)

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

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

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

Solution (Python):
n, k, *a = map(int, open(0).read().split())
a.sort()

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

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

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

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

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

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

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

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

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

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

Solution (Python):

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

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

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


print(min(x[:k-1]))

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

Solution (Python):
import sys
import itertools



read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

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

h.sort()

ans = 10**10

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

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

h.sort()

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

print(m)

Solution (Python):
N_hon, K_ko = map(int, input().split())
Height = list(int(input()) for i in range(N_hon))
Height.sort()
mini=float('inf')

for i in range(K_ko - 1, N_hon):
    mini = min(Height[i] - Height[i - (K_ko - 1)], mini)    
    
print(mini)

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

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

Solution (Python):
from collections import deque
n,k = map(int,input().split())
h = sorted([int(input()) for _ in range(n)])
x = deque([])
mn = 10**9
for i in h:
    x.append(i)
    if len(x) >= k:
        mn = min(mn,x[k-1]-x[0])
        x.popleft()
print(mn)

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

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

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

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

res = float("inf")

l = sorted([int(input())for i in range(N)])

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

print(res)

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

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

Solution (Python):
import numpy as np

N, K = map(int, input().split())
h = [int(input()) for _ in range(N)]
h = np.array(sorted(h))
diff_h = np.zeros(N, dtype=np.int32)
diff_h[1:] = np.diff(h)


cum_diff_h = np.cumsum(diff_h)

s = cum_diff_h[K - 1 :] - cum_diff_h[: -K + 1]

print(min(s))

Solution (Python):
n,k = map(int,input().split())
h = []
for i in range(n):
    a = int(input())
    h.append(a)
h.sort()
ans=10**10
for i in range(n-k+1):
    dif = abs(h[i]-h[i+k-1])
    if dif<ans: ans=dif
print(ans)

Solution (Python):
def main():
    N,K=map(int,input().split())
    h=[0 for i in range(0,N)]
    for i in range(0,N):
        h[i]=int(input())
    h.sort()
    ans=10**9
    for i in range(0,N-K+1):
        test=h[i+K-1]-h[i]
        if test<ans:
            ans=test
    print(ans)

if __name__ == '__main__':
    main()

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

Solution (Python):
S = input().split(" ")
n = int(S[0])
k = int(S[1])

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


def calculate(n,k,arr):
    arr.sort()

    drr = []

    for i in range(1,n):
        drr.append(arr[i] - arr[i-1])


    s = 0
    j = 0
    sums = []

    for i in range(len(drr) - (k-1) + 1):

        while j < i + k - 1:
            s = s + drr[j]
            j = j + 1
        sums.append(s)
        s = s - drr[i]


    print(min(sums))

calculate(n,k,arr)

Solution (Python):
S = input()
S = S.split(" ")
n = int(S[0])
k = int(S[1])
arr = []
for i in range(n):
    arr.append(int(input()))


def calculate(n,k,arr):
    arr.sort()
    drr = []
    for i in range(1,n):
        drr.append(arr[i] - arr[i-1])

    j = 0
    s = 0
    sums = []
    for i in range(len(drr) - (k-1) + 1):
        while j < i + (k-1):
            s = s + drr[j]
            j = j + 1
        sums.append(s)
        s = s - drr[i]


    print(min(sums))


calculate(n,k,arr)

Solution (Python):
S = input().split(" ")
n = int(S[0])
k = int(S[1])
arr = []
for i in range(n):
    arr.append(int(input()))


def calculate(n,k,arr):
    arr.sort()
    drr = []
    for i in range(1,n):
        drr.append(arr[i] - arr[i-1])

    j = 0
    s = 0
    sums = []
    for i in range(len(drr) - (k-1) + 1):
        while j < i + (k-1):
            s = s + drr[j]
            j = j + 1

        sums.append(s)
        s = s - drr[i]

    print(min(sums))

calculate(n,k,arr)

Solution (Python):
S = input().split(" ")
n = int(S[0])
k = int(S[1])

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



def calculate(n,k,arr):
    brr = []
    arr.sort()
    for i in range(1,n):
        brr.append(arr[i] - arr[i-1])

    j = 0
    s = 0
    sums = []
    for i in range(0,len(brr) - (k-1) + 1):
        while j < i + (k-1):
            s = s + brr[j]
            j = j + 1
        sums.append(s)
        s = s - brr[i]

    print(min(sums))



calculate(n,k,arr)

Solution (Python):
S = input().split(" ")
n = int(S[0])
k = int(S[1])

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

def calculate(n, k, arr):
    drr = []
    arr.sort()
    for i in range(1,n):
      drr.append(arr[i] - arr[i-1])

    s = 0
    j = 0
    sums = []
    for i in range(len(drr)- (k-1) + 1):
      while j < i + (k-1):
        s = s + drr[j]
        j = j + 1
      sums.append(s)
      s = s - drr[i]
    print(min(sums))

calculate(n, k, arr)

Solution (Python):
S = input().split(" ")
n = int(S[0])
k = int(S[1])
arr = []
for i in range(n):
    arr.append(int(input()))

def calculate(n, k, arr):
    arr.sort()
    drr = []
    for i in range(1, n):
        drr.append(arr[i] - arr[i - 1])

    j = 0
    s = 0
    sums = []
    for i in range(len(drr) - (k - 1) + 1):
        while j < i + (k-1):
            s = s + drr[j]
            j = j + 1
        sums.append(s)
        s = s - drr[i]
    print(min(sums))



calculate(n, k, arr)

Solution (Python):
import itertools
import math
import sys

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

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

trees = [0] * n
for i in range(n):
    trees[i] = int(input())
sorted_trees = sorted(trees)

minh = sorted_trees[0]
maxh = sorted_trees[k-1]
mind = maxh - minh
for i in range(k, n):
    maxh = sorted_trees[i]
    minh = sorted_trees[i-k+1]
    if mind > maxh - minh:
        mind = maxh - minh

print(mind)

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






h.sort()
hd=[]
for i in range(n-k+1):

    hd.append(h[i+k-1]-h[i])
print(min(hd))

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

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

h_sorted = sorted(h)

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

print(ans)

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

h = [int(input()) for _ in range(N)]
h.sort()
min=100000000000000000000
for i in range(0,N-(K-1)):
  if h[i+(K-1)]-h[i]<min:
    min=h[i+(K-1)]-h[i]
print(min)

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

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

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

h.sort()
tree = []

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

print(min(tree))

Solution (Python):
n, k = map(int, input().split())
k -= 1
a = sorted([int(input()) for i in range(n)])
ans_min = 10 ** 10
for i in range(n-k):
    result = a[i + k] - a[i]
    ans_min = result if ans_min > result else ans_min
print(ans_min)

Solution (Python):
import itertools
n,m = map(int,input().split())
l = [int(input()) for i in range(n)]

l.sort()
res = []

res = [l[i+m-1]-l[i] for i in range(n-m+1)]

print(min(res))

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

Solution (Python):
import sys
def input(): return sys.stdin.readline().rstrip()

def main():
    N, K = map(int, input().split())
    H = sorted([int(input()) for _ in range(N)], reverse=True)
    print(min([a - b for a, b in zip(H[:N-K+1], H[K-1:])]))

if __name__ == '__main__':
    main()

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

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

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

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

h.sort()

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

print(ans)

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

Solution (Python):
import sys
input = sys.stdin.readline
n,k = map(int,input().split())
l = sorted([int(input()) for _ in range(n)])
print(min(l[i+k-1]-l[i] for i in range(n-k+1)))

Solution (Python):




n, k = input().split()
n = int(n)
k = int(k)
ls = []
m = {}
for _ in range(n):
    __ = int(input())
    m[__] = m.get(__, 0) + 1
ls = [(0, 0)]
ls.extend(list(m.items()))
ls.sort(key=lambda s: s[0])
for i in range(1, len(ls)):
    ls[i] = (ls[i][0], ls[i-1][1]+ls[i][1])
l, r = 0, 0
ans = 9999999999999999

while True:
    if r < len(ls) and l < len(ls):
        if ls[r][1] - ls[l][1] < k:
            r += 1
        elif ls[r][1] - ls[l][1] >= k:
            
            ans = min(ans, ls[r][0] - ls[l+1][0])
            l += 1
    else:
        break
print(ans)

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

print(min(t))

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

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):
N, K = map(int, input().split())
l = []
for i in range(N):
  h = int(input())
  l.append(h)
l.sort()
m = []
for i in range(N - K + 1):
  d = l[i + K - 1] - l[i]
  m.append(d)
m.sort()
print(m[0])

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

H.sort()
ans = H[N-1]

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

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

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

h.sort()

ans = 10**10

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

print(ans)

Solution (Python):
import numpy as np
n, k = map(int, input().split())
h = [int(input()) for _ in range(n)]
h.sort()
h_max = np.array(h[k-1:])
h_min = np.array(h[:-k+1])
print(min(h_max - h_min))

Solution (Python):
n, k, *a = map(int, open(0).read().split())
a.sort()
for i in range(n-1): a[n-i-1] = abs(a[n-i-1]-a[n-i-2])
a[0] = 0
for i in range(n-1): a[i+1] += a[i]

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

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

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

H.sort()

ans = float('inf')
for i in range(N):
    if (i+K-1 > N-1):
        break
    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.sort()
ans = int(1e10)
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):
n,k =map(int,input().split())
h=[0]*n
for i in range(n):
    h[i]=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)

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

for j in range(0,N-K+1):
  mini=min(mini,h[j+K-1]-h[j])
  
print(mini)

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

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

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

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

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

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

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

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

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

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

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

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

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

print(ans)

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

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

Solution (Python):
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] 
  ans = min(ans, x)
print(ans)

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

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

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

Solution (Python):

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)

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

Solution (Python):

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

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

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

minval = int(1e10)


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