Leetcode Problem 2709. Greatest Common Divisor Traversal
2709. Greatest Common Divisor Traversal
Leetcode Solutions
Union-Find with Sieve of Eratosthenes
Initialize the Union-Find data structure with the size of nums.
Use the Sieve of Eratosthenes to precompute the smallest prime factor for numbers up to the maximum value in nums.
Iterate over the array nums and for each number:
a. Find its prime factors using the spf array.
b. Union the current index with the indices of its prime factors in the Union-Find structure.
After processing all numbers, check if there is only one component in the Union-Find structure.
If there is only one component, return true; otherwise, return false.