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






import sys
input = sys.stdin.readline

import math

import bisect
from collections import defaultdict
from collections import deque
from functools import lru_cache






MOD = 10**9+7
INF = float('inf')






def I(): return int(input().strip())
def S(): return input().strip()
def IL(): return list(map(int,input().split()))
def SL(): return list(map(str,input().split()))
def ILs(n): return list(int(input()) for _ in range(n))
def SLs(n): return list(input().strip() for _ in range(n))
def ILL(n): return [list(map(int, input().split())) for _ in range(n)]
def SLL(n): return [list(map(str, input().split())) for _ in range(n)]


def P(arg): print(arg); return
def Y(): print("Yes"); return
def N(): print("No"); return
def E(): exit()
def PE(arg): print(arg); exit()
def YE(): print("Yes"); exit()
def NE(): print("No"); exit()


def DD(arg): return defaultdict(arg)



def inv(n): return pow(n, MOD-2, MOD)


kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret


def factorization(n):
  arr = []
  temp = n
  for i in range(2, int(-(-n**0.5//1))+1):
    if temp%i==0:
      cnt=0
      while temp%i==0: 
        cnt+=1 
        temp //= i
      arr.append([i, cnt])
  if temp!=1:
    arr.append([temp, 1])
  if arr==[]:
    arr.append([n, 1])
  return arr


def make_divisors(n):
  divisors = []
  for i in range(1, int(n**0.5)+1):
    if n % i == 0:
      divisors.append(i)
      if i != n // i: 
        divisors.append(n//i)
  return divisors


def lcm(a, b):
    return a * b // gcd (a, b)


def count_bit(n):
  count = 0
  while n:
    n &= n -1
    count += 1
  return count


def base_10_to_n(X, n):
  if X//n:
    return base_10_to_n(X//n, n)+[X%n]
  return [X%n]

def base_n_to_10(X, n):
  return sum(int(str(X)[-i])*n**i for i in range(len(str(X))))


def int_log(n, a):
  count = 0
  while n>=a:
    n //= a
    count += 1
  return count





N,K = IL()
T = ILs(N)

T.sort()

print(min( T[i+K-1]-T[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()

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

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

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


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





if __name__ == "__main__":
    main()

Solution (Python):

N, K = map(int, input().split())
H = sorted([int(input()) for i in range(N)])
rt = 10**9
for i in range(0,N-K+1):
    rt = H[K - 1 + i] - H[i] if rt > H[K - 1 + i] - H[i] else rt
print(rt)

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

h.sort()
result = 10**9+1
for i in range(N-K+1):
    a = abs(h[i] - h[i+K-1])
    if a < result:
        result = a

print(result)

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

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

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

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

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

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

ans = 1e+12
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())
High = [int(input()) for _ in range(N)]
High.sort()
ans = float('inf')
for i in range(N - K + 1):
    ans = min(High[K + i - 1] - High[i], ans)

print(ans)

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

h.sort(reverse=True) 

out = 10**9

L = 0
R = K-1
while R<=N-1:
    D = h[L]-h[R]
    if out > D:
        out = D
    L+=1
    R+=1
print(out)

Solution (Python):
import itertools
import math
import fractions
import functools
import copy

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

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

print(minimum)

Solution (Python):


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

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

print(min(diffs))

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

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

min_d = 10**9
for i in range(K-1,N):
  d=h[i]-h[i-(K-1)]
  if d<min_d:
    min_d=d
print(min_d)

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

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

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

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

Solution (Python):
n,k = map(int,input().split())
h_list = [int(input()) for nesya in range(n)]
h = sorted(h_list)
s = 10**9
for i in range(n-k+1):
  t = h[i+k-1] - h[i]
  if s > t:
    s = t
print(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 = map(int, input().split())
h = []
for i in range(n):
    h.append(int(input()))

h.sort()

ret = 10 ** 100

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

print(ret)

Solution (Python):
N, K = map(int, input().split(' '))
heights = sorted([int(input()) for _ in range(N)])
print(min((_max - _min for _min, _max in zip(heights, heights[K - 1:]))))

Solution (Python):
N, K = map(int, input().split(' '))
heights = sorted([int(input()) for _ in range(N)])
print(min([_max - _min for _min, _max in zip(heights, heights[K - 1:])]))

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

Solution (Python):



from heapq import heapify, heappop, heappush

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

stack = []

for i in range(k):
  heappush(stack, H[i])

r = stack[-1] - stack[0] 

for i in range(k, n):
  heappop(stack)
  heappush(stack, H[i])
  r = min(r, stack[-1] - stack[0])

print(r)

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

L = []

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

print(min(L))

Solution (Python):


import sys
input= lambda: sys.stdin.readline().rstrip()
def pin(type=int):return map(type,input().split())
def tupin(t=int):return tuple(pin(t))

def resolve():
    N,K=pin()
    h=[int(input()) for i in range(N)]
    h.sort()
    temp=10**9+1
    for i in range(N-K+1):
        temp=min(temp,h[i+K-1]-h[i])
    print(temp)

resolve()

Solution (Python):
import itertools
import sys

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

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

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

print(m)

Solution (Python):
import sys
sys.setrecursionlimit(10 ** 6)


def input():
    return sys.stdin.readline()[:-1]


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

Solution (Python):
import sys
input = sys.stdin.readline
n, k = [int(x) for x in input().split()]
h = [int(input()) for _ 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):
import sys
def input(): return sys.stdin.readline().strip()

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

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

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

  return

if __name__ == "__main__":
  resolve()

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

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

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

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

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

h = sorted(h)

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

print(min(candidates))

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

ans = 10**18
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 = 1e10
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
from collections import *

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

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

h.sort()
ans = float("inf")

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

print(ans)

Solution (Python):
import sys
import math
import bisect

def solve(A, m):
    n = len(A)
    min_val = 10 ** 18
    A.sort()
    for i in range(n):
        if i + m <= n:
            val = A[i+m-1] - A[i]
            min_val = min(min_val, val)
    return min_val

def main():
    n, m = map(int, input().split())
    A = []
    for i in range(n):
        A.append(int(input()))
    print(solve(A, m))

if __name__ == "__main__":
    main()

Solution (Python):
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
from itertools import permutations, accumulate, combinations, combinations_with_replacement
from math import sqrt, ceil, floor, factorial
from bisect import bisect_left, bisect_right, insort_left, insort_right
from copy import deepcopy
from operator import itemgetter
from functools import reduce
from fractions import gcd
import sys
def input(): return sys.stdin.readline().rstrip()
def I(): return int(input())
def Is(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def TI(): return tuple(map(int, input().split()))
def IR(n): return [I() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def TIR(n): return [TI() for _ in range(n)]
def S(): return input()
def Ss(): return input().split()
def LS(): return list(input())
def SR(n): return [S() for _ in range(n)]
def SsR(n): return [Ss() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
sys.setrecursionlimit(10**6)
MOD = 10**9+7
INF = 10**18


n, k = Is()
H = IR(n)
H.sort()
ans = INF
for i in range(n-k+1):
    hmax = H[i+k-1]
    hmin = H[i]
    ans = min(ans, hmax-hmin)
print(ans)

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 = [0] * n
for i in range(n):
    x = int(input())
    h[i] = x

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):
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(' '))
wood = []
difference = []
for i in range (N):
  wood.append(int(input()))
wood.sort()
  
for i in range(N-K+1):
  difference.append(wood[K + i - 1] - wood[i])
print(min(difference))

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


import sys






















def main():
    mod = 1000000007                
    inf = float('inf')              
    
    sys.setrecursionlimit(10**6)    
    def input(): return sys.stdin.readline().rstrip()
    def ii():    return int(input())
    def mi():    return map(int, input().split())
    def mi_0():  return map(lambda x: int(x)-1, input().split())
    def lmi():   return list(map(int, input().split()))
    def lmi_0(): return list(map(lambda x: int(x)-1, input().split()))
    def li():    return list(input())
    
    
    n, k = mi()
    h = [ii() for _ in range(n)]
    h.sort()
    ans = inf
    for i in range(n - k + 1):
        j = i + (k - 1)
        ans = min(ans, h[j] - h[i])
    print(ans)

if __name__ == "__main__":
    main()

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

sorted_trees = sorted(h, reverse=True)

minimum = float('inf')
for i in range(n-(k-1)):
    diff = sorted_trees[i] - sorted_trees[i+(k-1)]
    if diff < minimum:
        minimum = diff
print(minimum)

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

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

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

    ans = 1 << 30
    left = 0
    while left + K - 1 < N:
        right = left + K - 1
        d = h[right] - h[left]
        if ans > d:
            ans = d
        left += 1
    print(ans)


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 heapq import heapify, heappop, heappush
 
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
 
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N, K = MAP()
H = [INT() for i in range(N)]
H.sort()
ans = INF
for i in range(N-K+1):
    mi = H[i]
    ma = H[i+K-1]
    tmp = ma - mi
    ans = min(tmp, ans)
print(ans)

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

h.sort(reverse=True)
ans = []
for i in range(n):
    if i+k-1 >= n:
        break
    ans.append(abs(h[i] - h[i+k-1]))
print(min(ans))

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


def readstr():
    return input().strip()


def readint():
    return int(input())


def readnums():
    return map(int, input().split())


def readstrs():
    return input().split()


def main():
    N, K = readnums()
    h = np.sort(np.array([readint() for _ in range(N)]))
    ans = 0
    for i in range(N - K + 1):
        if not i:
            ans = h[i + K - 1] - h[i]
        else:
            ans = min(ans, h[i + K - 1] - h[i])

    print(ans)


if __name__ == "__main__":
    main()

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

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

res = 10 ** 100
for i in range(n):
    j = i + k - 1
    if j >= n:
        break
    res = min(res, h[j] - h[i])
print(res)

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

h.sort()

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

print(mn)

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

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

Solution (Python):
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
from bisect import bisect_left,bisect_right 
import sys,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

n,k = inpl()
a = [inp() for _ in range(n)]
a.sort()
res = INF
for i in range(n-k+1):
    res = min(res, a[i+k-1] - a[i])
print(res)

Solution (Python):



import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
from math import floor, ceil,pi
from operator import itemgetter
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]
def SI(): return input().rstrip()
def printns(x): print('\n'.join(x))
def printni(x): print('\n'.join(list(map(str,x))))
inf = 10**17
mod = 10**9 + 7
n,k=MI()
lis=sorted([I() for i in range(n)])
u=[]
for i in range(n-k+1):
    u.append(lis[i+k-1]-lis[i])
print(min(u))

Solution (Python):
def ii():return int(input())
def iim():return map(int,input().split())
def iil():return list(map(int,input().split()))
def ism():return map(str,input().split())
def isl():return list(map(str,input().split()))
n,k = iim()
h = []
for _ in range(n):
    h.append(ii())
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())
L = [int(input()) for i in range(N)]

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

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

if __name__ == "__main__":
  resolve()

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

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

Solution (Python):
import sys

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


def resolve():
    n, k = map(int, input().split())
    H = sorted(list(int(input()) for _ in range(n)))

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

    print(res)


if __name__ == '__main__':
    resolve()

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

Solution (Python):
from sys import stdin
def main():
    
    readline=stdin.readline
    n,k=map(int,readline().split())
    h=[0]*n
    for i in range(n):
        h[i]=int(readline())
    h.sort()

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

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

Solution (Python):
import sys, math, itertools, collections, bisect
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
inf = float('inf') ;mod = 10**9+7
mans = inf ;ans = 0 ;count = 0 ;pro = 1

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

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

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

n, k = map(int, readline().split())
h = [int(readline()) for _ in range(n)]
h.sort()
ans = float('inf')
for bf, af in zip(h, h[k - 1:]):
    ans = min(ans, af - bf)
print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
N, K = map(int, input().split())
a = [0] * N
for i in range(N): a[i] = int(input())
a.sort()
res = float("inf")
for i in range(N - K + 1):
  res = min(res, a[i + K - 1] - a[i])
print(res)

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

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

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

ans = []
for i in range(0,len(h)-k+1):
  ans.append(h[i+k-1] - h[i])

print(min(ans))

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

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

Solution (Python):
def main():
    N, K = list(map(int, input().split(' ')))
    H = [int(input()) for _ in range(N)]
    H.sort()
    min_diff = H[-1] - H[0]
    for i in range(N - K + 1):
        min_diff = min([min_diff, H[i + K - 1] - H[i]])
    print(min_diff)


if __name__ == '__main__':
    main()

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
from math import floor, ceil
from operator import itemgetter
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]
def printns(x): print('\n'.join(x))
def printni(x): print('\n'.join(list(map(str,x))))
inf = 10**17
mod = 10**9 + 7


n,k=MI()
u=[]
lis=sorted([I() for i in range(n)])
for i in range(n-k+1):
    u.append(lis[i+k-1]-lis[i])
print(min(u))

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

h.sort()
h = deque(h)
d = []
d = deque(d)

for _ in range(k):
	d.append(h.popleft())

ans = d[-1] - d[0]
for _ in range(n-k):
	d.popleft()
	d.append(h.popleft())
	num = d[-1] - d[0]
	if ans > num:
		ans = num
print(ans)

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

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

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

h.sort()
subs = []
for i in range(1,n):
    subs.append(h[i]-h[i-1])

amount = [0]*(n)
sums = 0
for i in range(0,len(subs)):
    sums += subs[i]
    amount[i+1] = sums


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

print(minimum)

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

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

h_list.sort()

ans = 10000000000
for i in range(n-k+1):
    tmp = abs(h_list[i]-h_list[i+k-1])
    if ans > tmp:
        ans = tmp

print(ans)

Solution (Python):

import numpy as np
N,K= map(int, input().split())
Tree_List= [int(input()) for i in range(N)]
Tree_List=np.array(sorted(Tree_List))
Answer=min(Tree_List[K-1:]-Tree_List[:-(K-1)])
print(Answer)

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

Solution (Python):
n, k = map(int, input().split())
h = sorted([int(input()) for i in range(n)])
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 = [0]*n
for i in range(n):
    h[i] = int(input())
h.sort()
ans = float("inf")
for i in range(n-k+1):
    res = abs(h[i] - h[i+k-1])
    ans = min(ans, res)
print(ans)

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

h.sort()

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

print(min_diff)

Solution (Python):
import sys

input = sys.stdin.readline


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

    print(ans)


if __name__ == "__main__":
    main()

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

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

a.sort()

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

print(ans)