Greedy algorithm

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)

Here is the problem: http://codeforces.com/problemset/problem/337/A

Here is my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#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


using namespace 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];

    static int k = 0;
    static int 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 ^^
Topic archived. No new replies allowed.