mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-10 18:48:13 +08:00
61 lines
2.4 KiB
HTML
61 lines
2.4 KiB
HTML
|
<p>You are given an integer array <code>nums</code> of length <code>n</code> and an integer array <code>queries</code>.</p>
|
||
|
|
||
|
<p>Let <code>gcdPairs</code> denote an array obtained by calculating the <span data-keyword="gcd-function">GCD</span> of all possible pairs <code>(nums[i], nums[j])</code>, where <code>0 <= i < j < n</code>, and then sorting these values in <strong>ascending</strong> order.</p>
|
||
|
|
||
|
<p>For each query <code>queries[i]</code>, you need to find the element at index <code>queries[i]</code> in <code>gcdPairs</code>.</p>
|
||
|
|
||
|
<p>Return an integer array <code>answer</code>, where <code>answer[i]</code> is the value at <code>gcdPairs[queries[i]]</code> for each query.</p>
|
||
|
|
||
|
<p>The term <code>gcd(a, b)</code> denotes the <strong>greatest common divisor</strong> of <code>a</code> and <code>b</code>.</p>
|
||
|
|
||
|
<p> </p>
|
||
|
<p><strong class="example">Example 1:</strong></p>
|
||
|
|
||
|
<div class="example-block">
|
||
|
<p><strong>Input:</strong> <span class="example-io">nums = [2,3,4], queries = [0,2,2]</span></p>
|
||
|
|
||
|
<p><strong>Output:</strong> <span class="example-io">[1,2,2]</span></p>
|
||
|
|
||
|
<p><strong>Explanation:</strong></p>
|
||
|
|
||
|
<p><code>gcdPairs = [gcd(nums[0], nums[1]), gcd(nums[0], nums[2]), gcd(nums[1], nums[2])] = [1, 2, 1]</code>.</p>
|
||
|
|
||
|
<p>After sorting in ascending order, <code>gcdPairs = [1, 1, 2]</code>.</p>
|
||
|
|
||
|
<p>So, the answer is <code>[gcdPairs[queries[0]], gcdPairs[queries[1]], gcdPairs[queries[2]]] = [1, 2, 2]</code>.</p>
|
||
|
</div>
|
||
|
|
||
|
<p><strong class="example">Example 2:</strong></p>
|
||
|
|
||
|
<div class="example-block">
|
||
|
<p><strong>Input:</strong> <span class="example-io">nums = [4,4,2,1], queries = [5,3,1,0]</span></p>
|
||
|
|
||
|
<p><strong>Output:</strong> <span class="example-io">[4,2,1,1]</span></p>
|
||
|
|
||
|
<p><strong>Explanation:</strong></p>
|
||
|
|
||
|
<p><code>gcdPairs</code> sorted in ascending order is <code>[1, 1, 1, 2, 2, 4]</code>.</p>
|
||
|
</div>
|
||
|
|
||
|
<p><strong class="example">Example 3:</strong></p>
|
||
|
|
||
|
<div class="example-block">
|
||
|
<p><strong>Input:</strong> <span class="example-io">nums = [2,2], queries = [0,0]</span></p>
|
||
|
|
||
|
<p><strong>Output:</strong> <span class="example-io">[2,2]</span></p>
|
||
|
|
||
|
<p><strong>Explanation:</strong></p>
|
||
|
|
||
|
<p><code>gcdPairs = [2]</code>.</p>
|
||
|
</div>
|
||
|
|
||
|
<p> </p>
|
||
|
<p><strong>Constraints:</strong></p>
|
||
|
|
||
|
<ul>
|
||
|
<li><code>2 <= n == nums.length <= 10<sup>5</sup></code></li>
|
||
|
<li><code>1 <= nums[i] <= 5 * 10<sup>4</sup></code></li>
|
||
|
<li><code>1 <= queries.length <= 10<sup>5</sup></code></li>
|
||
|
<li><code>0 <= queries[i] < n * (n - 1) / 2</code></li>
|
||
|
</ul>
|