Skip to content

Instantly share code, notes, and snippets.

@ateruimashin
Created December 31, 2018 06:10
Show Gist options
  • Save ateruimashin/fd31dc210fc943de2e681486fb7e28c8 to your computer and use it in GitHub Desktop.
Save ateruimashin/fd31dc210fc943de2e681486fb7e28c8 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <vector>
#include <algorithm>
#include <climits>
using namespace std;
int main(int argc, char const *argv[]) {
int n,k;
cin>>n>>k;
vector<int> v(n);
for(int i=0;i<n;i++){
cin>>v[i];
}
sort(v.begin(),v.end());
int min=INT_MAX;
for(int i=0;i+k-1<n;i++){
int tmp=v[i+k-1]-v[i];
if(tmp<min) min=tmp;
}
cout<<min<<endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment