Let’s approach the problem like the sieve of Eratosthenes. For each integer, AiA_i we will find the smallest integer divisible by AiA_i which is not present in the array. The time complexity of this approach will be the sum of number of divisors for all AiA_i, which is not enough for the given constraints.

Optimization: Let’s process the array in nondecreasing order, for each AiA_i​ if we visited AiA_i for a smaller number AjA_j(AjA_j is a divisor of AiA_i), ​ we don’t need to process this AiA_i as while processing AjA_j we would have countered the result for AiA_i too. The time complexity of this approach will be the sum of number of prime divisors for all AiA_i​, which should be fast enough.

Time Complexity: O(nlog(log(Ai)))O(n*log(log(A_i)))

Space Complexity: O(n)O(n)

Statistics

44% Solution Ratio
tanvir03Earliest, Sep '22
masud_parvezppFastest, 0.0s
AndalusLightest, 25 kB
Being_GoromShortest, 464B
Toph uses cookies. By continuing you agree to our Cookie Policy.