Language: C++

/*																													   
 Templatka c++ Kacper Fis																							   																											   //
*/																													  
																													   
#include <iostream>       	
#include <cstdio>         
#include <limits>		  
#include <cstdlib>       
#include <utility>        
#include <iterator>       
#include <bitset>         
#include <list>           
#include <vector>
#include <map>
#include <unordered_map>
#include <set>            														   
#include <queue>          																							   
#include <deque>          																							   
#include <stack>																									   
#include <algorithm>																								   
#include <cmath>																									   
#include <string>      																								   
#include <time.h>																									   
#include <iomanip> 																									   
#include <sstream>																									   
#include <bits/stdc++.h>																							   
	
using namespace std; 	
																													   
const int INF = 1000000001;																						   
const float PI = 3.14;																								   
typedef long long LL;																								   
																													   
typedef vector<int> vi;																								   
typedef list<int> li;																								   
typedef queue<int> qi; 																								   
typedef pair<int,int> ii; 																							   
typedef map<string,int> msi; //np msi['nazwa'] = 'liczba'															   
typedef vector<ii> vii; //vector par<int>, do tworzenia tablicy dla grafĂłw wagowych #dijkstra #Bellman_Ford			   
typedef priority_queue< ii, vector<ii>, greater<ii> > pq; //kolejka priorytetowa vectorĂłw par(top()=min) #dijkstra	   
																													   
//list<int>::iterator it;																								   
list<int>::iterator it1;																							   
vector<int>::iterator it2;																							   
vector<int>::iterator it3;																							   
set<int>::iterator it4;																								   
map<string, int>::iterator it5; 																					   
vector<ii>::iterator iter;																							   
																													   																				   
#define print_list(x) for(it = (x).begin(); it != (x).end(); it++) {cout << *it << " ";}							   
#define print_vector(x) for(it2 = (x).begin(); it2 != (x).end(); it2++) {cout << *it2 << " ";}						   
#define search_list(x) for(it = (x).begin(); it != (x).end(); it++)													   
#define search_vector(x) for(it2 = (x).begin(); it2 != (x).end(); it2++);											   
#define pb(x) push_back(x) 																							   
#define pf(x) push_front(x)																							   
#define mp(x, y) make_pair(x, y)                                                                                       
///////////////////////////////////////////////GLOBAL DEFINITIONS////////////////////////////////////////////////////////

int tab[1000005];

///////////////////////////////////////////////////FUNCTIONS/////////////////////////////////////////////////////////////


/////////////////////////////////////////////////////MAIN////////////////////////////////////////////////////////////////
int main(int argc, char* argv[])																					 
{ios_base::sync_with_stdio(false); cin.tie(NULL);    
	
	int n, k;
	cin >> n >> k;
	vi V;
	for(int i = 0; i < n; i++){
		int x;
		cin >> x;
		V.pb(x);
	}
	
	sort(V.begin(), V.end());
	
	//cout << "Vector V: " << endl;
	//for(auto it : V){
		//cout << it << " ";
	//}
	
	vi ans;
	for(int i = 2; i < n; i++){
		ans.pb(V[i] - V[i-k+1]);
	}
	
	sort(ans.begin(), ans.end());
	cout << *ans.begin();
	
	
	
	
	
	
	
	return 0;
}              												       
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/*																													   
 Templatka c++ Kacper Fis																							   																											   //
*/																													  
																													   
#include <iostream>       	
#include <cstdio>         
#include <limits>		  
#include <cstdlib>       
#include <utility>        
#include <iterator>       
#include <bitset>         
#include <list>           
#include <vector>
#include <map>
#include <unordered_map>
#include <set>            														   
#include <queue>          																							   
#include <deque>          																							   
#include <stack>																									   
#include <algorithm>																								   
#include <cmath>																									   
#include <string>      																								   
#include <time.h>																									   
#include <iomanip> 																									   
#include <sstream>																									   
#include <bits/stdc++.h>																							   
	
using namespace std; 	
																													   
const int INF = 1000000001;																						   
const float PI = 3.14;																								   
typedef long long LL;																								   
																													   
typedef vector<int> vi;																								   
typedef list<int> li;																								   
typedef queue<int> qi; 																								   
typedef pair<int,int> ii; 																							   
typedef map<string,int> msi; //np msi['nazwa'] = 'liczba'															   
typedef vector<ii> vii; //vector par<int>, do tworzenia tablicy dla grafĂłw wagowych #dijkstra #Bellman_Ford			   
typedef priority_queue< ii, vector<ii>, greater<ii> > pq; //kolejka priorytetowa vectorĂłw par(top()=min) #dijkstra	   
																													   
//list<int>::iterator it;																								   
list<int>::iterator it1;																							   
vector<int>::iterator it2;																							   
vector<int>::iterator it3;																							   
set<int>::iterator it4;																								   
map<string, int>::iterator it5; 																					   
vector<ii>::iterator iter;																							   
																													   																				   
#define print_list(x) for(it = (x).begin(); it != (x).end(); it++) {cout << *it << " ";}							   
#define print_vector(x) for(it2 = (x).begin(); it2 != (x).end(); it2++) {cout << *it2 << " ";}						   
#define search_list(x) for(it = (x).begin(); it != (x).end(); it++)													   
#define search_vector(x) for(it2 = (x).begin(); it2 != (x).end(); it2++);											   
#define pb(x) push_back(x) 																							   
#define pf(x) push_front(x)																							   
#define mp(x, y) make_pair(x, y)                                                                                       
///////////////////////////////////////////////GLOBAL DEFINITIONS////////////////////////////////////////////////////////

int tab[1000005];

///////////////////////////////////////////////////FUNCTIONS/////////////////////////////////////////////////////////////


/////////////////////////////////////////////////////MAIN////////////////////////////////////////////////////////////////
int main(int argc, char* argv[])																					 
{ios_base::sync_with_stdio(false); cin.tie(NULL);    
	
	int n, k;
	cin >> n >> k;
	vi V;
	for(int i = 0; i < n; i++){
		int x;
		cin >> x;
		V.pb(x);
	}
	
	sort(V.begin(), V.end());
	
	//cout << "Vector V: " << endl;
	//for(auto it : V){
		//cout << it << " ";
	//}
	
	vi ans;
	for(int i = 2; i < n; i++){
		ans.pb(V[i] - V[i-k+1]);
	}
	
	sort(ans.begin(), ans.end());
	cout << *ans.begin();
	
	
	
	
	
	
	
	return 0;
}              												       
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


Compiler output:
a.cc:130:11: error: redefinition of 'const int INF'
  130 | const int INF = 1000000001;
      |           ^~~
a.cc:30:11: note: 'const int INF' previously defined here
   30 | const int INF = 1000000001;
      |           ^~~
a.cc:131:13: error: redefinition of 'const float PI'
  131 | const float PI = 3.14;
      |             ^~
a.cc:31:13: note: 'const float PI' previously defined here
   31 | const float PI = 3.14;
      |             ^~
a.cc:143:21: error: redefinition of 'std::__cxx11::list<int>::iterator it1'
  143 | list<int>::iterator it1;
      |                     ^~~
a.cc:43:21: note: 'std::__cxx11::list<int>::iterator it1' previously declared here
   43 | list<int>::iterator it1;
      |                     ^~~
a.cc:144:23: error: redefinition of 'std::vector<int>::iterator it2'
  144 | vector<int>::iterator it2;
      |                       ^~~
a.cc:44:23: note: 'std::vector<int>::iterator it2' previously defined here
   44 | vector<int>::iterator it2;
      |                       ^~~
a.cc:145:23: error: redefinition of 'std::vector<int>::iterator it3'
  145 | vector<int>::iterator it3;
      |                       ^~~
a.cc:45:23: note: 'std::vector<int>::iterator it3' previously defined here
   45 | vector<int>::iterator it3;
      |                       ^~~
a.cc:146:20: error: redefinition of 'std::set<int>::iterator it4'
  146 | set<int>::iterator it4;
      |                    ^~~
a.cc:46:20: note: 'std::set<int>::iterator it4' previously declared here
   46 | set<int>::iterator it4;
      |                    ^~~
a.cc:147:28: error: redefinition of 'std::map<std::__cxx11::basic_string<char>, int>::iterator it5'
  147 | map<string, int>::iterator it5;
      |                            ^~~
a.cc:47:28: note: 'std::map<std::__cxx11::basic_string<char>, int>::iterator it5' previously declared here
   47 | map<string, int>::iterator it5;
      |                            ^~~
a.cc:148:22: error: redefinition of 'std::vector<std::pair<int, int> >::iterator iter'
  148 | vector<ii>::iterator iter;
      |                      ^~~~
a.cc:48:22: note: 'std::vector<std::pair<int, int> >::iterator iter' previously defined here
   48 | vector<ii>::iterator iter;
      |                      ^~~~
a.cc:159:5: error: redefinition of 'int tab [1000005]'
  159 | int tab[1000005];
      |     ^~~
a.cc:59:5: note: 'int tab [1000005]' previously declared here
   59 | int tab[1000005];
      |     ^~~
a.cc:165:5: error: redefinition of 'int main(int, char**)'
  165 | int main(int argc, char* argv[])
      |     ^~~~
a.cc:65:5: note: 'int main(int, char**)' previously defined here
   65 | int main(int argc, char* argv[])
      |     ^~~~