INF = 1000000007



input = open(0).read



N, K, *h = list(map(int, input().split()))





def main():



    h.sort()



    ans = INF



    for i in range(N - K + 1):

        ans = min(ans, h[i + K - 1] - h[i])



    print(ans)

    return





main()

def main():

    N, K, *h = list(map(int, open(0).read().split()))

    h.sort()

    print((min(j - i for i, j in zip(h, h[K - 1:]))))

    return





main()

INF = 1000000007
input = open(0).read
N, K, *h = list(map(int, input().split()))


def main():
    h.sort()
    ans = INF
    for i in range(N - K + 1):
        ans = min(ans, h[i + K - 1] - h[i])
    print(ans)
    return


main()

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


main()

-INF = 1000000007 -input = open(0).read -N, K, *h = list(map(int, input().split())) - - +    N, K, *h = list(map(int, open(0).read().split())) -    ans = INF -    for i in range(N - K + 1): -        ans = min(ans, h[i + K - 1] - h[i]) -    print(ans) +    print((min(j - i for i, j in zip(h, h[K - 1 :]))))