program main
    implicit none
    integer(4):: n,k,i
    integer(4),allocatable:: h(:)
    integer(4):: li, ri, cnt

    read*, n, k
    allocate(h(n))
    do i=1,n
        read*, h(i)
    end do
    call merge_sort(h,1,n)
    print*, h(:)

    cnt=2000000001
    
    do i=1, n-k+1
        cnt = min(cnt, h(i+k-1) - h(i))
    end do
    print*, cnt

contains
recursive subroutine merge_sort(ar,f,l)
    integer(4):: ar(:), f,l,tmp,m

    if (l-f <= 1) then
        if (ar(l) < ar(f)) then
            tmp = ar(f)
            ar(f) = ar(l)
            ar(l) = tmp
        end if
        RETURN
    end if

    m = (f+l)/2
    call merge_sort(ar,f,m)
    call merge_sort(ar,m+1,l)
    call merge_(ar,f,m,l)
end subroutine

subroutine merge_(ar,f,m,l)
    integer(4)::ar(:),f,m,l
    integer(4):: li, fi, ti
    integer(4),allocatable:: tmp(:)
    allocate(tmp(l-f+1))
    li = m+1
    fi = f
    ti = 1

    do while(fi <= m .and. li <= l)
        if (ar(fi) <= ar(li)) then
            tmp(ti) = ar(fi)
            fi=fi+1
        else
            tmp(ti) = ar(li)
            li=li+1
        end if
        ti=ti+1
    end do


    if (fi > m) then
        tmp(ti:) = ar(li:l)
    else
        tmp(ti:) = ar(fi:m)
    end if



    ar(f:l) = tmp(:)
    deallocate(tmp)
end subroutine
end program main