Class Solution
-
- All Implemented Interfaces:
public final class Solution3312 - Sorted GCD Pair Queries.
Hard
You are given an integer array
numsof lengthnand an integer arrayqueries.Let
gcdPairsdenote an array obtained by calculating the GCD of all possible pairs(nums[i], nums[j]), where0 <= i < j < n, and then sorting these values in ascending order.For each query
queries[i], you need to find the element at indexqueries[i]ingcdPairs.Return an integer array
answer, whereanswer[i]is the value atgcdPairs[queries[i]]for each query.The term
gcd(a, b)denotes the greatest common divisor ofaandb.Example 1:
Input: nums = 2,3,4, queries = 0,2,2
Output: 1,2,2
Explanation:
gcdPairs = [gcd(nums[0], nums[1]), gcd(nums[0], nums[2]), gcd(nums[1], nums[2])] = [1, 2, 1].After sorting in ascending order,
gcdPairs = [1, 1, 2].So, the answer is
[gcdPairs[queries[0]], gcdPairs[queries[1]], gcdPairs[queries[2]]] = [1, 2, 2].Example 2:
Input: nums = 4,4,2,1, queries = 5,3,1,0
Output: 4,2,1,1
Explanation:
gcdPairssorted in ascending order is[1, 1, 1, 2, 2, 4].Example 3:
Input: nums = 2,2, queries = 0,0
Output: 2,2
Explanation:
gcdPairs = [2].Constraints:
<code>2 <= n == nums.length <= 10<sup>5</sup></code>
<code>1 <= numsi<= 5 * 10<sup>4</sup></code>
<code>1 <= queries.length <= 10<sup>5</sup></code>
0 <= queries[i] < n * (n - 1) / 2
-
-
Constructor Summary
Constructors Constructor Description Solution()
-