Prateek Loves Candy

I’m getting timelimit error in this code

please share your code ide so that i can have a look

#include
using namespace std;
void primesieve(int p,long long n)
{
p[0]=0;
p[1]=0;
p[2]=1;
for(long long i=3;i<=n;i+=2)
{
p[i]=1;
}
for(long long i=3;i<=n;i+=2)
{
if(p[i])
{
for(long long j=i
i;j<=n;j+=2*i)
{
p[j]=0;
}
}
}
return;
}
int main()
{
long long n=100000000;
int p[n+1]={0};
long long t;
long long j=0;
cin>>t;
while(t!=0)
{
long long n;
cin>>n;
primesieve(p,100000000);
long long ans=0;
for(long long i=0;i<=100000000;i++)
{

    if(p[i]==1)
    ans++;
    if(ans==n)
    {
        j=i;
    break;
    }
}
cout<<j<<endl;
t--;
}
return 0;

}

Time limit is exceeding due to the following reasons :

  1. for every test case you are again and again calculating the prime no. ( solution => call primesieve(p,100000000) before the while loop of test case )
  2. for every test case you are iterating over p array so to find n number of prime no. ( sol => take a new array and pass it to function and while calculating the primes then only save no. of prime correcting to index of that array so that in main function in the while loop you can directly access the answer by p[n]. )

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.