Let’s approach the problem like the sieve of Eratosthenes. For each integer, we will find the smallest integer divisible by which is not present in the array. The time complexity of this approach will be the sum of number of divisors for all , which is not enough for the given constraints.
Optimization: Let’s process the array in nondecreasing order, for each if we visited for a smaller number ( is a divisor of ), we don’t need to process this as while processing we would have countered the result for too. The time complexity of this approach will be the sum of number of prime divisors for all , which should be fast enough.
Time Complexity:
Space Complexity: