Why am I getting Runtime error for input being 499979 ? Could not figure out :/


  • 0
    J

    class Solution {
    public:
    int countPrimes(int n) {
    vector<bool> nums(n,true);
    int i,cnt=0;
    nums[0]=false;nums[1]=false;
    for(i=2;i<n;i++)
    {
    if(nums[i]==true)
    { cnt++;
    for(int j=i*i;j<n;j+=i)
    {
    nums[j]=false;
    }
    }
    }
    return cnt;
    }
    };


  • 0
    C

    @just95 i*i is an overflow


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.