2709. Greatest Common Divisor Traversal

Greatest Common Divisor Traversal (LeetCode 2709) is a Hard Data Structures and Algorithms interview problem you can solve, run and submit on Thita.ai. It belongs to the Graph Traversal Patterns (DFS & BFS) pattern, in the Union-Find (Disjoint Set Union - DSU) subpattern.

How to solve Greatest Common Divisor Traversal

Greatest Common Divisor Traversal is a Union-Find problem, part of Graph Traversal Patterns (DFS & BFS). Master Union-Find data structure with path compression and union by rank for connectivity problems.

Work towards the solution in these steps:

  1. Think of positions as vertices in an undirected graph, with an edge whenever the two corresponding values have a common factor greater than 1.
  2. Connecting every pair directly would be too expensive. Instead, use prime factors as intermediate connection points.
  3. A disjoint-set union structure can merge all values that share a prime factor and then test whether all positions have the same representative.

The reference solution runs in O(M log log M + n log M · α(n + M)) time and O(n + M) space.

The Greatest Common Divisor Traversal solution and editorial walks through the full approach with reference code.

Problem statement

Implement Solution.canTraverseAllPairs, which receives an integer array nums. Treat each array position as a vertex; positions remain distinct even when their values are equal.

You may move directly between two different positions i and j when gcd(nums[i], nums[j]) is greater than 1, where gcd is the greatest common divisor. A position is reachable from another if there is a sequence of such direct moves, possibly passing through other positions.

Return true exactly when every pair of positions in nums is mutually reachable. Return false otherwise. An array with one position is considered fully reachable.

Example 1:

Input: nums = [14,25,35,77] Output: true Explanation: The values connect through shared prime factors: 14 connects to 35 through 7, 35 connects to 25 through 5, and 35 connects to 77 through 7. Thus every position belongs to one reachable component.

Example 2:

Input: nums = [6,35,77] Output: false Explanation: 35 and 77 share a factor of 7, but 6 shares no factor greater than 1 with either value, so its position is isolated.

Example 3:

Input: nums = [1] Output: true Explanation: There is only one position, so there is no other position that must be reached.

Constraints:

- 1 <= nums.length <= 10^5 - 1 <= nums[i] <= 10^5 - All elements of nums are integers.

Topics and companies

Topics: Union Find, Graph, Number Theory, Array.

How to practise Greatest Common Divisor Traversal on Thita.ai

Starter code is provided in C#, C++, Go, Java, JavaScript and Python. Your solution runs against 5 test cases for this problem, with the AI coach available for a hint when you are stuck rather than a finished answer. The reference solution runs in O(M log log M + n log M · α(n + M)) time and O(n + M) space.

Open Greatest Common Divisor Traversal in the code editor, or read the Greatest Common Divisor Traversal editorial for a worked solution with its approach and complexity analysis.

Where Greatest Common Divisor Traversal sits in the DSA pattern sheet

Problems related to Greatest Common Divisor Traversal

Other problems that use the same Union-Find (Disjoint Set Union - DSU) and Graph Traversal Patterns (DFS & BFS) techniques:

Greatest Common Divisor Traversal FAQ

What is the LeetCode number of Greatest Common Divisor Traversal?

Greatest Common Divisor Traversal is LeetCode problem 2709, rated Hard.

What pattern is used to solve Greatest Common Divisor Traversal?

Greatest Common Divisor Traversal is a Union-Find problem. It belongs to the Graph Traversal Patterns (DFS & BFS) pattern, in the Union-Find (Disjoint Set Union - DSU) subpattern.

What is the time complexity of Greatest Common Divisor Traversal?

The reference solution runs in O(M log log M + n log M · α(n + M)) time and O(n + M) space.

Preparing this workspace.