<p>You are given an integer array <code>nums</code>, and you can perform the following operation <strong>any</strong> number of times on <code>nums</code>:</p>

<ul>
	<li>Swap the positions of two elements <code>nums[i]</code> and <code>nums[j]</code> if <code>gcd(nums[i], nums[j]) &gt; 1</code> where <code>gcd(nums[i], nums[j])</code> is the <strong>greatest common divisor</strong> of <code>nums[i]</code> and <code>nums[j]</code>.</li>
</ul>

<p>Return <code>true</code> <em>if it is possible to sort </em><code>nums</code><em> in <strong>non-decreasing</strong> order using the above swap method, or </em><code>false</code><em> otherwise.</em></p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [7,21,3]
<strong>Output:</strong> true
<strong>Explanation:</strong> We can sort [7,21,3] by performing the following operations:
- Swap 7 and 21 because gcd(7,21) = 7. nums = [<u><strong>21</strong></u>,<u><strong>7</strong></u>,3]
- Swap 21 and 3 because gcd(21,3) = 3. nums = [<u><strong>3</strong></u>,7,<u><strong>21</strong></u>]
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [5,2,6,2]
<strong>Output:</strong> false
<strong>Explanation:</strong> It is impossible to sort the array because 5 cannot be swapped with any other element.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> nums = [10,5,9,3,15]
<strong>Output:</strong> true
We can sort [10,5,9,3,15] by performing the following operations:
- Swap 10 and 15 because gcd(10,15) = 5. nums = [<u><strong>15</strong></u>,5,9,3,<u><strong>10</strong></u>]
- Swap 15 and 3 because gcd(15,3) = 3. nums = [<u><strong>3</strong></u>,5,9,<u><strong>15</strong></u>,10]
- Swap 10 and 15 because gcd(10,15) = 5. nums = [3,5,9,<u><strong>10</strong></u>,<u><strong>15</strong></u>]
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums.length &lt;= 3 * 10<sup>4</sup></code></li>
	<li><code>2 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li>
</ul>

class Solution:
    def gcdSort(self, nums: List[int]) -> bool:
        max_num=ceil(max(nums) ** 0.5)

        parent=[i for i in range(len(nums))]
        rank=[1 for _ in range(len(nums))]
        def find(x):
            if x!=parent[x]:
                parent[x]=find(parent[x])
            return parent[x]

        def union(x,y):
            x,y=find(x),find(y)
            if x==y:return False
            if rank[x]>=rank[y]:
                parent[y]=x
                rank[x]+=rank[y]
            else:
                parent[x]=y
                rank[y]+=rank[x]
            return True
        
        def sieve_of_eratosthenes(limit):
            prime = [True] * (limit + 1)
            prime[0] = prime[1] = False

            for p in range(2, int(limit ** 0.5) + 1):
                if prime[p]:
                    for i in range(p * p, limit + 1, p):
                        prime[i] = False

            # Return a list of all prime numbers in the range [2, limit]
            return [p for p in range(2, limit + 1) if prime[p]]

        primes=sieve_of_eratosthenes(max_num)
        prime_index={}

        for i,n in enumerate(nums):
            for p in primes:
                if n%p ==0:
                    while n%p ==0 :n//=p
                    if p in prime_index:
                        union(prime_index[p],i)
                    else:
                        prime_index[p]=i
                if n==1:break
            if n>1:
                if n in prime_index:
                    union(prime_index[n],i)
                else:
                    prime_index[n]=i

        grps=defaultdict(list)
        for i in range(len(nums)):
            grps[find(i)].append(i)
        
        temp=[None]*len(nums)
        for g in grps:
            for sort_idx,org_idx in zip(sorted(grps[g],key=lambda x:nums[x]), grps[g]):
                temp[org_idx]= nums[sort_idx]
        
        return temp == sorted(nums)
1479