the solution is very hard to understand. is there any other explanation of the solution?
Solution not clear
@Divit-Goel-2940158062683318 hey you must have studied this algorithm, sieve of Eratosthenes before in school. We are implementing the same algorithm, I can provide you a dry run, but please provide me the code you are refering to.
#include using namespace std; #define ll long long int prime(int p,int n) { for(int i=3;i<=5000000;i+=2) { p[i]=1; } for(ll i=3;i<=5000000;i+=2) { if(p[i]==1) { for(ll j=ii;j<=5000000;j+=i) { p[j]=0; } } } p[2]=1; p[1]=0; p[0]=0; int ans=0; for(ll i=1;i<=5000000;i++) { if(p[i]==1) { ans++; if(ans==n) { return i; } } } return ans; } int main() { int p[5000000]={0}; int n; cin>>n; cout<<prime(p,n)<<endl; return 0; }
i used this code( the ide one) in it iām getting 4 test cases right but 5th is showing error and 6th is showing tle
@Divit-Goel-2940158062683318 Hey make sure you array is bit larger then given constraints in case you by mistake move to edge, You have created an array of 10^6 and also accessing the position 10^6 so make and array 10^6 + 5
Second thing , the out loop can be run till i*i<=10^6
As factors occur in pair, no number can have smaller factor greater than 10^3 if it is less than equal to 10^6
If this resolves your doubt mark it as resolved.