Tle error in printing nth prime number

this is my code for “Prateek loves candy” problem but it shows tle error in test case 5:
##include< bits/stdc++.h>
using namespace std;
bool a[1000002];
void prime()
{
memset(a, true, sizeof(a));
for(long long int i=2;ii<=1000002;i++)
{
if(a[i]==true)
{
for(long long int j=i
i;j<=1000002;j+=i)
{ a[j]=false;}
}
}
}
long long int check(long long int N)
{
long long int pos=0;
for(long long int i=2;i<=1000002;i++)
{
if(a[i]==true)
pos++;
if(pos==N)
{
return i;
}
}
}

int main() {
long long int T;
cin>>T;
prime();
while(T>0)
{
long long int N;
cin>>N;
cout<<check(N)<<endl;
T–;
}
return 0;
}

Hi @pooja_narula
You need not calculate nth prime number again and again. In prime function, just push all the prime numbers in vector v, then for each query just print v[N-1].

Link : https://ide.codingblocks.com/s/211750

Hope it Helps.

@pooja_narula
Please mark your doubt as resolved.