Pages

Tuesday, 13 January 2015

Spoj solution of Christmas Play problem AMR10G

 Christmas Play problem


#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
long long int i,k,n,t,temp,temp2;
cin>>t;
while(t--)
{
cin>>n>>k;
long long int a[n];
for(i=0;i<n;i++)
cin>>a[i];
sort(a,a+n);
 temp=(a[k-1]-a[0]);
 if(k==1)
 cout<<temp<<endl;
 else
 {

for(i=0;i<n-k;i++)
{
temp2=(a[i+k]-a[i+1]);
if(temp>temp2)
 temp=temp2;
}
cout<<temp<<endl;
     }
     
}
return 0;

}


No comments:

Post a Comment