What can I change in my code so the TLE error will remove https://ide.codingblocks.com/s/580230
Time Limit Exceeded
Hey Arsh, you are getting a TLE as the number of test cases is very large. For each test case you are computing the prime numbers till that number and then returning the last valid prime number. The recalculation of prime numbers is causing the TLE.
To overcome this problem, let’s make an array of the prime numbers present till a large number(the maximum number in the given testcases). We can simply use the SOE(Sieve of Eratosthenes) to find the prime number upto that number and store that in a the array. After that we can simply iterate the array containing primes and check the value for given test case.
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.