Codefest miser travelling

can anyone tell how to approach this problem
https://www.codechef.com/FEST2021/problems/MRTRAVEL

Logic
1> First Sort the array .
2> Declare a set .
3 > After this do following for each element .
Check For every factor of number .
If :
if in set if the factor of current number already exists means we can put this element in a grp
of that element so dont do anything just break and check for next element .
Else :
if any of factor doesnot extist in the set means we need to create new grp of this element

at the end print the size of set which will be number of groups

Here is solution for above approach .
https://www.codechef.com/viewsolution/46494638