Confusion about TLE in a question.

https://www.codechef.com/problems/KPRIME This is the question i was attempting.Here is my code getting TLE
https://www.codechef.com/viewsolution/16019853 .This is another solution by one @gargmehul10 https://www.codechef.com/viewsolution/15957624 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!

1 Like

Thank you very much! Doubt cleared.