detailed comments
Code\ncpp []\nclass Solution {\npublic:\n    vector<int> gcdValues(vector<int>& nums, vector<long long>& queries) {\n        //1.find frequency of all factors o
# Code\n```cpp []\nclass Solution {\npublic:\n    vector<int> gcdValues(vector<int>& nums, vector<long long>& queries) {\n        //1.find frequency of all factors of every nums[i]\n        //2.for each factor frequency, freqC2 pairs can be made = freq*(freq-1)/2\n        //this number of pairs = gcd count of that factor\n        //3.from this number substract the number of pairs it\'s multiples can make \n        //(greatest common divisor bruh)\n        //4.so now we know how many times a factor will be appearing \n        //just map this "how many times" to the factor\n        //5.calculate lower bound for each query and return the factor stored for that\n        //We will find lower bound on indices, for that need to store indices of each gcd\n        //for example= gcdcnt[3] =2,gcdcnt[4] =5. we will store {1,6} 0,1 index for 3, 2-6 index for 4\n        map<int,int> factorfreq;\n       \n        for(int i=0;i<nums.size();i++){\n            for(int j=1;j*j<=nums[i];j++){\n                if(nums[i]%j==0) {\n                    factorfreq[j]++;\n                    if(j!=nums[i]/j) factorfreq[nums[i]/j]++; //to avoid double counting sqaure root\n                }\n            }\n        }\n        int maxi = *max_element(nums.begin(),nums.end());\n        map<int,long int> gcdcnt;\n        for(auto i = factorfreq.rbegin();i!=factorfreq.rend();i++){\n            long int cnt = i-> second;\n            long int times = (cnt*(cnt-1))/2;\n            gcdcnt[i->first] = times;\n            for(int j=i->first*2;j<=maxi;j+=i->first){\n                gcdcnt[i->first]-=gcdcnt[j];\n            }\n        }\n        vector<long int> indextill,gcd;\n        long int start =-1;\n        for(auto it: gcdcnt){\n            if(it.second ==0) continue;\n            start+=it.second;\n            indextill.push_back(start);\n            gcd.push_back(it.first); //corresponding gcd\n        }\n        vector<int> ans;\n        for(auto q: queries){\n            int index = lower_bound(indextill.begin(),indextill.end(),q) -indextill.begin();\n            ans.push_back(gcd[index]);\n        }\n        return ans;\n    }\n};\n```
['C++']