Prateek loves candy


it is showing TLE for test case 5 while all other test cases are correct.

Hello @agarwalbhavya06,

If you would look at total iterations in worst case:
You are using nested loop:
iterations of outer loop: t=10000
iterations of inner loop 90000000
So, total iterations=t*90000000=9*10^11, which is exceeding the time limit.

Solution:
Take extra memory. (say prime)
Create an array that stores the nth prime number at (n-1)th index.
i.e. prime[0]=2, prime[1]=3 and so on.

Explanation:
This will reduce the searching time to O(1) i.e. constant time.
Because now you can find the nth prime from the prime array directly eliminating the need of loop.

Hope, this would help.
Give a like if you are satisfied.

1 Like

yeah it worked
thanks @S18ML0016