Given an integer array nums of length n and an array of queries, you need to answer queries about GCD pairs.

Here's how it works:

  1. Calculate the Greatest Common Divisor (GCD) of all possible pairs (nums[i], nums[j]) where 0 ≀ i < j < n
  2. Sort all these GCD values in ascending order to form the gcdPairs array
  3. For each query queries[i], return the element at index queries[i] in the sorted gcdPairs array

Example: If nums = [2, 3, 4], the pairs are (2,3), (2,4), (3,4) with GCDs [1, 2, 1]. After sorting: [1, 1, 2].

Return an array answer where answer[i] corresponds to gcdPairs[queries[i]].

Input & Output

example_1.py β€” Basic Example
$ Input: nums = [2, 3, 4], queries = [0, 2, 2]
β€Ί Output: [1, 2, 2]
πŸ’‘ Note: Pairs are (2,3), (2,4), (3,4) with GCDs [1, 2, 1]. After sorting: [1, 1, 2]. Query 0β†’1, Query 2β†’2, Query 2β†’2.
example_2.py β€” Larger Array
$ Input: nums = [4, 4, 2, 1], queries = [5, 3, 1, 0]
β€Ί Output: [4, 2, 1, 1]
πŸ’‘ Note: 6 pairs: (4,4)β†’4, (4,2)β†’2, (4,1)β†’1, (4,2)β†’2, (4,1)β†’1, (2,1)β†’1. Sorted: [1,1,1,2,2,4]. Queries return [4,2,1,1].
example_3.py β€” Edge Case
$ Input: nums = [1, 1], queries = [0]
β€Ί Output: [1]
πŸ’‘ Note: Only one pair (1,1) with GCD=1. Query 0 returns the first (and only) element: 1.

Constraints

  • 2 ≀ nums.length ≀ 105
  • 1 ≀ nums[i] ≀ 2 Γ— 104
  • 1 ≀ queries.length ≀ 105
  • 0 ≀ queries[i] < nums.length Γ— (nums.length - 1) / 2
  • All query indices are valid (within bounds of gcdPairs array)

Visualization

Tap to expand
GCD Pair Query Optimization JourneyBrute ForceGenerate all pairsCalculate GCDsO(nΒ²) time1Frequency CountCount duplicatesReduce redundancyStill O(nΒ²)2Math InsightCount by GCDUse divisibilityO(M log M)3OptimalInclusion-ExclusionExact countsO(M log M)4Example: nums = [4, 4, 6, 8]Brute Force:6 pairs to checkCalculate each GCDFrequency:4β†’2, 6β†’1, 8β†’1Reduce some workMath Insight:Count by GCD valueNo pair generationOptimal:Exact GCD counts[1,2,2,4,4,4]Key Mathematical InsightInstead of generating O(nΒ²) pairs, count how many pairs have each GCDUse inclusion-exclusion: pairs with GCD=g = divisible by g - multiples of g
Understanding the Visualization
1
Brute Force Approach
Generate all pairs and calculate GCDs - simple but inefficient O(nΒ²)
2
Frequency Analysis
Count occurrences of each number to avoid redundant calculations
3
Mathematical Insight
Use number theory: count pairs by GCD value using divisibility
4
Inclusion-Exclusion
Remove overcounted pairs to get exact counts for each GCD
Key Takeaway
🎯 Key Insight: The breakthrough comes from realizing we don't need to generate all pairs. Instead, we can mathematically count how many pairs will have each possible GCD value, dramatically reducing complexity from O(n²) to O(M log M).
Asked in
Google 45 Amazon 32 Microsoft 28 Meta 18
23.2K Views
Medium Frequency
~35 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
πŸ’‘ Explanation
AI Ready
πŸ’‘ Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen