3312. Sorted GCD Pair Queries
/**
* Problem: LeetCode 3312 - Sorted GCD Pair Queries
* Difficulty: Hard
* Language: Java
* Author: CodeByTushu
*/
import java.util.*;
/**
* Problem: LeetCode 3312 - Sorted GCD Pair Queries
* Difficulty: Hard
* Language: Java
* Author: CodeByTushu
*/
class Solution {
public int[] gcdValues(int[] nums, long[] queries) {
int max = 0;
for (int x : nums) max = Math.max(max, x);
int[] freq = new int[max + 1];
for (int x : nums) freq[x]++;
long[] cnt = new long[max + 1];
for (int g = 1; g <= max; g++) {
long c = 0;
for (int j = g; j <= max; j += g) {
c += freq[j];
}
cnt[g] = c * (c - 1) / 2;
}
long[] exact = new long[max + 1];
for (int g = max; g >= 1; g--) {
exact[g] = cnt[g];
for (int j = g * 2; j <= max; j += g) {
exact[g] -= exact[j];
}
}
long[] prefix = new long[max + 1];
for (int g = 1; g <= max; g++) {
prefix[g] = prefix[g - 1] + exact[g];
}
int[] ans = new int[queries.length];
for (int i = 0; i < queries.length; i++) {
long k = queries[i] + 1; // 1-based position
int l = 1, r = max;
while (l < r) {
int mid = (l + r) / 2;
if (prefix[mid] >= k)
r = mid;
else
l = mid + 1;
}
ans[i] = l;
}
return ans;
}
}
"""
Problem: LeetCode 3312 - Sorted GCD Pair Queries
Difficulty: Hard
Language: Python
Author: CodeByTushu
"""
import bisect
from typing import List
class Solution:
def gcdValues(self, nums: List[int], queries: List[int]) -> List[int]:
max_val = max(nums)
freq = [0] * (max_val + 1)
for x in nums:
freq[x] += 1
cnt = [0] * (max_val + 1)
for g in range(1, max_val + 1):
c = sum(freq[g::g])
cnt[g] = c * (c - 1) // 2
exact = list(cnt)
for g in range(max_val, 0, -1):
for j in range(g * 2, max_val + 1, g):
exact[g] -= exact[j]
prefix = [0] * (max_val + 1)
for g in range(1, max_val + 1):
prefix[g] = prefix[g - 1] + exact[g]
ans = []
for q in queries:
k = q + 1
idx = bisect.bisect_left(prefix, k)
ans.append(idx)
return ans
/**
* Problem: LeetCode 3312 - Sorted GCD Pair Queries
* Difficulty: Hard
* Language: C++
* Author: CodeByTushu
*/
#include <vector>
#include <algorithm>
#include <iterator>
using namespace std;
/**
* Problem: LeetCode 3312 - Sorted GCD Pair Queries
* Difficulty: Hard
* Language: C++
* Author: CodeByTushu
*/
class Solution {
public:
vector<int> gcdValues(vector<int>& nums, vector<long long>& queries) {
int max_val = *max_element(nums.begin(), nums.end());
vector<int> freq(max_val + 1, 0);
for (int x : nums) {
freq[x]++;
}
vector<long long> cnt(max_val + 1, 0);
for (int g = 1; g <= max_val; ++g) {
long long c = 0;
for (int j = g; j <= max_val; j += g) {
c += freq[j];
}
cnt[g] = c * (c - 1) / 2;
}
vector<long long> exact = cnt;
for (int g = max_val; g >= 1; --g) {
for (int j = g * 2; j <= max_val; j += g) {
exact[g] -= exact[j];
}
}
vector<long long> prefix(max_val + 1, 0);
for (int g = 1; g <= max_val; ++g) {
prefix[g] = prefix[g - 1] + exact[g];
}
vector<int> ans;
ans.reserve(queries.size());
for (long long q : queries) {
long long k = q + 1;
auto it = lower_bound(prefix.begin() + 1, prefix.end(), k);
ans.push_back(static_cast<int>(distance(prefix.begin(), it)));
}
return ans;
}
};
/**
* Problem: LeetCode 3312 - Sorted GCD Pair Queries
* Difficulty: Hard
* Language: JavaScript
* Author: CodeByTushu
*/
var gcdValues = function(nums, queries) {
let maxVal = 0;
for (let i = 0; i < nums.length; i++) {
if (nums[i] > maxVal) maxVal = nums[i];
}
const freq = new Int32Array(maxVal + 1);
for (let i = 0; i < nums.length; i++) {
freq[nums[i]]++;
}
const cnt = new Float64Array(maxVal + 1);
for (let g = 1; g <= maxVal; g++) {
let c = 0;
for (let j = g; j <= maxVal; j += g) {
c += freq[j];
}
cnt[g] = (c * (c - 1)) / 2;
}
const exact = new Float64Array(cnt);
for (let g = maxVal; g >= 1; g--) {
for (let j = g * 2; j <= maxVal; j += g) {
exact[g] -= exact[j];
}
}
const prefix = new Float64Array(maxVal + 1);
for (let g = 1; g <= maxVal; g++) {
prefix[g] = prefix[g - 1] + exact[g];
}
const m = queries.length;
const ans = new Int32Array(m);
for (let i = 0; i < m; i++) {
const k = Number(queries[i]) + 1;
let l = 1, r = maxVal;
while (l < r) {
const mid = (l + r) >> 1;
if (prefix[mid] >= k) {
r = mid;
} else {
l = mid + 1;
}
}
ans[i] = l;
}
return Array.from(ans);
};
TIME COMPLEXITY
O(N + M log M + Q log M)