Deepak and primes

I am getting error in 4 test cases pls help me with this
this is my code
#include<bits/stdc++.h>
using namespace std;
#define MAX 5000000
bool isPrime[MAX];
int sieveOfEratosthenes(int n){

vector<int>ans;
for(int i=0;i<=MAX;i++){
	isPrime[i]=true;
}
isPrime[0]=false;
isPrime[1]=false;
for(int i=2;i*i<=MAX;i++){
	if(isPrime[i]==true){
        ans.push_back(i);
		for(int j=i*i;j<=MAX;j+=i){
			isPrime[j]=false;
		}
	}
}
return ans[n-1];

}
int main(){
int n;
cin>>n;
int nthPrime = sieveOfEratosthenes(n);
cout<<nthPrime<<endl;
return 0;
}

Hello @avinashmallik2017,

  1. n is the input in the question with range:
    1<=n<=5000000
    Don’t you think that the 5000000th prime number is greater than 5000000?

  2. There are few more logical errors in your code.

I have modified it:

Hope, this would help.
Give a like if you are satisfied.

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.