Can you tell an efficient way to solve this problem, using my approach I am not able to pass for all test cases.
Not able to find an efficient way
Hey
this question requires the use of the concept Sieve of Eratosthenes that is covered under the topic Number Theory . Try that.
#include #include #include using namespace std; #define N 55000001 int main() { int n; scanf("%d",&n); vector prime; vector arr(N,false); //memset(arr,false,arr.size()); arr[2]=true; for(int i=3;i<N;i+=2){ arr[i]=true; } for(int i=3;ii<N;i+=2){ if(arr[i]){ for(int j=ii;j<N;j+=i){ arr[j]=false; } } } for(int i=0;i<N;i++){ if(arr[i]) prime.push_back(i); } // for(int i=1;i<n;i++){ // printf("%d “,prime[i]); // } if(n>=prime.size()) printf(“Hi”); else printf(”%d",prime[n-1]); return 0; }
I have used sieve of eratosthenenes but still one test case is failing, because constrains are very large