Algorithm
Code Examples
#1 Code Example with C++ Programming
Code -
C++ Programming
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'
#define debug(n) cout<<(n)<<endl;
const ll INF = 2e18 + 99;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, m;
cin>>n>>m;
int arr[m];
for(int i = 0; i < m; i++){
cin>>arr[i];
}
sort(arr, arr+m);
int res = 1000000;
for(int i = 0; i <= m - n; i++){
if((arr[n + i - 1] - arr[i]) < res){
res = (arr[i+n-1] - arr[i]);
}
}
cout<<res<<endl;
}
Copy The Code &
Try With Live Editor
Input
4 6
10 12 10 7 5 22
10 12 10 7 5 22
Output
5
Demonstration
Codeforcess Solution 337-A A. Puzzles ,C++, Java, Js and Python ,337-A,Codeforcess Solution