program atcoder_abc115c_sort
    implicit none
    integer(8) :: n,k,i,ans=10000000000_8
    integer(8),allocatable,dimension(:) :: a
    read*,n,k
    allocate( a(n) )
    read(*,*)(a(i),i=1,n)
    call sort(a)
    do i=1,n-k+1
        ans = min(ans,a(i+k-1)-a(i))
    enddo
    write(*,'(I0)')ans
!----------------------functions--------------------------
contains
recursive subroutine mergesort(a, p, r, b)
  integer(8) a(:), b(:)
  integer(8) p, r
  integer(8) q
  if (p < r) then
    q = (p+r)/2
    call mergesort(a, p, q, b)
    call mergesort(a, q+1, r, b)
    call merge(a, p, q, r, b)
  end if
end subroutine mergesort
subroutine merge(a, p, q, r, b)
    integer(8) a(:), b(:)
    integer(8) p, r, q, i, j, k
    integer(8) t
    k = p;   i = p;   j = q+1
    do
      if (.not. (k <= r)) exit
      if (j > r) then
        t = a(i);   i = i+1
      else if (i > q) then
        t = a(j);   j = j+1
      else if (a(i) <= a(j)) then
        t = a(i);   i = i+1
      else
        t = a(j);   j = j+1
      end if
      b(k) = t;   k = k+1
    end do
    do i = p, r
      a(i) = b(i)  
    end do
end subroutine merge
subroutine sort(a)
    integer(8) a(:), b(size(a)),n
    n = size(a)
    call mergesort(a,1_8,n,b)
end subroutine sort
end program atcoder_abc115c_sort