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^51 <= nums[i] <= 10^5All elements of nums are integers.Need help or want more practice?