Confusion about TLE in a question.


#1 This is the question i was attempting.Here is my code getting TLE .This is another solution by one @gargmehul10 getting AC with a time of 0.59 s. Even though my code uses lesser loops and we both followed the same concept, mine gets a TLE. Can someone please explain?


Reason for TLE:

The code by @gargmehul10 executes the sieve only once inside main() for all the t test cases.

Your code executes the sieve t times for t test cases.

You are calling solve() which contains the sieve for all the t test cases.

Consider putting the sieve part in one function and call it just once before answering the test cases.

I hope this cleared your doubt.

All the best!


Thank you very much! Doubt cleared.