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):
if __name__ == "__main__":
    N, K = map(int, input().split())
    h = list()
    for i in range(N):
        h.append(int(input()))

    h = sorted(h)

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

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

Solution (Python):
n,k = map(int,input().split())
h = [int(input()) for i in range(n)]
h.sort()
m = 10**9
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 = [0] * n
ans = []
for i in range(n):
  h[i] = int(input())
h.sort()
for i in range(n-k+1):
  ans.append(h[i+k-1] - h[i])
print(min(ans))

Solution (Python):
a,b = [int(i) for i in input().split()]

l = sorted([int(input()) for _ in range(a)])

m = [l[i+b-1] - l[i] for i in range(len(l)-b+1)]

print(min(m))

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

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

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

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

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

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


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


sh = sorted(h)

min_ = 10**10
for i in range(n-k+1):
    min_ = min(min_,sh[i+k-1]-sh[i])

print(min_)

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

Solution (Python):
import sys
input = sys.stdin.readline
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]
print(ans)

Solution (Python):

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

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

h.sort()

result = h[-1] -h[0]
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 = sorted(H)
A=[0]*(N-K+1)
ans=0
for i in range(N-K+1):
    A[i] =h[i+K-1]-h[i]
ans=min(A)
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 = map(int, input().split())
h = sorted([int(input()) for _ in range(N)])[::-1]

diff = 10 ** 10
for i in range(N):
    if i + K - 1 <= N - 1:
        diff = min(diff, h[i] - h[i+K-1])
    else:
        break

print(diff)

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

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

list.sort(h_list)

ans = 1000000000
for i in range(0, N-K+1):
    if h_list[i+K-1] - h_list[i] < ans:
        ans = h_list[i+K-1] - h_list[i]
        
print(ans)

Solution (Python):
a=list(map(int,input().split()))
n=[]
for i in range(a[0]):
  n.append( int(input()) )
n.sort()
  
c = [x - y for (x, y) in zip(n[a[1]-1:] , n[:-a[1]+1])]
print(min(c))

Solution (Python):
import numpy as np


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

h.sort()
hdiff = [h[i + 1] - h[i] for i in range(len(h) - 1)]
hdiff = np.array(hdiff)
ans = np.sum(hdiff[0:0+K-1])

sum_ = np.sum(hdiff[0:0+K-1])
for i in range(1, N-K+1):
    
    
    sum_ = sum_-hdiff[i-1]+hdiff[i+K-2]
    ans = min(ans, sum_)


print(ans)

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

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

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

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

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

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

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

Solution (Python):
def MAP(): return list(map(int,input().split()))
def INT(): return int(input())
def FLOAT(): return float(input())
MOD = 10**9+7

n,k = MAP()
h = [INT() 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())
h = [int(input()) for _ in range(N)]
h.sort()
res = 10 ** 9
for i in range(N - K + 1):
  hmin, hmax = h[i], h[i + K - 1]
  res = min(res, hmax - hmin)
print(res)

Solution (Python):

import sys
input = sys.stdin.readline
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7



N, K = map(int, input().split())
H = [ int(input()) for _ 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)

Solution (Python):
n=[int(i) for i in input().split()]
array=[]
for i in range(n[0]):
  array.append(int(input()))
array.sort()
ans=abs(array[0]-array[n[1]-1])
for i in range(n[0]-(n[1]-1)):
  if(ans>abs(array[i]-array[i+n[1]-1])):
    ans=abs(array[i]-array[i+n[1]-1])
print(ans)

Solution (Python):
def christmasEve2():
    N, K = map(int, input().split())
    strees = sorted([int(input()) for _ in range(N)])
    print(min(sorted([y - x for x, y in zip(strees, strees[K-1:])])))


if __name__ == "__main__":
    christmasEve2()

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

if __name__ == "__main__":
    christmasEve()

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

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):
    
    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 = 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()
Max = []
Min = []
ans = 999999999999999
for i in range(N-K+1): 
    Max.append(i+K-1) 
    Min.append(i)     
for i in range(N-K+1):
    ans = min(ans,h[Max[i]] - h[Min[i]])
print(ans)

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

for i in range(N):
    b=int(input())
    a.append(b)

a=sorted(a)

b=[]
for j in range(N-K+1):
    c=a[j+K-1]-a[j]
    b.append(c)    
        
print(min(b))

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

height.sort()

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

print(ans)

Solution (Python):
if __name__ == '__main__':
    N, K = map(int, input().split())
    H = [int(input()) for _ in range(N)]
    H.sort()
    dis_min = 10**9
    for i in range(N-K+1):
        dis_min = min(dis_min, H[i+K-1]-H[i])
        
    print(dis_min)

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

Solution (Python):
n, k = [int(x) for x in 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 _ in range(n)],reverse=True)
mn = max(h)
for i in range(n-k+1):
    mn1 = h[i] - h[i+k-1]
    if mn1 < mn:
        mn = mn1
print(mn)

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

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

print(d)

Solution (Python):
import sys
readline = sys.stdin.readline
N, K = list(map(int, readline().split()))
h = []
for _ in range(N):
    h.append(int(readline()))

h.sort()

L = 0
resp = 10**9 + 1
while L + K <= N:
    b = h[L + K - 1] - h[L]
    if resp > b:
        resp = b
    
    L += 1

print(resp)

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

print(minv)

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

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

q=sorted(h)
ansList=[]
for c,d in zip(q,q[k-1:]):
  ansList.append(d-c)

print(min(ansList))

Solution (Python):
N, K = map(int, input().split())
H = [int(input()) for _ in range(N)]
H = sorted(H)
ans = 1e9
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 = 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 = list(sorted([int(input()) for _ in range(N)]))

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

print(res)

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

diff = [h - H[0] for h in H]

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

print(res)

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

for _ in range(N):
    tree_h=int(input())
    trees.append(tree_h)

trees.sort()
trees_dif=[trees[i+K-1]-trees[i] for i in range(N-K+1)]
print(min(trees_dif))

Solution (Python):
N, K = list(map(int, input().split()))
H = [int(input()) for _ 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()
m = 1e10
for i in range(N - K + 1):
    d = H[i + K - 1] - H[i]
    if d < m:
        m = d
print(m)

Solution (Python):
def main():
    N, K = (int(i) for i in input().split())
    h = [int(input()) for i in range(N)]
    h.sort()
    ans = 10**9 + 7
    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)])
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)]

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

print(ans)

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

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

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

print(INF)

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

print(result)

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

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

print(ans)

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

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

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

print(ans)

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

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

ans = float('inf')
h1 = sorted(h)

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

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

h = list(int(input()) for _ in range(N))
sh = sorted(h, reverse=True)
ans = sh[0] - sh[K - 1]
for i in range(1, N - K + 1):
    if N < (i + K):
        break
    ans = min(ans, sh[i] - sh[i + K - 1])

print(ans)

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

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

highs = [int(input()) for i in range(n)]
highs = sorted(highs)

minimum = min([highs[i + k - 1] - highs[i] for i in range(n - k + 1)])

print(minimum)

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

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

Solution (Python):
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate
import sys



def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LIM(): return list(map(lambda x:int(x) - 1, sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline()
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def LIRM(n): return [LIM() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
mod = 1000000007



n, k = LI()
trees = sorted(IR(n))
ret = float('inf')
for i in range(n - k + 1):
    ret = min(ret, trees[i + k - 1] - trees[i])



print(ret)

Solution (Python):

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

h = sorted(h)

nearest = 1000000000
for i in range(N-K+1):
    if nearest > h[i+K-1] - h[i]:
        nearest = h[i+K-1] - h[i]

print(nearest)

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

Solution (Python):

N, K = map(int, input().split())
h = [int(input()) for i in range(N)]
H = list(h)
H.sort(reverse=True)
ans = H[0]-H[N-1]
tmp = 0
for i in range(len(H)-K+1):
    tmp = H[i]-H[i+K-1]
    if ans >= tmp:
        ans = tmp
    else:
        pass
print(ans)

Solution (Python):
N, K = map(int, input().split())
lst = []
for i in range(N):
    lst.append(int(input()))
lst.sort()
dif = 10**9
for i in range(N-K+1):
    if lst[i+K-1] - lst[i] < dif:
        dif = lst[i+K-1] - lst[i]
print(dif)

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

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

Solution (Python):

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

ans = 10**10
H.sort()

for i in range(N-K+1):
    t = H[i+K-1] - H[i]
    ans = min(ans,t)
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 = map(int, input().split())
H = [int(input()) for i in range(N)]

H.sort()
ans = 10**9
for i in range(N-K+1):
    if (H[i+K-1]-H[i] < ans):
        ans = H[i+K-1]-H[i]

print(ans)

Solution (Python):
from sys import stdin
import itertools






temp = stdin.readline().split()
N = int(temp[0])
X = int(temp[1])

data = []
for i in range(N):
    data.append(int(stdin.readline()))
    
d = sorted(data)

mindif = 1000000000000000
for i in range(N-X+1):
    dif = d[i+X-1] - d[i]
    if dif < mindif:
        mindif = dif
        
print(mindif)

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

Solution (Python):
def main():
    import sys
    input = sys.stdin.readline
    N, K = map(int, input().split())
    H = [int(input()) for i in range(N)]
    H = sorted(H)
    ans = float("inf")
    for i in range(N-K+1):
        tmp = H[i+K-1] - H[i]
        if tmp < ans:
            ans = tmp
    print(ans)
    

main()

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

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

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

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

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

print(diff)

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

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

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

print(min(diff))

Solution (Python):
N,A= [int(i) for i in input().split()]
n = [int(input()) for i in range(N)]
n.sort()
mn = 10**9
for i in range(N-A+1):
	m = n[i+A-1] - n[i]	
	if m == 0:
		print(0)
		exit()
	elif mn > m: mn=m 
print(mn)

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())
h = list(int(input()) for i in range(n))
h.sort()
ans = max(h)
i = 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):
def christmas_eve(n,k,h):
    _h = sorted(h)
    opt = 0
    ans = 10**9 + 1
    _k = k - 1
    loop = n - _k
    for i in range(loop):
        opt = _h[i+_k] - _h[i]
        if ans >= opt:
            ans = opt

    return ans
if __name__ == '__main__':
    n,k = map(int,raw_input().split())
    h = [int(raw_input()) for _ in range(n)]

    print christmas_eve(n,k,h)

Solution (Python):
import math
import copy
import sys
import fractions
import numpy as np
from functools import reduce



def getInputInt():
    inputNum = int(input())
    return inputNum

def getInputListInt():
    outputoData = []
    inputData = input().split()
    outputData = [int(n) for n in inputData]

    return outputData

def getSomeInputInt(n):
    outputDataList = []
    for i in range(n):
        inputData = int(input())
        outputDataList.append(inputData)

    return outputDataList

def getSomeInputListInt(n):
    inputDataList = []
    outputDataList = []
    for i in range(n):
        inputData = input().split()
        inputDataList = [int(n) for n in inputData]
        outputDataList.append(inputDataList)

    return outputDataList



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

data.sort()


ans = sys.maxsize

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

print(ans)

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

  if d<ans:
    ans=d
  


print(ans)

Solution (Python):
import sys
from collections import deque
stdin = sys.stdin

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

n, k = na()
li = [ni() for i in range(n)]
li.sort()
ans = 100000000000000000
for i in range(n - k + 1):
    dif = li[i + k - 1] - li[i]
    ans = min(ans, dif)
    if ans == 0:
        break
print(ans)

Solution (Python):

import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)


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

print(ans)

Solution (Python):
import heapq


n, k = map(int, input().split())
hs = list(zip(sorted([int(input()) for _ in range(n)]),
              range(n)))
minus_hs = []
for i in range(n):
    minus_hs.append((-hs[i][0], hs[i][1]))

q = hs[:k - 1][:]
heapq.heapify(q)
minus_q = minus_hs[:k - 1][:]
heapq.heapify(minus_q)

min_diff = float('inf')
for i in range(k - 1, n):
    heapq.heappush(q, hs[i])
    heapq.heappush(minus_q, minus_hs[i])
    while q[0][1] < i - k + 1:
        heapq.heappop(q)
    while minus_q[0][1] < i - k + 1:
        heapq.heappop(minus_q)
    min_diff = min(min_diff, abs(q[0][0] + minus_q[0][0]))
print(min_diff)

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

sa = sorted(a)
v=10**9
for i in range(n-k+1):
	if v > sa[i+k-1]-sa[i]:
		v = sa[i+k-1]-sa[i]
print(v)

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

_, k, *h = map(int, open(0).read().split())
h.sort()
ans = 10**9
for hmin, hmax in zip(h, h[k-1:]):
    ans = min(ans, hmax-hmin)
print(ans)

Solution (Python):
N,K = map(int,input().split())
HList = [ int(input()) for _ in range(N)]
HList.sort()
ans = 10**10
for n in range(N-K+1):
    num = abs( HList[n]-HList[n+K-1])
    if num < ans:
        ans = num
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 = map(int, input().split())
h = [int(input()) for i in range(n)]
h.sort()
result = float('inf')
for i in range(n - k + 1):
  if h[i + k - 1] - h[i] < result:
    result = h[i + k - 1] - h[i]
print(result)

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

    sort_h = sorted(h, reverse=True)

    ans = float('inf')
    for i in range(N-K+1):
        tmp = sort_h[i] - sort_h[i + K - 1]
        if ans > tmp:
            ans = tmp

    print(ans)

main()

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

Solution (Python):
import random

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

def quick_sort(array):
    low_array = []
    high_array = []
    len_array = len(array)
    if len_array <= 1:
        return array
    pivot = random.choice(array)
    pivot_count = 0
    for i in range(len_array):
        if pivot > array[i]:
            low_array.append(array[i])
        elif pivot < array[i]:
            high_array.append(array[i])
        else:
            pivot_count += 1
    low_array = quick_sort(low_array)
    high_array = quick_sort(high_array)
    return low_array + [pivot]*pivot_count + high_array

H = quick_sort(H_input)

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

print(quick_sort(min)[0])

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

sl = sorted(l)
mn = 10 ** 9
for i in range(N):
    if i + K - 1< N:
        if mn > sl[i + K - 1] - sl[i]:
            mn = sl[i + K - 1] - sl[i];

print(mn)

Solution (Python):
n,k=map(int,input().split())
h=[int(input()) for i in range(n)]
h.sort()
d=[0]*n
for j in range(n-1):
  d[j+1]=d[j]+h[j+1]-h[j]
m=float('inf')
for l in range(n-k+1):
  m=min(m,d[l+k-1]-d[l])
print(m)

Solution (Python):
import sys
import math



n,k = map(int, input().split())
h = sorted(list(map(int, (input() for i in range(n)))))

ans = max(h)

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 _ in range(n):
    h.append(int(input()))

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())
lis = []
for i in range(n):
	h = int(input())
	lis.append(h)
lis.sort()
diff = []
for j in range(n-k+1):
	diff.append(lis[j+k-1] - lis[j])
print(min(diff))

Solution (Python):
n,k = (int(i) for i in 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=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, h[i+k-1]-h[i])
print(ans)

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

psa=float('inf')
for i in range(n-k+1):
  sa=hs[k+i-1]-hs[i]
  if psa>sa:
    psa=sa

print(psa)

Solution (Python):
def main():
    N, K = map(int, input().split())
    H = []
    for _ in range(N):
        H.append(int(input()))
    H = sorted(H)
    min_d = float('inf')
    for i in range(N-K+1):
        min_d = min(min_d, H[i+K-1]-H[i])
    return min_d


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

Solution (Python):

import sys
import math
from collections import defaultdict


sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]


mod = 10**9 + 7


def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N):
    read_all = [tuple(map(int, input().split())) for _ in range(N)]
    return map(list,zip(*read_all))



N,K = II()
h = [I() for _ in range(N)]
h.sort()

ans = float('inf')

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

print(ans)

Solution (Python):


import itertools

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

for _ in range(n):
    h_list.append(int(input().split()[0]))

h_list = sorted(h_list)

min_value = float('inf')

for i, v in enumerate(h_list[:-k+1]):
    diff_h = h_list[i+k-1] - v
    if diff_h < min_value:
        min_value = diff_h

print(min_value)

Solution (Python):
N,K = map(int,input().split())
para = [int(input()) for i in range(N)]
para.sort(reverse=True)
val = float("inf")
for i in range(0,N-K+1):
    val = min(val,para[i]-para[i+K-1])
print(val)

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

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