Time limit exceeded in prateek loves candy problem

here is my code for the PRATEEK LOVES CANDY PROBLEM


timelimit exceeded is comming , please provide me the right solution

TESTCASE # 1 : timelimit (Time: 5.99 s)

Hi @diwakargour121,

In this question you need to use the sieve technique to deal with the test cases.

Precompute the prime numbers using sieve technique. In the sieve technique where you create a boolean array of b+1 size. And start from the first prime number i.e. 2. Take its square(4) and increment it by the number you took (i.e. 4,6,8,10 and so on). Mark all these as false since they cannot be primes. Now take the next unmarked number, 3. And mark 9,12,15 and so as false. Similarly do it for 4. 16,20,24 and so on as false. When you finish the loop, count all the positions marked as true and when count is equal to the given test case value print that index. This will be our final answer.

given- It is guaranteed that the answer does not exceed 10^6.
does it means we have to Precompute the prime numbers using sieve technique upto 10^6 in this question??

Hi @diwakargour121,
Yes. You got that right. Please respond if you face any problems.