Time limit in my code

Kindly tell the error in my code as it is showing time limit error

Hello @Ramitgoel,

If you would look at total iterations in worst case:
t=10000 and N=1000000
So, total iterations=t*N=10^10, 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.

Modified Code:

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

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.

1 Like