First line contains a single integer denoting the number of test cases T. Next T lines contains a single integer N, denoting the number of primes required

Time limit error.
Please tell what is the problem in the code.
Here is the ide link:
https://ide.codingblocks.com/s/50019

In 1s you can run max 10^8 iteration. you are running more than that i.e TLE.
Hint : Precompute the Prime numbers.
Share Problem link.

not been able to understand.
Please provide me the link of your code

Please share the Link the question , so that i can tell you more precisely the approach.

https://online.codingblocks.com/player/14664/content/4702?s=2685

please share hacker block link or screen shot, i cannot open this online course link.

Bhai question ka screenshot. complete question nhi show ho rha

vi v(1000002),cou;
void solve()
{
ll i,j;
v[2]=0;
FOI(i,2,1000000)
{
if(v[i]==0)
{
for(j=i*i;j<=1000000;j=j+i)
v[j]=1;
}
}

FOI(i,2,1000000)
if(v[i]==0)
cou.pb(i);

}
int main()
{
ll t;
solve();
cin>>t;
while(t–)
{
ll n;
cin>>n;
cout<<cou[n-1]<<"\n";
}

}

Hey Rishab, you have made many mistakes in your code

  1. while(T<=10000) this statement is wrong as you have taken T in input so put this condition as while(T > 0)
  2. you are not decrementing T anywhere so this while loop is resulting in an infinite loop which gives TLE.
  3. Use Sieve of Eratosthenes for this problem otherwise it may result in TLE as constraints are big.

https://ide.codingblocks.com/s/52860
Link to the code.

to over come TLE. I have used the sieve of Eratosthenes, using solve function i have pre calculated all the prime numbers and stored them in array “cou” and then to answer i’m taking O(1) time , so over all complexity is O(nlog log n)

Hit like if you get it.

Do tell me if you need more explanation.
Cheers
Happy coding

1 Like