#include<iostream>
#include<algorithm>
using namespace std;

int main(){
  int N,K,h[100000],i,ans=((long long)1<<31)-1,temp=0;
  cin>>N>>K;
  for(i=0;i<N;i++){
    cin>>h[i];
  }
  sort(h,h+N);
  for(i=0;i<N-K+1;i++){
    if(h[i+K-1]-h[i]<ans)ans=h[i+K-1]-h[i];
  }
  cout<<ans<<endl;
}