<p>Given an integer <code>n</code>, return <em>the number of prime numbers that are strictly less than</em> <code>n</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> n = 10
<strong>Output:</strong> 4
<strong>Explanation:</strong> There are 4 prime numbers less than 10, they are 2, 3, 5, 7.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> n = 0
<strong>Output:</strong> 0
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 0
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>0 &lt;= n &lt;= 5 * 10<sup>6</sup></code></li>
</ul>

class Solution {
public:
    vector<int> SimpleSieve(int n) {
        vector<int> answer;
        vector <bool> IsPrime(n+1,true);
        IsPrime[0]=false;
        IsPrime[1]=false;
        IsPrime[n]=false;
        for(int i=2;i<n;i++){
            if(IsPrime[i]){
                answer.push_back(i);
                for(int j=2*i;j<n;j=j+i){
                    IsPrime[j]=false;
                }
            }
        }
        return answer;
    }
    int Segmented_Sieve(int n){
        int p= floor(sqrt(n))+1;
        vector<int> prime1 = SimpleSieve(p);
        int cnt=prime1.size();
        int low=p;
        int high=2*p;
        while(low<n){
            if(high>=n){
                high=n;
            }
            vector<bool> mark(p,true);
            for(int i=0;i<prime1.size();i++){
                int low_mul;
                if(low%prime1[i]==0){
                    low_mul=low;
                }
                else low_mul=(int(low/prime1[i])+1)*prime1[i];
                for(int j=low_mul;j<high;j+=prime1[i]){
                    mark[j-low]=false;
                }

            }
            for(int k=low;k<high;k++){
                if(mark[k-low]==true){
                    cnt++;
                }
            }
            low=low+p;
            high=high+p;
        }
        return cnt;
        
    }
    int countPrimes(int n){
          return Segmented_Sieve(n);
    }
};
15780