2709. Greatest Common Divisor Traversal

Practice Greatest Common Divisor Traversal for free on NudgeCode: Hard, Graph pattern. Write it in Python or JavaScript, run the tests in your browser, and ask for a hint when you are stuck.

You can move directly between indices i and j (i != j) when gcd(nums[i], nums[j]) > 1, that is, when the two values share a factor greater than 1. Return true if every pair of indices can reach each other through a sequence of such moves.

Example

nums = [2,3,6]  ->  true

(0 and 1 connect through index 2, since 6 shares a factor with both 2 and 3.)

Constraints