Easy Java Solution 100% Beats
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```java []\nclass Solution {\n    public static long[] freq = new long[(int) 5e4 + 1];\n    public int[] gcdValues(int[] nums, long[] q) {\n        int n = nums.length;\n        int m = q.length;\n        int max = 0;\n        for(int i = 0; i < n; i++) {\n            int l = nums[i];\n            max = Math.max(max, l);\n            for(int j = 1; j * j <= l; j++) {\n                if(l % j == 0) {\n                    freq[j]++;\n                    if(j == (l / j)) continue;\n                    freq[l / j]++;\n                }\n            }\n        }\n        long[] dp = new long[max + 1];\n        for(int i = max; i >= 1; i--) {\n            long count = (1l * freq[i] * (freq[i] - 1)) / 2;\n            dp[i] += count;\n            for(int j = i * 2; j <= max; j += i) {\n                dp[i] -= dp[j];\n            }\n        }\n        for(int i = 1; i <= max; i++) {\n            dp[i] += dp[i - 1];\n        }\n        int[] ans = new int[m];\n        for(int i = 0; i < m; i++) {\n            long l = q[i] + 1;\n            int r = BS(dp, l);\n            ans[i] = r;\n        }\n        for(int i = 0; i < n; i++) {\n            int l = nums[i];\n            for(int j = 1; j * j <= l; j++) {\n                if(l % j == 0) {\n                    freq[j]--;\n                    if(j == (l / j)) continue;\n                    freq[l / j]--;\n                }\n            }\n        }\n        return ans;\n    }\n    public static int BS(long[] dp, long i) {\n        int start = 0;\n        int end = dp.length;\n        while(start < end) {\n            int mid = start + (end - start) / 2;\n            if(i > dp[mid]) {\n                start = mid + 1;\n            } else {\n                end = mid;\n            }\n        }\n        return start;\n    }\n}\n```
['Java']