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):
def main():
    N, K = map(int, input().split(" "))
    tree_height = []
    for _ in range(N):
        tree_height.append(int(input()))
    tree_height.sort()
    result = 10 ** 10
    for i in range(N - K + 1):
        result = min(result, tree_height[i + K - 1] - tree_height[i])
    print(result)


if __name__ == '__main__':
    main()

Solution (Python):
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians

from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from functools import reduce
from bisect import bisect_left, insort_left
from heapq import heapify, heappush, heappop

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

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


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

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

    print(ans)


if __name__ == '__main__':
    main()

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

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

print(ans)

Solution (Python):
import sys

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

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

H.sort()

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

print(ans)

Solution (Python):

import math
import heapq
import bisect
import numpy as np


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

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

print(ans)

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

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




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

new_trees = sorted(trees)  

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

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

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

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

n, k = na()
h = [ni() for i in range(n)]
h.sort()
ans = min([h[i + k - 1] - h[i] for i in range(n - k + 1)])
print(ans)

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

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

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

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

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

h.sort()
m = sum(h)

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

Solution (Python):
N, K = map(int, input().split())
hn = []
for i in range(N):
  hn.append(int(input()))
hn = sorted(hn,reverse=True)
min = 10**9+7
for i in range(N-K+1):

  if min > (hn[i]-hn[K+i-1]):
    min = hn[i]-hn[K+i-1]
print(min)

Solution (Python):
import sys

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

    h = sorted(h)

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

    print(min(ave))

main()

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

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

print(min_diff)

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

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

print(min_diff)

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

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

print(min_diff)

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

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

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

Solution (Python):
import sys

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

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

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

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

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

h.sort()

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

print(min(d))

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

if __name__ == '__main__':
    main()

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

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

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

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

print(minv)

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=[int(i) for i in input().split()]
h=sorted([int(input()) for _ in range(N)])

ans=10**9

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

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

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

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

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


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

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

    print(ans)



if __name__ == '__main__':
    main()

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

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

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

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

print(count)

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

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

print(min)

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

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

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

Solution (Python):

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

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

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

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

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

print(ans)

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

ans = 1e15

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

print(ans)

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

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

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

l = [0]
h.sort()


t = max(h)

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

print(t)

Solution (Python):




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

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

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

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

print(ans)

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

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

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

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

def main():
    N,K = map(int,input().split())
    h = [int(input()) for _ in range(N)]
    h.sort()
    
    ans = 10**10
    for i in range(N-K+1):
        ans = min(ans,h[K+i-1]-h[i])
        
    print(ans)
    
if __name__ == "__main__":
    main()

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

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

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

print(min)

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

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

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

Solution (Python):
import os
import sys

import numpy as np

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

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



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

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

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

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

ls.sort(reverse=True)

ans = 10 ** 9

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

print(ans)

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

h.sort()

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

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

H.sort()

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

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

Solution (Python):

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

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

print(ans)

Solution (Python):


N,K=map(int,input().split())
H=[0]*N
for i in range(N):
    H[i]=int(input())
H.sort()
ans=float('inf')
for i in range(N-K+1):
    ans=min(ans,H[i+K-1]-H[i])
print(ans)

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

h.sort()
mindh = 10**9

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

ans = mindh
print(ans)

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

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

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

ans = mindh
print(ans)

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


if __name__ == '__main__':
    solve()

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

print(ans)

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

Solution (Python):

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

trees = []

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


trees.sort()

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

print(ans)

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

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

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

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

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

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

print(ans)

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

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

input = sys.stdin.readline

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



l = sorted(l)



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

print(ansmin)

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


main()

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

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

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

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

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

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

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

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

main()

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

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

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

print(ans)

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

Solution (Python):
N,K = map(int,input().split())
treelist = []
difflist = []
append = treelist.append

append_diff = difflist.append
for i in range(N):
    tree = int(input())
    
    append(tree)
treelist.sort()

for i in range(K-1,N):
    diff = treelist[i] - treelist[i-K+1]
    
    append_diff(diff)
print(min(difflist))

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

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

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 = list(map(int, input().split()))


h_list = [0]*100000
i = 0
for i in range(N):
    x = int(input())
    h_list[i] = x
    x = 0
j = 0
min = 1000000000
for i in range(100000 - N):
    h_list.remove(0)

h_list.sort()

y = 0
q = 0
while q < N-K+1:
    y = h_list[q + K-1] - h_list[q]


    if y < min:
        min = y
        y = 0
    else:
        y = 0
    q += 1
print(min)

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

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

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

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

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


if __name__ == '__main__':
    main()

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

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

an.sort()
dn = [-1 for _ in range(n - k + 1)]
for i in range(n - k + 1):
  dn[i] = an[i+k-1] - an[i]

print(min(dn))

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

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

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

Solution (Python):
import sys
from collections import deque
input = sys.stdin.readline
import numpy as np
N, K = [int(x) for x in input().split()]
h = []
for _ in range(N):
    h.append(int(input()))
h.sort()
i = 0
ans = []
while N - i >= K:
    temp = []
    temp.append(h[i])
    temp.append(h[i+K-1])
    ans.append(temp[-1] - temp[0])
    i += 1
print(min(ans))

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())
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):
def main(n: int, k: int, h: list):
    h.sort()

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


if __name__ == "__main__":
    n, k = map(int, input().split())
    h = [int(input()) for _ in range(n)]

    main(n, k, h)

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

H.sort()

for i in range(0, N - (K-1)):
    min_K = H[i]
    max_K = H[i + K - 1]

    if i == 0:
        min_diff = max_K - min_K
    else:
        min_diff = min(min_diff, max_K - min_K)

print(min_diff)

Solution (Python):
import sys
input=sys.stdin.readline
n,k=map(int,input().split())
a=[int(input()) for i in range(n)]
a.sort()
res=float("INF")
for i in range(0,n-k+1):
    b=a[i+k-1]-a[i]
    if res>b:
        res=b
print(res)

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

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

input_list.sort()

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

print(ans)

Solution (Python):
import sys
def input():
  return sys.stdin.readline().rstrip()
n, k = [int(i) for i in 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 _ in range(n)]
ans=10**10
h.sort()
for i in range(n):
	if i+k-1 <n:
		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()
ans = 10**10
for i in range(n-k+1):
  if ans >= h[i+k-1] - h[i]:
    ans = h[i+k-1] - h[i]
    if ans == 0:
      break
print(ans)

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

h.sort()
d_list = []
for i in range(N-K+1):
  d_list.append(h[i+K-1] - h[i])
  
print(min(d_list))

Solution (Python):
import sys
sys.setrecursionlimit(10**6)
if sys.platform in (['ios','darwin','win32']):
	sys.stdin=open('Untitled.txt')
input = sys.stdin.readline
def INT(): return int(input())
def MAP(): return [int(s) for s in input().split()]

N, K = MAP()
A = [INT() for _ in range(N)]

A.sort()

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

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=[int(input()) for _ in range(N)]
LS = sorted(L)
import sys
ANS = sys.maxsize

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

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

Solution (Python):

import sys

n,k = map(int, input().split())
H = sorted(map(int, sys.stdin.readlines()))
ans = 10**9
for i in range(n-k+1):
    dif = H[i+k-1] - H[i]
    if dif == 0:
        print(0)
        break
    elif dif < ans:
        ans = dif
else:
    print(ans)

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

    S.sort(reverse=True)

    diff_arr = [S[i] - S[i+K-1] for i in range(len(S)-K+1)]
    
    
    
    
    

    print(min(diff_arr))

if __name__ == "__main__":
    main()

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

    S.sort(reverse=True)

    diff_arr = [S[i] - S[i+K-1] for i in range(len(S)-K+1)]
    print(min(diff_arr))

if __name__ == "__main__":
    main()

Solution (Python):

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

H.sort()

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

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

s=[0]*n
for i in range(1,n):
    s[i]=s[i-1]+(h[i]-h[i-1])
ans=float("inf")
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 = [int(input()) for i in range(n)]
h.sort()
d = h[n-1]-h[0]
for i in range(n-k+1):
    if h[i+k-1] - h[i] < d:
        d = h[i+k-1] - h[i]
print(d)

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

h = [input() for i in range(N)]
H = [int(s) for s in h]
H.sort()
d = 10000000000
for i in range(N-K+1):
    if d > (H[i+K-1] - H[i]):
        d = H[i+K-1] - H[i]

print(d)

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

for i in range(r,n-k+1):
    if n - r < k: 
        break
    h_max = h[r+k-1]
    
    h_min = h[r]
    ans.append(h_max - h_min)
    r += 1
print(min(ans))

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

h.sort()
m=abs(h[0]-h[K-1])
for i in range(1,N-K+1):
    if m>abs(h[i]-h[i+K-1]):
        m=abs(h[i]-h[i+K-1])
print(m)

Solution (Python):
import sys

N,K=(int(x) for x in input().split())

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

minimum=10**10
tmp=N-K
for i in range(N-K+1):
	
	if h[i+K-1]-h[i] < minimum:
		minimum=h[i+K-1]-h[i]

print(minimum)

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

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

h.sort()

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

print(min(length))

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

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

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

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

h_list = [int(input()) for _ in range(N)]
h_list.sort(reverse=True)

minmax = 10e10
for i in range(N - K + 1):
    minmax = min(minmax, h_list[i] - h_list[i + K - 1])

print(minmax)

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

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

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