Deepak And Primes- Segmentation fault - Why?

#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main() {
ll n;
cin>>n;
ll ar[5000001]={0}, count=1;
ar[2]=2;
for(ll i=3; i<5000001; i+=2){
ar[i]=i;
}
// for(ll i=3; i<5000001; i++){
// cout<<ar[i]<<"\t";
// }

for(ll i=3; i<5000001; i+=2){
    if(ar[i]!=0){
	    count++;
	    if(count<n){
		    for(ll j=i*i; j<5000001; j+=2){
		        if(j%i==0){
			        ar[j]=0;
		        }
	        }
	    }
	    else{
		    cout<<i;
		    break;
	    }
    }
}
return 0;

}

@alien
It is given that N can be as large as 50000. 50000th prime number is a little above 86 million. Make your sieve size as 87 million just to be safe. It should work fine.

Still there is a segmentation fault

Okay works. But there is TLE now

@alien
Share your latest code.

@alien
Declare your array in global.