Math Fundamentals18 sections · 814 units
Open in Course

Practice - Greatest Common Divisor Traversal

LeetCode 2709

You are given an array nums. You can move from i to j if gcd(nums[i], nums[j]) > 1. Check if you can reach any index from any other index.

This is a graph connectivity problem where edges exist between indices with gcd > 1. Use union-find or BFS/DFS.

Key insight: if two numbers share a prime factor, they are connected. Build a graph of prime factors.