The code didn’t pass 1 of the test cases.
#include<bits/stdc++.h>
#define ll long long
using namespace std;
void primes(ll p,ll n,ll x)
{
p[0]=p[1]=0;
p[2]=1;
for(ll i=3;i<=n;i=i+2)
p[i]=1;
for(ll i=3;i<=n;i++)
{
if(p[i]==1)
{
for(ll j=ii;j<=n;j=j+i)
p[j]=0;
}
}
ll j=0;
ll prime[n+1]={0};
for(ll i=0;i<=n;i++)
{
if(p[i]==1)
{
prime[j]=i;
j++;
}
}
cout<<prime[x-1];
}
int main() {
ll n=500000,p[n+1]={0},x;
cin>>x;
primes(p,n,x);
return 0;
}
Incorrect output
hello @jha.aparna17 i have made some changes in your code .
please go through the code once again and try to understand them .
i hope i have cleared your doubt .
Happy Learning !!
still the code didn’t pass the third case
the code didn’t pass all the test cases
@jha.aparna17
The problem asks you to print nth prime number where n can be as large as 500000.
The 500000th prime number is 7,368,787.
Your array is too small to accomodate that.
i made the changes but now its giving runtime error
@jha.aparna17
I think the most likely reason for this error would be that you are using too much space. Your sieve array in the code is made as a long long type array which is quite unnecessary. You only have to store 0 or 1 ( false or true) value in it so a bool array should suffice. That would save 8 times the space. Infact if you use a bitset or a vector , that space can further be brought down as you only need 1 bit , not even a byte to store boolean data.
Change the array data type and try again. If there is still some problem , share your latest code here by saving it on the IDE.
code accepted, thanks!