Can someone please tell me what's wrong with my code?

Also, I have some confusion about the calculation of frequency and product of divisor. If anyone could help me it would be greatly appreciated.

Here is my code:

bool func (const pair<int, int> &a, const pair<int, int> &b) {
    if (a.first == b.first) return (a.second < b.second);
    return (a.first > b.first);
}

int divisor (int x) {
    long long int ans = 1;
    for (int i = 2; i <= x / 2; i++) {
        if (x % i == 0) ans = (ans * i) % 1000000007;
    }
    ans = (ans * x) % 1000000007;
    return (int)ans;
}

vector<int> Solution :: solve (vector<int> &a, vector<int> &q) {
    int n = a.size();
    sort(a.begin(), a.end());
    vector<pair<int, int>> g;

    for (int i = 0; i < n; i++) g.push_back(make_pair(a[i], i + 1));

    n = g.size();
    for (int i = 0; i < n; i++) g[i].first = divisor(g[i].first);
    sort(g.begin(), g.end(), func);

    g[0].second = g[0].second - 1;
    for (int i = 1; i < n; i++) g[i].second += g[i - 1].second;
    
    for (int i = 0; i < q.size(); i++) {
        int start = 0, end = g.size() - 1, j;
        
        for (; start <= end;) {
            j = (start + end) / 2;
            
            if (j == 0) {
                q[i] = g[j].first;
                break;
            }           

            if (g[j - 1].second < q[i] - 1 && q[i] - 1 <= g[j].second) {
                q[i] = g[j].first;
                break;
            }
            else if (q[i] - 1 > g[j].second) start = j + 1;
            else end = j - 1;
        }
    }

    return q;
}

@joshi_vishrut
calculation of frequency can be found by

unordered_map<long long int,int> freq;
for(long long int const &i:g)
freq[i]++;

product_of_divisor = pow(n,d/2); // d is the number of divisors

Your logic to calculate G array fails for input
1 5 2 4 3 6
. Since we are calculating max elements in subarrays so sorting before calulating G is incorrect as the original array will be lost.

Click here to start solving coding interview questions