Deepak and primes doubt

#include
using namespace std;

int main() {
long long n;

cin>>n;
int p[5000002]={0};

for(long long i=3;i<5000002;i+=2){
p[i]=1;
}
for( long long int i=3;i<5000002;i+=2){
if(p[i]==1){
for(long long int j=i*i;j<5000002;j+=i){
p[j]=0;
}
}

}
p[0]=p[1]=0;
p[2]=1;
/*long long a[5000002]={0};
int k=0;
for(long long i=0;i<5000002;i++){
if(p[i]==1){
a[k]=i;

	k++;
    
}*/
int count =0;
int k;
for(int i=0;i<5000002;i++){
    count+=p[i];
    if(count==n){
        k=i;
        break;

    }
}
cout<<k<<endl;
}

whats the prblm in this code its giving segmentation fault

@deepakjumani09 firstly please always share your code using ide.codingblocks.com sharing it like this makes it completely unreadable.
Secondly, why are you making a separate array for storing the “ans” when really all you need is a single number. That is a huge amount of memory being wasted.
I have made some changes here please have a look https://ide.codingblocks.com/s/226079

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.