Not very straightforward dear. If you assume that the for(int k:id[x]) runs for all N numbers everytime, what is the maximum number of times it can run?
A number can be product of <20 distinct prime numbers if A_i \ {10}^{6}. So the complexity becomes O(20*N*LogN) \equiv O(NLogN) as shown in editorial.
@vijju123 Bro, see these lines–>
for (int k : id[x])
if (k >= h)
now += ((k - h) >> 1); // sum, that we can use
for (int k : id[x])
if (k < h)
now -= (h - k); // sum, that we need
in idx[x] where x is prime, holds exponent of x in every A[i](excluding if exponent is 0)
so i guess overall complexity will be O(NLogN+k.(summation of number of element id[x] for each prime)*logH)…
if i am right, can u prove that this solution will not give TLE ?
I had answered this already in an ans on next page.
I say, lets try to get an upper bound on number of iterations. It cannot be O(N*K) as that’d mean every array element A_i is divisible by each of k primes - not possible based on my previous arguments.
At max, how many iterations can it go then? Each element A_i is divisible by <log_2N primes. So worst case calculated by me was select logN smallest primes such that their product is <{10}^{6}. Let it be X. Make an array with N-1 elements X and one element 1. That gave O(NLogN) complexity.
Calculate total number of times a prime number appears in the factorization of every array elements like in example 3,15,15,27,1024 total number of 3’s are six and 2’s are ten.
Now simply multiply all the prime numbers with count greater than or equal to number of elements+1.