No output on submitting works fine in codeblocks

//shows no output
#include

using namespace std;
int main()
{ int t;
cin>>t;
for(int i=0;i<t;i++){
int n=10000;
int prime[n+1];
int k;cin>>k;
for(int i=3;i<n;i+=2){prime[i]=1;}
for(int i=3;i<n;i+=2)
{
if(prime[i])
{
for(int j=ii;j<=n;j+=2i)
{
prime[j]=0;
}

}

}
prime[2]=1;
int pr=0;
for(int i=2;i<=n;i++)
{
if(prime[i]==1){pr++;}
if(pr==k){cout<<i<<endl;break;}

}

}

}

@shubhamgh00 Please save the code on coding blocks ide and share its link.

here

@shubhamgh00 Check there are some compilation errors. Even after removing that your code is not printing the correct answer for the sample test case. Check your code once, if you are not able to correct it then relpy back,
Also i would suggest you to compute all the prime number by prime sieve before beginning the loop for test case as recomputing it again and again for each test case would be very time inefficient.

Hey, I have made some changes in code but I am not able to get the answer for n>1500, can you please point out the error.

I am attaching the code down below with some changes I made.

@shubhamgh00 In line number 5 just initialize n with 10^6 and then check.

1 Like

it is working fine in code blocks and cb ide but giving time limit on submission, also when ever I add a while loops for test case at the starting of program its lag for few second then takes input how can i remove that.please help.

@shubhamgh00 Whenever you will add a while loop in the beginning of test cases it will give Time Limit Exceeded as for every test case all prime numbers will be computed and store again and again which will make the code inefficient. So compute all the prime numbers separately only once outside the loop of test cases.

I have edited the code to compute first the prime number then added a while loop it is still showing time limit (5.82sec).Can you please check my code.

Test this code.

same time limit.

I have also tried it on codechef ide it is working fine.

Can you please explain me how is this time limit occuring ?

I tried this through my account. It is passing the test case. Please check it again and let me know.

Thank you sir It was not passing in the tab that i have opened before,i opened a new tab and submitted the code and it passed.

@shubhamgh00 Okay cool :+1: Also check out this solution. This is more efficient than your code as the additional for loop within the loop for test cases is avoided. Read out the comments.

1 Like

Thank you Sir for the code.