GCD Sort of an Array - Problem

Imagine you have an array of numbers, but you can only swap two numbers if they share a common factor greater than 1 (their GCD > 1). Your goal is to determine whether you can sort this array in non-decreasing order using only these restricted swaps.

The Greatest Common Divisor (GCD) of two numbers is the largest positive integer that divides both numbers. For example:

  • gcd(6, 9) = 3 → Can swap because 3 > 1
  • gcd(4, 10) = 2 → Can swap because 2 > 1
  • gcd(7, 11) = 1 → Cannot swap because 1 is not > 1

This problem tests your understanding of connected components and number theory. Elements that can be transitively swapped through a chain of GCD relationships form a connected group, and within each group, elements can be arranged in any order.

Input & Output

example_1.py — Basic Case
$ Input: [7, 21, 3]
Output: true
💡 Note: We can swap 21 and 3 since gcd(21,3) = 3 > 1, giving us [7, 3, 21]. Then we can swap 7 and 3 since gcd(7,3) = 1... wait, this is actually false since 7 and 3 are coprime!
example_2.py — Sortable Array
$ Input: [5, 20, 3, 15, 9]
Output: true
💡 Note: Multiple swaps possible: gcd(20,15)=5, gcd(15,9)=3, etc. Through a series of valid swaps, we can achieve the sorted order [3, 5, 9, 15, 20].
example_3.py — Already Sorted
$ Input: [2, 3, 4]
Output: true
💡 Note: Array is already sorted in non-decreasing order, so no swaps needed. Return true.

Visualization

Tap to expand
GCD Sort: Friend Network Approach12[2²×3]Position 018[2×3²]Position 18[2³]Position 2Friends!gcd(12,18)=6>1Not friendsgcd(18,8)=2>1Actually friends!Transitive: 12↔18↔8Connected Component AnalysisAll three numbers are connected: {12, 18, 8}Sorted target: [8, 12, 18]✓ Component has exactly the right elements!Current: [12, 18, 8]Can rearrange freelywithin componentTarget: [8, 12, 18]Same elements,different order🎯 RESULT: TRUE - Sorting is possible!
Understanding the Visualization
1
Identify Friendships
Two numbers are 'friends' if gcd > 1 (share a prime factor)
2
Form Friend Groups
Use Union-Find to create connected components of friends
3
Check Group Capabilities
Each friend group must contain exactly the elements needed for their target positions
4
Verify Sortability
If all groups have the right elements, sorting is possible
Key Takeaway
🎯 Key Insight: Numbers that share prime factors form connected components where any arrangement is achievable through valid GCD swaps.

Time & Space Complexity

Time Complexity
⏱️
O(n³ × log(max(nums)))

For each of O(n²) iterations, we check O(n²) pairs and compute GCD in O(log(max(nums))) time

n
2n
Quadratic Growth
Space Complexity
O(n)

Space for the working copy of the array

n
2n
Linearithmic Space

Constraints

  • 1 ≤ nums.length ≤ 3 × 104
  • 2 ≤ nums[i] ≤ 105
  • All elements are at least 2 (no 1's that would make GCD always 1)
Asked in
Google 15 Meta 12 Microsoft 8 Amazon 6
28.4K Views
Medium Frequency
~25 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