The Greatest Common Divisor Traversal coding problem gives you an array of integers. You can move between index i and index j if and only if the greatest common divisor of nums[i] and nums[j] is strictly greater than 1 (meaning they share a prime factor). You need to determine if you can traverse between all pairs of indices in the array.
This is a "Hard" problem asked by Google. It tests your mastery of the Union Find (Disjoint Set) data structure combined with Number Theory (Prime Factorization). Trying to compute the GCD for every pair of numbers takes , which is too slow. It evaluates your ability to use a middleman (the prime factors) to connect indices in time.
This problem uses Union Find with Prime Factor Nodes.
nums[i], find its prime factors.p, perform a union operation between the index i and the prime factor p. This means if nums[i] and nums[j] both share prime factor p, they will end up in the same component.nums = [2, 3, 6]
(i, j) pair and checking gcd(nums[i], nums[j]) > 1.1 has no prime factors. If the array has a 1 and the array length is , traversal is immediately impossible because 1 cannot connect to anything.In graph problems where connectivity is based on a mathematical property (like common factors), treat the mathematical properties (the primes) as actual nodes in your graph.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Graph Connectivity With Threshold | Hard | Solve | |
| Largest Component Size by Common Factor | Hard | Solve | |
| GCD Sort of an Array | Hard | Solve | |
| Check If It Is a Good Array | Hard | Solve | |
| Find the Count of Numbers Which Are Not Special | Medium | Solve |