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

int heights[100000];

int main()
{
 int N,K,i,min_diff = 1000000000;
 cin>>N>>K;
 for(i=0;i<N;i++)
 {
  cin>>heights[i];
 }
 sort(heights,heights+N);
 for(i=0;i<N-K+1;i++)
 {
  if(heights[i+K-1]-heights[i]<min_diff)
  {
      min_diff = heights[i+K-1]-heights[i];
  }
 }
 cout<<min_diff;
 return 0;
}
