Greatest Common Divisor Traversal - Problem

Imagine you have an array of integers, and you want to determine if you can create a connected network where any two positions can reach each other through a series of jumps.

You are given a 0-indexed integer array nums. You can traverse (or jump) between index i and index j if and only if gcd(nums[i], nums[j]) > 1, where gcd is the greatest common divisor.

Your task is to determine if every pair of indices in the array can be connected through a sequence of valid traversals. In other words, can you reach any index from any other index by following the GCD rule?

Example: If nums = [2, 3, 6], you can traverse between indices 0 and 2 (since gcd(2,6)=2 > 1), and between indices 1 and 2 (since gcd(3,6)=3 > 1). This means all indices are connected through index 2.

Return true if it's possible to traverse between all pairs of indices, or false otherwise.

Input & Output

example_1.py โ€” Basic Connected Array
$ Input: nums = [2, 3, 6]
โ€บ Output: true
๐Ÿ’ก Note: We can traverse between indices 0 and 2 (gcd(2,6)=2 > 1), and between indices 1 and 2 (gcd(3,6)=3 > 1). Since index 2 connects to both 0 and 1, all indices are reachable from each other.
example_2.py โ€” Disconnected Components
$ Input: nums = [3, 9, 5]
โ€บ Output: false
๐Ÿ’ก Note: Index 0 and 1 can connect (gcd(3,9)=3 > 1), but index 2 cannot connect to either (gcd(3,5)=1, gcd(9,5)=1). The array has two separate components: {0,1} and {2}.
example_3.py โ€” Single Element Edge Case
$ Input: nums = [4]
โ€บ Output: true
๐Ÿ’ก Note: With only one element, there are no pairs to check. By definition, all elements (just one) are trivially connected.

Constraints

  • 1 โ‰ค nums.length โ‰ค 105
  • 1 โ‰ค nums[i] โ‰ค 105
  • All values in nums are positive integers
  • Arrays with length 1 should return true

Visualization

Tap to expand
Island 0Treasure: 4Island 1Treasure: 3Island 2Treasure: 12Island 3Treasure: 8GCD(4,12)=4GCD(3,12)=3GCD(4,8)=4๐Ÿ๏ธ Can all islands be connected?โœ… YES! All islands are connected through shared bridgesIsland 2 (treasure=12) acts as a hub connecting all other islands
Understanding the Visualization
1
Identify Islands
Each array index represents an island with a treasure number
2
Check Bridge Rules
Bridges can only be built between islands where GCD of treasure numbers > 1
3
Build Bridge Network
Connect all valid island pairs using Union-Find for efficiency
4
Verify Connectivity
Check if all islands belong to one connected network
Key Takeaway
๐ŸŽฏ Key Insight: Numbers sharing prime factors create connected components. Use Union-Find with prime factorization for O(nโˆšmax + n log* n) optimal solution instead of O(nยฒ) pairwise GCD checks.
Asked in
Google 12 Amazon 8 Meta 6 Microsoft 4
26.4K Views
Medium Frequency
~25 min Avg. Time
847 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