FPEvalRepoPublic/LeetCodeMetaData
0399
1{2 "id": 3583,3 "name": "sorted-gcd-pair-queries",4 "difficulty": "Hard",5 "link": "https://leetcode.com/problems/sorted-gcd-pair-queries/",6 "date": "2024-09-29",7 "task_description": "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`. **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:** `gcdPairs` sorted 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:** `2 <= n == nums.length <= 105` `1 <= nums[i] <= 5 * 104` `1 <= queries.length <= 105` `0 <= queries[i] < n * (n - 1) / 2`",8 "test_case": [9 {10 "label": "Example 1",11 "input": "nums = [2,3,4], queries = [0,2,2]",12 "output": "[1,2,2] "13 },14 {15 "label": "Example 2",16 "input": "nums = [4,4,2,1], queries = [5,3,1,0]",17 "output": "[4,2,1,1] "18 },19 {20 "label": "Example 3",21 "input": "nums = [2,2], queries = [0,0]",22 "output": "[2,2] "23 }24 ],25 "constraints": [26 "2 <= n == nums.length <= 105",27 "1 <= nums[i] <= 5 * 104",28 "1 <= queries.length <= 105",29 "0 <= queries[i] < n * (n - 1) / 2"30 ],31 "python_template": "class Solution(object):\n def gcdValues(self, nums, queries):\n \"\"\"\n :type nums: List[int]\n :type queries: List[int]\n :rtype: List[int]\n \"\"\"\n ",32 "java_template": "class Solution {\n public int[] gcdValues(int[] nums, long[] queries) {\n \n }\n}",33 "metadata": {34 "func_name": "gcdValues"35 }36}