Skip to content

Practice Sorted GCD Pair Queries

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Problem Statement

You are given an integer array nums of length n and an integer array queries. Let gcdPairs denote an array obtained by calculating the GCD of all possible pairs (nums[i], nums[j]), where 0 <= i < j < n, and then sorting these values in ascending order. For each query queries[i], you need to find the element at index queries[i] in gcdPairs. Return an integer array answer, where answer[i] is the value at gcdPairs[queries[i]] for each query. The term gcd(a, b) denotes the greatest common divisor of a and b.

Source: Sorted GCD Pair Queries on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
nums = [2,3,4], queries = [0,2,2]
Output
[1,2,2]

# Example 2

Input
nums = [4,4,2,1], queries = [5,3,1,0]
Output
[4,2,1,1]

# Example 3

Input
nums = [2,2], queries = [0,0]
Output
[2,2]

Constraints

2 <= n == nums.length <= 10^5
1 <= nums[i] <= 5 * 10^4
1 <= queries.length <= 10^5
0 <= queries[i] < n * (n - 1) / 2

Related LeetCode Problems

Practice more problems similar to Sorted GCD Pair Queries.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.