from collections import Counter,defaultdict,deque

from heapq import heappop,heappush,heapify

import sys,bisect,math,itertools,fractions

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)
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)
from collections import Counter, defaultdict, deque
from heapq import heappop, heappush, heapify
import sys, bisect, math, itertools, fractions

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)

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)

-import sys, bisect, math, itertools, fractions +from bisect import bisect_left, bisect_right +import sys, math, itertools, fractions, pprint