Hello I just encountered this problem that I solved but I wanted to check if I used the correct algorithm (My approach to the solution was right but I wanna know if the algorithm is actually greedy algorithm)
#include <bits/stdc++.h>
#define fl(n) for(int i = 0; i < n; i++)
#define ll long long
#define nl endl
#define init -9999
#define INF 1e9
#define u unsigned
usingnamespace std;
//will need greedy algorithm with a/b pruning
int main()
{
int n,m;
cin >> n >> m;
int a[m];
for(int i = 0; i < m; i++)
cin >> a[i];
staticint k = 0;
staticint z = 0;
int tot;
int total[50];
//int mn[50];
// mn[0] = 0;
int index = 0;
sort(a,a+m);
for(int j = index; j < m; j++)
{
if(index+n > m) break;
int maxx = a[index];
int minn = a[index];
for(int c = index; c < (index+n); c++)
{
if(maxx < a[c]) maxx = a[c];
}
for(int c = index; c < (index+n); c++)
{
if(minn > a[c]) minn = a[c];
}
tot = 0;
tot = maxx - minn;
total[z] = tot;
z++;
++index;
}
int mnn = total[0];
for(int c = 0; c < z; c++)
{
if(mnn > total[c]) mnn = total[c];
}
cout << mnn;
return 0;
}
i drew a tree for myself to solve this problem and i pruned the nodes where the indexes of them will be irrelevant if we reach them but uhm, it said the problem could use greedy algorithm or dynamic programming and im honestly not experienced with both but I tried it this way after reading about both and idk which one I used in my code :D but hope someone experienced knows ^^