Run error in prateek loves candy


plz… can u explain why i am getting run time error but my code works fine for test case given I have used sieve of eratosthenes.

Hi Akansha,
Consider if the testcases are 10^4 then your are trying to make an array of length 10^8 which is an error because 10^8 is long type where as you can give length of array as int type number only.

so, how can i overcome this problem???

Hi Akansha,
See you can observe that all the prime numbers are odd and a number is prime if it does not divisible by any number till its square root. For eg 11 is a prime number and when we round off its square root to int type is 3 and since it is not divisible by 2 and 3 so its a prime number. So you can use a loop and find all the prime numbers till the maximum N in the testcase and stored in the array. Since you had find prime numbers till the maximum N so you can easily print all the required prime numbers by just iterating over the array.

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.