#include<stdio.h>

#include<algorithm>

using namespace std;

int main(){

long long int t,x,y,i,h,ar[10000],arr[10000];

scanf("%lld",&t);

while(t--){

h=0;

scanf("%lld %lld",&x,&y);

for(i=0;i<x;i++)

scanf("%lld",&ar[i]);

sort(ar,ar+x);

for(i=y-1;i<x;i++){

arr[h]=ar[i]-ar[i+1-y];

h+=1;

}

sort(arr,arr+h);

printf("%lld\n",arr[0]);

}

return 0;

}

#include<algorithm>

using namespace std;

int main(){

long long int t,x,y,i,h,ar[10000],arr[10000];

scanf("%lld",&t);

while(t--){

h=0;

scanf("%lld %lld",&x,&y);

for(i=0;i<x;i++)

scanf("%lld",&ar[i]);

sort(ar,ar+x);

for(i=y-1;i<x;i++){

arr[h]=ar[i]-ar[i+1-y];

h+=1;

}

sort(arr,arr+h);

printf("%lld\n",arr[0]);

}

return 0;

}

## No comments:

## Post a Comment