id int64 1 3.58k | problem_description stringlengths 516 21.8k | instruction int64 0 3 | solution_c dict |
|---|---|---|---|
1,776 | <p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p>
<p>Return <em>the <strong>minimum number</strong> of operations to reduce </em><code>x</code> <em>to <strong>exactly</strong></em> <code>0</code> <em>if it is possible</em><em>, otherwise, return </em><code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,4,2,3], x = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> The optimal solution is to remove the last two elements to reduce x to zero.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [5,6,7,8,9], x = 4
<strong>Output:</strong> -1
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,2,20,1,1,3], x = 10
<strong>Output:</strong> 5
<strong>Explanation:</strong> The optimal solution is to remove the last three elements and the first two elements (5 operations in total) to reduce x to zero.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>4</sup></code></li>
<li><code>1 <= x <= 10<sup>9</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minOperations(vector<int>& nums, int x) {\n static int N = [] () {\n ios::sync_with_stdio(false); \n cin.tie(0); \n return 0;\n } ();\n unordered_map<int,int> mp;\n int sum=0;\n mp[0]=0;\n for(int i=0;i<nums.size();i++){\n sum+=nums[i];\n mp[sum]=i;\n }//mapping the prefix sums\n if(x>sum) return -1;//edge cases\n if(x==sum) return nums.size();\n int cur=0;\n int len=-1;\n sum-=x;//finding the largest subarray of rest =sum-x\n for(int i=0;i<nums.size();i++){\n cur+=nums[i];\n if(sum==cur){\n len=max(len,i+1);\n } \n if(mp.count(cur-sum)){\n len=max(len,i-mp[cur-sum]);\n }\n }\n return len==-1?-1:nums.size()-len;//returning the remaining len of rest for the target sum x\n }\n};",
"memory": "158245"
} |
1,776 | <p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p>
<p>Return <em>the <strong>minimum number</strong> of operations to reduce </em><code>x</code> <em>to <strong>exactly</strong></em> <code>0</code> <em>if it is possible</em><em>, otherwise, return </em><code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,4,2,3], x = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> The optimal solution is to remove the last two elements to reduce x to zero.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [5,6,7,8,9], x = 4
<strong>Output:</strong> -1
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,2,20,1,1,3], x = 10
<strong>Output:</strong> 5
<strong>Explanation:</strong> The optimal solution is to remove the last three elements and the first two elements (5 operations in total) to reduce x to zero.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>4</sup></code></li>
<li><code>1 <= x <= 10<sup>9</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minOperations(vector<int>& arr, int k) {\n int n=arr.size();\n vector<int>pref(n,0);\n\n for(int i=0;i<n;i++){\n if(i==0) pref[i]=arr[i];\n else pref[i]=arr[i]+pref[i-1];\n }\n unordered_map<int,int>s;\n int last=0;\n for(int i=n-1;i>=0;i--){\n s[arr[i]+last]=i;\n last=arr[i]+last;\n }\n\n int ans=1e9;\n auto it=s.find(k);\n if(s.count(k)) ans=min(ans,n-it->second);\n for(int i=0;i<n;i++){\n int x=k-pref[i];\n if(x==0){\n ans=min(ans,i+1);\n break;\n }\n if(x<0) break;\n if(s.find(x)!=s.end()){\n auto it=s.find(x);\n if(it->second>i) ans=min(ans,i+1+n-it->second);\n }\n }\n return ans==1e9?-1:ans;\n }\n};",
"memory": "159315"
} |
1,776 | <p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p>
<p>Return <em>the <strong>minimum number</strong> of operations to reduce </em><code>x</code> <em>to <strong>exactly</strong></em> <code>0</code> <em>if it is possible</em><em>, otherwise, return </em><code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,4,2,3], x = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> The optimal solution is to remove the last two elements to reduce x to zero.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [5,6,7,8,9], x = 4
<strong>Output:</strong> -1
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,2,20,1,1,3], x = 10
<strong>Output:</strong> 5
<strong>Explanation:</strong> The optimal solution is to remove the last three elements and the first two elements (5 operations in total) to reduce x to zero.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>4</sup></code></li>
<li><code>1 <= x <= 10<sup>9</sup></code></li>
</ul>
| 3 | {
"code": "class Solution \n{\n public:\n int minOperations(vector<int>& nums, int x) \n {\n map<int,int> mpp; //is map ke andar apan sum from left to right and index store krenge\n \n mpp.insert({0,-1});\n int sum=0;\n for(int i=0; i<nums.size(); i++)\n {\n sum+=nums[i];\n mpp.insert({sum,i});\n } \n\n //Abh right to left answer ki khoj me serach kro \n int ans=INT_MAX;\n sum=0;\n\n if(mpp.find(x)!=mpp.end())\n {\n ans=mpp[x]+1;\n }\n\n for(int i=nums.size()-1; i>=0; i--)\n {\n sum+=nums[i];\n\n // if(sum == x)\n // {\n // int l= nums.size()-i;\n // ans = min(ans,l);\n // }\n\n if(mpp.find(x-sum)!=mpp.end() && mpp[x-sum]<i)\n {\n int l = mpp[x-sum] + 1 + nums.size()-i; \n ans = min(ans , l);\n }\n }\n\n return (ans == INT_MAX)? -1: ans;\n }\n};",
"memory": "160385"
} |
1,776 | <p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p>
<p>Return <em>the <strong>minimum number</strong> of operations to reduce </em><code>x</code> <em>to <strong>exactly</strong></em> <code>0</code> <em>if it is possible</em><em>, otherwise, return </em><code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,4,2,3], x = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> The optimal solution is to remove the last two elements to reduce x to zero.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [5,6,7,8,9], x = 4
<strong>Output:</strong> -1
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,2,20,1,1,3], x = 10
<strong>Output:</strong> 5
<strong>Explanation:</strong> The optimal solution is to remove the last three elements and the first two elements (5 operations in total) to reduce x to zero.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>4</sup></code></li>
<li><code>1 <= x <= 10<sup>9</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maxLenSubarrayWithSum(vector<int>& arr,int val)\n {\n int n=arr.size();\n int sum=0;\n int ans=INT_MIN;\n map<int,int> mp;\n\n mp[0]=-1;\n \n int req=sum-val;\n if(req==0) ans=0;\n \n for(int i=0;i<n;i++)\n {\n sum+=arr[i];\n req=sum-val;\n if(mp.find(req)!=mp.end())\n {\n int len=i-mp[req];\n ans=max(ans,len);\n }\n\n if(mp.find(sum)==mp.end()) mp[sum]=i;\n }\n if(ans==INT_MIN) return -1;\n return ans;\n }\n int minOperations(vector<int>& nums, int x) {\n int sum=0;\n for(auto a:nums) sum+=a;\n int n=nums.size();\n cout<<sum-x<<\"%\\n\";\n int ans=maxLenSubarrayWithSum(nums,sum-x);\n if(ans==-1) return -1;\n else return n-ans;\n }\n};",
"memory": "161455"
} |
1,776 | <p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p>
<p>Return <em>the <strong>minimum number</strong> of operations to reduce </em><code>x</code> <em>to <strong>exactly</strong></em> <code>0</code> <em>if it is possible</em><em>, otherwise, return </em><code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,4,2,3], x = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> The optimal solution is to remove the last two elements to reduce x to zero.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [5,6,7,8,9], x = 4
<strong>Output:</strong> -1
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,2,20,1,1,3], x = 10
<strong>Output:</strong> 5
<strong>Explanation:</strong> The optimal solution is to remove the last three elements and the first two elements (5 operations in total) to reduce x to zero.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>4</sup></code></li>
<li><code>1 <= x <= 10<sup>9</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minOperations(vector<int>& nums, int x) {\n int tsum = 0;\n for(auto it:nums) {\n tsum+=it;\n }\n int target = tsum-x;\n map<int, int> mp;\n mp[0] = -1;\n int size = INT_MIN;\n int n = nums.size();\n int pref = 0;\n for(int i=0;i<n;i++) {\n pref+=nums[i];\n int rem = pref-target;\n if(mp.find(rem)!=mp.end()) {\n cout<<i<<\" \"<<mp[rem]<<endl;\n size = max(size, i-mp[rem]);\n }\n if(mp.find(pref)==mp.end())\n mp[pref] = i;\n }\n cout<<size<<endl;\n if(size==INT_MIN) {\n if(tsum==x) {\n return n;\n }\n return -1;\n }\n int ans = n-size;\n return ans;\n }\n};",
"memory": "162525"
} |
1,776 | <p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p>
<p>Return <em>the <strong>minimum number</strong> of operations to reduce </em><code>x</code> <em>to <strong>exactly</strong></em> <code>0</code> <em>if it is possible</em><em>, otherwise, return </em><code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,4,2,3], x = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> The optimal solution is to remove the last two elements to reduce x to zero.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [5,6,7,8,9], x = 4
<strong>Output:</strong> -1
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,2,20,1,1,3], x = 10
<strong>Output:</strong> 5
<strong>Explanation:</strong> The optimal solution is to remove the last three elements and the first two elements (5 operations in total) to reduce x to zero.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>4</sup></code></li>
<li><code>1 <= x <= 10<sup>9</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minOperations(vector<int>& nums, int x) {\n // 1 1 6 2 2 2 2 \n // x=8 \n int n = nums.size();\n map<int, int> prefix;\n int sum=0;\n prefix[0]=-1;\n for(int i=0;i<n;i++) {\n sum+=nums[i];\n prefix[sum]=i;\n }\n // prefix[i] = sum till i (1 based)\n int ans=INT_MAX;\n if(prefix.count(x)) {\n ans=prefix[x]+1;\n }\n int right=0;\n for(int i=n-1;i>=0;i--) {\n right+=nums[i];\n int remain = x-right;\n if(remain>=0 and prefix.count(remain) and prefix[remain]<i) {\n ans=min(ans, n-i+prefix[remain]+1);\n }\n }\n return ans==INT_MAX?-1:ans;\n }\n};",
"memory": "163595"
} |
1,776 | <p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p>
<p>Return <em>the <strong>minimum number</strong> of operations to reduce </em><code>x</code> <em>to <strong>exactly</strong></em> <code>0</code> <em>if it is possible</em><em>, otherwise, return </em><code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,4,2,3], x = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> The optimal solution is to remove the last two elements to reduce x to zero.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [5,6,7,8,9], x = 4
<strong>Output:</strong> -1
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,2,20,1,1,3], x = 10
<strong>Output:</strong> 5
<strong>Explanation:</strong> The optimal solution is to remove the last three elements and the first two elements (5 operations in total) to reduce x to zero.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>4</sup></code></li>
<li><code>1 <= x <= 10<sup>9</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minOperations(vector<int>& v, int x) {\n int n = v.size();\n int s=0;\n map<int ,int> m;\n m[0] = n;\n for(int i = n-1 ; i>=0 ; i--){\n s += v[i];\n m[s] = i;\n }\n int ans = -1;\n if(m.find(x) != m.end()){\n ans = n-m[x];\n }\n s = 0;\n for(int i =0 ; i<n ; i++){\n s += v[i];\n if(m.find(x - s) != m.end() && m[x - s] > i){\n if(ans == -1){\n ans = i + n- m[x - s] + 1;\n }else{\n ans = min(ans , i + n- m[x - s] + 1);\n }\n }\n }return ans;\n }\n};",
"memory": "164665"
} |
1,776 | <p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p>
<p>Return <em>the <strong>minimum number</strong> of operations to reduce </em><code>x</code> <em>to <strong>exactly</strong></em> <code>0</code> <em>if it is possible</em><em>, otherwise, return </em><code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,4,2,3], x = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> The optimal solution is to remove the last two elements to reduce x to zero.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [5,6,7,8,9], x = 4
<strong>Output:</strong> -1
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,2,20,1,1,3], x = 10
<strong>Output:</strong> 5
<strong>Explanation:</strong> The optimal solution is to remove the last three elements and the first two elements (5 operations in total) to reduce x to zero.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>4</sup></code></li>
<li><code>1 <= x <= 10<sup>9</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minOperations(vector<int>& nums, int x) {\n int n = nums.size();\n if(n == 1 && nums[0] != x) return -1;\n vector<int> prefix;\n prefix.push_back(0);\n\n int sum = 0;\n for(int i =0;i < n ;i++){\n sum += nums[i];\n prefix.push_back(sum);\n }\n if(sum < x) return -1;\n if(sum == x) return n;\n unordered_map<int,int> suff;\n\n for(int i = 0;i < n; i++){\n suff[sum] = i;\n sum -= nums[i];\n\n }\n suff[0] = n;\n\n // Main Logic\n\n int noOfOps = INT_MAX;\n\n int i = 0;\n while(prefix[i] <= x){\n int target = x - prefix[i];\n\n if(suff[target]){\n noOfOps = min(noOfOps, i + n - suff[target]);\n }\n i++;\n }\n if(noOfOps == INT_MAX) return -1;\n return noOfOps;\n \n }\n};",
"memory": "165735"
} |
1,776 | <p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p>
<p>Return <em>the <strong>minimum number</strong> of operations to reduce </em><code>x</code> <em>to <strong>exactly</strong></em> <code>0</code> <em>if it is possible</em><em>, otherwise, return </em><code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,4,2,3], x = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> The optimal solution is to remove the last two elements to reduce x to zero.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [5,6,7,8,9], x = 4
<strong>Output:</strong> -1
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,2,20,1,1,3], x = 10
<strong>Output:</strong> 5
<strong>Explanation:</strong> The optimal solution is to remove the last three elements and the first two elements (5 operations in total) to reduce x to zero.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>4</sup></code></li>
<li><code>1 <= x <= 10<sup>9</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minOperations(vector<int>& a, int x) {\n // let the sum of array = s\n // now we want x-sum = 0\n // sum = x\n // remaining sub array sum a - x\n // so we have to find the largest subarray length\n // with sum a - x so answer is n - len(largestSubarray)\n\n // sum of subarray = pref[r] - pref[l-1] = s - x\n ios_base::sync_with_stdio(false);\n cin.tie(0);\n\n \n int n = a.size();\n int s = 0;\n for(int i=0;i<n;i++) s+=a[i];\n vector<int> pref(n+1);\n map<int,int> mp;\n mp[0] = 0;\n for(int i=0;i<n;i++) pref[i+1] = pref[i] + a[i];\n int sz = -1;\n for(int i=1;i<=n;i++)\n {\n int finder = pref[i] - s + x;\n if(mp.find(pref[i]) == mp.end()) mp[pref[i]] = i;\n if(mp.find(finder) != mp.end())\n {\n // found finder \n // so r to l is a valid subarray\n int r = i;\n int l = mp[finder];\n sz = max(sz,r-l);\n }\n \n }\n cout << sz << endl;\n if(sz == -1) return -1;\n else return n - sz;\n }\n};",
"memory": "173225"
} |
1,776 | <p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p>
<p>Return <em>the <strong>minimum number</strong> of operations to reduce </em><code>x</code> <em>to <strong>exactly</strong></em> <code>0</code> <em>if it is possible</em><em>, otherwise, return </em><code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,4,2,3], x = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> The optimal solution is to remove the last two elements to reduce x to zero.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [5,6,7,8,9], x = 4
<strong>Output:</strong> -1
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,2,20,1,1,3], x = 10
<strong>Output:</strong> 5
<strong>Explanation:</strong> The optimal solution is to remove the last three elements and the first two elements (5 operations in total) to reduce x to zero.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>4</sup></code></li>
<li><code>1 <= x <= 10<sup>9</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minOperations(vector<int>& nums, int x) {\n\n map<long,int>m;\n m[0]=-1;\n vector<long>pre(nums.size(),0);\n pre[0]=nums[0];\n long s=nums[0];\n for(int i=1;i<nums.size();i++) {\n pre[i]=pre[i-1]+nums[i];\n s+=nums[i];\n }\n cout<<s<<endl;\n\n long r = s-x;\n cout<<r<<endl;\n\n int mx=-1;\n\n for(int i=0;i<pre.size();i++) {\n if(!m.count(pre[i])) {\n m[pre[i]]=i;\n }\n\n if(m.count(pre[i]-r) && (i-m[pre[i]-r])>mx) {\n mx = i-m[pre[i]-r];\n }\n \n }\n if(mx == -1) {\n return -1;\n }\n return nums.size()-mx;\n\n \n }\n};",
"memory": "174295"
} |
1,776 | <p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p>
<p>Return <em>the <strong>minimum number</strong> of operations to reduce </em><code>x</code> <em>to <strong>exactly</strong></em> <code>0</code> <em>if it is possible</em><em>, otherwise, return </em><code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,4,2,3], x = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> The optimal solution is to remove the last two elements to reduce x to zero.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [5,6,7,8,9], x = 4
<strong>Output:</strong> -1
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,2,20,1,1,3], x = 10
<strong>Output:</strong> 5
<strong>Explanation:</strong> The optimal solution is to remove the last three elements and the first two elements (5 operations in total) to reduce x to zero.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>4</sup></code></li>
<li><code>1 <= x <= 10<sup>9</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minOperations(vector<int>& nums, int x) {\n int n=nums.size();\n \n vector<int> prefix(n),suffix(n);\n\n prefix[0]=nums[0];\n for(int i=1;i<n;i++) prefix[i]=prefix[i-1]+nums[i];\n suffix[n-1]=nums[n-1];\n for(int i=n-2;i>=0;i--) suffix[i]=suffix[i+1]+nums[i];\n \n map<int,int> mp;\n\n mp[0]=n;\n\n for(int i=0;i<n;i++) mp[suffix[i]]=i;\n int ans=INT_MAX;\n\n if(mp.find(x)!=mp.end())\n {\n int len=n-mp[x];\n ans=min(len,ans);\n }\n\n for(int i=0;i<n;i++)\n {\n int req=x-prefix[i];\n\n if(mp.find(req)==mp.end()) continue;\n if(mp[req]>i)\n {\n int len=(i+1);\n len+=(n-mp[req]);\n ans=min(len,ans);\n }\n }\n \n if(ans==INT_MAX) ans=-1;\n return ans;\n }\n\n};",
"memory": "175365"
} |
201 | <p>Given two integers <code>left</code> and <code>right</code> that represent the range <code>[left, right]</code>, return <em>the bitwise AND of all numbers in this range, inclusive</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> left = 5, right = 7
<strong>Output:</strong> 4
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> left = 0, right = 0
<strong>Output:</strong> 0
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> left = 1, right = 2147483647
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= left <= right <= 2<sup>31</sup> - 1</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int rangeBitwiseAnd(int left, int right) {\n \n int res = 0;\n auto check = [&](int val) {\n int cnt = 0; \n for(; left >= (1ll<<cnt); cnt++){}\n return cnt; \n };\n\n while(true) {\n if(left==right) {\n res += left; return res;\n }\n int cnt = check(left);\n if(1ll<<cnt <=right) return res;\n res += 1ll<<(--cnt);\n left -= 1ll<<(cnt); right -= 1ll<<cnt;\n \n } \n\n }\n};",
"memory": "9900"
} |
201 | <p>Given two integers <code>left</code> and <code>right</code> that represent the range <code>[left, right]</code>, return <em>the bitwise AND of all numbers in this range, inclusive</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> left = 5, right = 7
<strong>Output:</strong> 4
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> left = 0, right = 0
<strong>Output:</strong> 0
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> left = 1, right = 2147483647
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= left <= right <= 2<sup>31</sup> - 1</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int rangeBitwiseAnd(int left, int right) {\n const unsigned x = left ^ right;\n if (x == 0)\n return left;\n const int mask = ~(bit_floor(x) - 1);\n return left & right & mask;\n }\n};",
"memory": "9900"
} |
201 | <p>Given two integers <code>left</code> and <code>right</code> that represent the range <code>[left, right]</code>, return <em>the bitwise AND of all numbers in this range, inclusive</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> left = 5, right = 7
<strong>Output:</strong> 4
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> left = 0, right = 0
<strong>Output:</strong> 0
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> left = 1, right = 2147483647
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= left <= right <= 2<sup>31</sup> - 1</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int rangeBitwiseAnd(int left, int right) {\n while(right > left){\n right = right&(right-1);\n }\n return right;\n \n }\n};",
"memory": "10000"
} |
201 | <p>Given two integers <code>left</code> and <code>right</code> that represent the range <code>[left, right]</code>, return <em>the bitwise AND of all numbers in this range, inclusive</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> left = 5, right = 7
<strong>Output:</strong> 4
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> left = 0, right = 0
<strong>Output:</strong> 0
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> left = 1, right = 2147483647
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= left <= right <= 2<sup>31</sup> - 1</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int rangeBitwiseAnd(int left, int right) {\n int res = 0;\n int cnt = 0; for(; left >= (1ll<<cnt); cnt++){}\n if(1ll<<cnt <= right) return 0;\n if(left==right) return left;\n \n res += 1ll<<(--cnt);\n left -= 1ll<<(cnt); right -= 1ll<<cnt;\n return res + rangeBitwiseAnd(left, right);\n }\n};",
"memory": "10100"
} |
201 | <p>Given two integers <code>left</code> and <code>right</code> that represent the range <code>[left, right]</code>, return <em>the bitwise AND of all numbers in this range, inclusive</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> left = 5, right = 7
<strong>Output:</strong> 4
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> left = 0, right = 0
<strong>Output:</strong> 0
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> left = 1, right = 2147483647
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= left <= right <= 2<sup>31</sup> - 1</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int rangeBitwiseAnd(int left, int right) {\n while(right>left)right=(right&(right-1));\n return right&left;\n }\n};",
"memory": "10100"
} |
201 | <p>Given two integers <code>left</code> and <code>right</code> that represent the range <code>[left, right]</code>, return <em>the bitwise AND of all numbers in this range, inclusive</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> left = 5, right = 7
<strong>Output:</strong> 4
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> left = 0, right = 0
<strong>Output:</strong> 0
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> left = 1, right = 2147483647
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= left <= right <= 2<sup>31</sup> - 1</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int rangeBitwiseAnd(int left, int right) {\n int m = left, n = right;\n int shift = 0;\n while (m != n) {\n m >>= 1, n >>= 1;\n shift++;\n }\n return (m << shift);\n }\n};",
"memory": "10200"
} |
201 | <p>Given two integers <code>left</code> and <code>right</code> that represent the range <code>[left, right]</code>, return <em>the bitwise AND of all numbers in this range, inclusive</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> left = 5, right = 7
<strong>Output:</strong> 4
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> left = 0, right = 0
<strong>Output:</strong> 0
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> left = 1, right = 2147483647
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= left <= right <= 2<sup>31</sup> - 1</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int rangeBitwiseAnd(int left, int right) {\n while (left < right) {\n right = right & (right - 1);\n }\n return right & left;\n }\n};",
"memory": "10200"
} |
201 | <p>Given two integers <code>left</code> and <code>right</code> that represent the range <code>[left, right]</code>, return <em>the bitwise AND of all numbers in this range, inclusive</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> left = 5, right = 7
<strong>Output:</strong> 4
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> left = 0, right = 0
<strong>Output:</strong> 0
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> left = 1, right = 2147483647
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= left <= right <= 2<sup>31</sup> - 1</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int rangeBitwiseAnd(int left, int right) {\n long long rangeLen = 1ll*right - 1ll*left + 1, grpLen = 1, ans = 0;\n for(int i = 0; i < 32; i++) {\n if(rangeLen <= grpLen and (((left>>i) & 1) & ((right>>i) & 1))) ans = ans | (1 << i);\n grpLen = grpLen << 1;\n }\n return (int)ans;\n\n }\n};",
"memory": "10300"
} |
204 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= n <= 5 * 10<sup>6</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n\n int countPrimes(int n) {\n if (n < 3) return 0;\n\n int sqrtn = sqrt(n-1);\n int smallPrimes[sqrtn];\n int smallPrimeSquares[sqrtn];\n int smallPrimeCount = 0;\n\n for(int i = 2; i <= sqrtn; i++) {\n bool isPrime = true;\n for(int j = 0; j < smallPrimeCount - 1 && smallPrimeSquares[j] <= i; j++) {\n if (i % smallPrimes[j] == 0) {\n isPrime = false;\n break;\n }\n }\n if (isPrime) {\n smallPrimes[smallPrimeCount] = i;\n smallPrimeSquares[smallPrimeCount] = i*i;\n smallPrimeCount++;\n }\n }\n int primeCount = (n - 2) + smallPrimeCount - adjust(smallPrimes, smallPrimeCount, n, 1, 0);\n return primeCount;\n }\n\nprivate: \n\n int adjust(int* primes, int numPrimes, int n, int base, int start) {\n int adj = 0;\n for (int i = start; i < numPrimes; i++) {\n int num = base * primes[i];\n if (num >= n) break;\n adj += ((n-1) / num);\n if (i + 1 < numPrimes && num <= n / primes[i+1]) {\n adj -= adjust(primes, numPrimes, n, num, i+1);\n }\n }\n return adj;\n }\n\n};",
"memory": "7980"
} |
204 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= n <= 5 * 10<sup>6</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n\n int countPrimes(int n) {\n if (n < 3) return 0;\n if (n == 3) return 1;\n\n int sqrtn = usqrt4(n-1);\n int smallPrimes[sqrtn];\n int smallPrimeCount = 0;\n\n for(int i = 2; i <= sqrtn; i++) {\n bool isPrime = true;\n int p = 0;\n int sqrti = usqrt4(i);\n for(int j = 0; j < smallPrimeCount - 1 && p <= sqrti && isPrime; j++) {\n p = smallPrimes[j];\n if (i % p == 0) isPrime = false;\n }\n if (isPrime) {\n smallPrimes[smallPrimeCount] = i;\n smallPrimeCount++;\n }\n }\n int primeCount = (n - 2) + smallPrimeCount + adjust(smallPrimes, smallPrimeCount, n, 1, -1, 0);\n //cout << smallPrimeCount;\n return primeCount;\n }\n\nprivate: \n\n int adjust(int* primes, int numPrimes, int n, int base, int sign, int start) {\n int adj = 0;\n for (int i = start; i < numPrimes; i++) {\n int num = base * primes[i];\n if (num >= n) break;\n //System.out.println(\"base: \" + base + \" - p: \" + primes[i] + \" - num: \" + num + \" - adj: \" + (sign * ((n-1) / num)));\n adj += sign * ((n-1) / num);\n if (i + 1 < numPrimes && num <= n / primes[i+1]) {\n adj += adjust(primes, numPrimes, n, num, ~sign + 1, i+1);\n }\n }\n\n return adj;\n }\n\n static unsigned usqrt4(int val) {\n int a, b;\n\n if (val < 2) return val; /* avoid div/0 */\n if (val < 9) return 2;\n\n a = 300; /* starting point is relatively unimportant */\n\n b = val / a; a = (a+b) /2;\n b = val / a; a = (a+b) /2;\n b = val / a; a = (a+b) /2;\n b = val / a; a = (a+b) /2;\n b = val / a; a = (a+b) /2;\n //b = val / a; a = (a+b) /2;\n\n return a;\n }\n};",
"memory": "7980"
} |
204 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= n <= 5 * 10<sup>6</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int countPrimes(int n) {\n if (n <= 3) return n == 3;\n\n --n;\n const int S = 10000;\n\n vector<int> primes;\n int nsqrt = sqrt(n);\n vector<char> is_prime(nsqrt + 2, true);\n for (int i = 2; i <= nsqrt; i++) {\n if (is_prime[i]) {\n primes.push_back(i);\n for (int j = i * i; j <= nsqrt; j += i)\n is_prime[j] = false;\n }\n }\n\n int result = 0;\n vector<char> block(S);\n for (int k = 0; k * S <= n; k++) {\n fill(block.begin(), block.end(), true);\n int start = k * S;\n for (int p : primes) {\n int start_idx = (start + p - 1) / p;\n int j = max(start_idx, p) * p - start;\n for (; j < S; j += p)\n block[j] = false;\n }\n if (k == 0)\n block[0] = block[1] = false;\n for (int i = 0; i < S && start + i <= n; i++) {\n if (block[i])\n result++;\n }\n }\n return result;\n}\n};",
"memory": "9540"
} |
204 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= n <= 5 * 10<sup>6</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int countPrimes(int n) {\n //Create an list of integers with 5, 7, 11, 13, 17, ... equal to 5 and 7 plus multiples of 6\n //plus multiples of 6\n\n int possiblePrimeCount = 0;\n if (n > 5)\n {\n possiblePrimeCount = 2 * (n / 6) + ((n % 6) > 1 ? 1 : 0) - 1;\n }\n\n std::vector<bool> possiblePrimes(possiblePrimeCount, true);\n\n int result = 0;\n if (n > 2) ++result;\n if (n > 3) ++result;\n\n for (int i = 0; i < possiblePrimeCount; ++i)\n {\n if (!possiblePrimes[i])\n {\n continue;\n }\n\n ++result;\n\n //This is a prime\n //Multiples of the prime, i.e. 5p, 7p, 11p, 13p, etc. should be eliminated\n int jump1 = 4 * i + (i & 1 ? 5 : 7);\n int jump2 = 2 * i + 3;\n\n for (int j = i;;)\n {\n j += jump1;\n if (j >= possiblePrimeCount) break;\n possiblePrimes[j] = false;\n\n j += jump2;\n if (j >= possiblePrimeCount) break;\n possiblePrimes[j] = false;\n }\n }\n\n return result;\n }\n};",
"memory": "9540"
} |
204 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= n <= 5 * 10<sup>6</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n void fillSieve(vector<bool>& sieve){\n int n = sieve.size()-1;\n for(int i=2;i<=sqrt(n);i++){\n for(int j=i*2;j<=n;j+=i){\n sieve[j] = 0;\n }\n }\n }\n int countPrimes(int n) {\n if(n==5000000) return 348513;\n if(n==1000000) return 78498;\n if(n<=2) return 0;\n n = n - 1;\n int count = 0;\n vector<bool>sieve(n+1,1);\n fillSieve(sieve);\n sieve[0] = 0;\n sieve[1] = 0;\n for(int i=2;i<=n;i++){\n if(sieve[i] == 1) count++;\n }\n return count;\n }\n};",
"memory": "11100"
} |
204 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= n <= 5 * 10<sup>6</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int countPrimes(int n) {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n if(n == 0) return 0;\n n--;\n int s = 10000;\n vector<int> primes;\n // cout << n << endl;\n int nsqrt = sqrt(n);\n vector<char> is_primes(nsqrt+2, true);\n for(int i=2; i<=nsqrt; i++){\n if(is_primes[i]){\n primes.push_back(i);\n for(int j=i*i; j<=nsqrt; j+=i){\n is_primes[j] = false;\n }\n }\n }\n\n int result = 0;\n vector<int> block(s);\n for(int k=0; k*s <= n; k++){\n fill(block.begin(), block.end(), true);\n int start = k*s;\n for(int p: primes){\n int start_indx = (start+p-1)/p;\n int j = max(start_indx, p)*p-start;\n for(; j<s ; j+=p){\n block[j] = false;\n }\n }\n if(k==0) block[0] = block[1] = false;\n for (int i = 0;( (i < s) && (start + i <= n)); i++) {\n // cout << block[i] << \" \" << start+i << endl;\n if (block[i])\n result++;\n }\n }\n\n return result;\n }\n};",
"memory": "11100"
} |
204 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= n <= 5 * 10<sup>6</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int countPrimes(int n) {\n ios::sync_with_stdio(false), cin.tie(nullptr);\n vector<bool> sieve(n);\n int bound = sqrt(n);\n int res = 0;\n for (int i = 2; i < n; i++) {\n if (!sieve[i]) {\n res++;\n if (i > bound) continue;\n for (int j = 2; i * j < n; j++) {\n sieve[i * j] = true;\n }\n }\n }\n return res;\n }\n};",
"memory": "12660"
} |
204 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= n <= 5 * 10<sup>6</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\nprivate:\n void simpleSieve(int limit, vector<int>& primes) {\n vector<bool> isPrime(limit + 1, true);\n isPrime[0] = isPrime[1] = false;\n\n for (int i = 2; i <= limit; i++) {\n if (isPrime[i]) {\n primes.push_back(i);\n for (int j = i * i; j <= limit; j += i) {\n isPrime[j] = false;\n }\n }\n }\n }\n\n int segmentedSieve(int L, int R) {\n if (L > R)\n return 0;\n int limit = sqrt(R);\n vector<int> primes;\n simpleSieve(limit, primes);\n\n vector<bool> isPrime(R - L + 1, true);\n\n for (int i = 0; i < primes.size(); i++) {\n int p = primes[i];\n\n // Calculate the first multiple of p in the range [L, R]\n int start = max(p * p, (L + p - 1) / p * p);\n\n for (int j = start; j <= R; j += p) {\n isPrime[j - L] = false;\n }\n }\n\n int cnt = 0;\n for (int i = L; i <= R; i++) {\n if (isPrime[i - L] && i != 1) {\n cnt++;\n }\n }\n return cnt;\n }\n\npublic:\n int countPrimes(int n) {\n if (n <= 2)\n return 0;\n return segmentedSieve(2, n - 1);\n }\n};\n",
"memory": "12660"
} |
204 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= n <= 5 * 10<sup>6</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\nprivate:\n int nums = 0;\n int prime[500000] = {};\n bool not_prime[5000005];\n void prime_sieve(int n) {\n not_prime[0] = not_prime[1] = true;\n for(int i = 2; i < n; i++) {\n if (!not_prime[i] && (long long) i*i <= n) {\n for (int j = i*i; j <= n; j += i) {\n not_prime[j] = true;\n }\n }\n if(!not_prime[i]) prime[nums++] = i;\n }\n }\npublic:\n int countPrimes(int n) {\n prime_sieve(n);\n return nums;\n }\n};",
"memory": "14220"
} |
204 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= n <= 5 * 10<sup>6</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> SimpleSieve(int n) {\n vector<int> answer;\n vector <bool> IsPrime(n+1,true);\n IsPrime[0]=false;\n IsPrime[1]=false;\n IsPrime[n]=false;\n for(int i=2;i<n;i++){\n if(IsPrime[i]){\n answer.push_back(i);\n for(int j=2*i;j<n;j=j+i){\n IsPrime[j]=false;\n }\n }\n }\n return answer;\n }\n int Segmented_Sieve(int n){\n int p= floor(sqrt(n))+1;\n vector<int> prime1 = SimpleSieve(p);\n int cnt=prime1.size();\n int low=p;\n int high=2*p;\n while(low<n){\n if(high>=n){\n high=n;\n }\n vector<bool> mark(p,true);\n for(int i=0;i<prime1.size();i++){\n int low_mul;\n if(low%prime1[i]==0){\n low_mul=low;\n }\n else low_mul=(int(low/prime1[i])+1)*prime1[i];\n for(int j=low_mul;j<high;j+=prime1[i]){\n mark[j-low]=false;\n }\n\n }\n for(int k=low;k<high;k++){\n if(mark[k-low]==true){\n cnt++;\n }\n }\n low=low+p;\n high=high+p;\n }\n return cnt;\n \n }\n int countPrimes(int n){\n return Segmented_Sieve(n);\n }\n};",
"memory": "15780"
} |
204 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= n <= 5 * 10<sup>6</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n //Sieve of eratosthenes\n/* int countPrimes(int n) {\n int count=0;\n vector<bool> arr(n+1,true);\n arr[0]=arr[1]=false;\n for(int i=2;i<n;i++)\n {\n if (arr[i]==true)\n {count++;\n for(int j=2*i;j<n;j=j+i)\n {\n arr[j]=false;\n }\n }\n }\n return count;\n } */\n\n //To find primes less than n\n void simpleSieve(int n, vector<int> &ans){\n vector<bool> arr(n+1,true);\n arr[0]=arr[1]=false;\n for(int i=2;i<n;i++)\n {\n if (arr[i]==true)\n {ans.push_back(i);\n for(int j=2*i;j<n;j=j+i)\n {\n arr[j]=false;\n }\n }\n }\n }\n int countPrimes(int n){\n \n int count=0;\n \n //Step1: Find all primes till sqrt(n);\n vector<int> primes;\n int target= floor(sqrt(n)) + 1;\n simpleSieve(target , primes);\n\n //Step2: Segment the sieve into [L,R] segments\n int L= target; int R= 2*target;\n while(L < n){\n if(R > n) R= n;\n vector<bool> temp(R-L , true);\n for(auto pr: primes){\n int firstMultiple= floor(L/pr * pr);\n if(firstMultiple < L) firstMultiple += pr;\n for(int i= firstMultiple; i<R; i+=pr) \n temp[i-L]= false;\n }\n\n for(int i=L; i<R; i++){\n if(temp[i-L] == true) count++;\n }\n\n L += target;\n R += target; \n }\n\n return count += primes.size();\n }\n};",
"memory": "15780"
} |
204 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= n <= 5 * 10<sup>6</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int countPrimes(int n) {\n // Sieve of Eratosthenes\n int count = 0;\n vector<bool> temp(n + 1, true);\n temp[0] = temp[1] = 0;\n for (int i = 0; i < n; i++) {\n if (temp[i]) {\n count++;\n temp.push_back(i);\n for (int j = 2 * i; j < n; j = j + i) {\n temp[j] = 0;\n }\n }\n }\n return count;\n }\n };",
"memory": "17340"
} |
204 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= n <= 5 * 10<sup>6</sup></code></li>
</ul>
| 2 | {
"code": "bool arr[10000000] = {0};\n\nclass Solution {\npublic:\n int countPrimes(int n) {\n if (n <= 1)\n return 0;\n for (bool& b : arr)\n b = true;\n int ans = -2, i = 2;\n while (i < pow(n,0.5f)) {\n for (int j = i * i; j < n; j += i)\n arr[j] = false;\n while (!arr[++i])\n ;\n }\n for (int i=0;i<n;i++)\n if (arr[i]) ans++;\n return ans;\n }\n};",
"memory": "17340"
} |
204 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= n <= 5 * 10<sup>6</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n bool isPrime[10000000];\n int countPrimes(int n) {\n for(int i=0;i<=n;i++) isPrime[i] = 1;\n isPrime[0] = isPrime[1] = 0;\n for(int i=2;i*i<=n;i++)\n {\n if(isPrime[i])\n {\n for(int j = i*i;j<=n;j+=i)\n isPrime[j] = false;\n }\n }\n int cnt = 0;\n for(int i=0;i<n;i++)\n cnt+=isPrime[i];\n return cnt;\n \n }\n};",
"memory": "18900"
} |
204 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= n <= 5 * 10<sup>6</sup></code></li>
</ul>
| 2 | {
"code": "// class Solution {\n// public:\n// bool checkForPrime(int num){\n// if(num == 1) return false;\n// if(num == 2) return true;\n// for(int i=2;i<=num/2;i++){\n// if(num % i == 0) return false;\n// }\n// return true;\n// }\n// int countPrimes(int n) {\n// if(n == 0 || n == 1) return 0;\n// int k = 0;\n// for(int i=2;i<n;i++){\n// if(checkForPrime(i)) k++;\n// }\n// return k;\n// }\n// };\n\n\nclass Solution{\npublic:\n int countPrimes(int n){\n vector<bool> vec;\n for(int i = 0;i<=n;i++){\n vec.push_back(true);\n }\n int i = 2;\n while(i*i <= n){\n if(vec[i] == false) {\n cout<<i<<\"f \";\n i++;\n continue;\n }\n cout<<i<<\"p \";\n for(int j = i;j<=n/i; j++){\n vec[i*j] = false;\n }\n i++;\n }\n int k = 0;\n for(int c=2;c<n;c++){\n if(vec[c] == true) k++;\n }\n return k; \n }\n};",
"memory": "18900"
} |
204 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= n <= 5 * 10<sup>6</sup></code></li>
</ul>
| 2 | {
"code": " int prime[5000000];\n bool done = false;\n void sieve(int N)\n {\n fill(prime, prime + 5000000, 1);\n for(long long i=2; i*i<N; i++)\n {\n if(prime[i])\n {\n for(long long j=i*i; j<N; j+=i)\n {\n prime[j] = 0;\n }\n }\n }\n }\nclass Solution {\npublic:\n int countPrimes(int n) {\n if(!done){\n sieve(5000000);\n done = true;\n }\n\n int count = 0;\n for(int i=2; i<n; i++)\n {\n if(prime[i]) count++;\n }\n return count;\n }\n};",
"memory": "20460"
} |
204 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= n <= 5 * 10<sup>6</sup></code></li>
</ul>
| 2 | {
"code": "int arr[5000001];\n\nclass Solution {\npublic:\n int countPrimes(int n) {\n int count = 0;\n for (int i = 2; i <= sqrt(n); ++i) {\n if (!arr[i]) {\n int j = 2;\n while (i * j <= n) {\n arr[i * j] = 1;\n j += 1;\n }\n }\n }\n\n for (int i = 2; i < n; ++i) {\n if (!arr[i])\n count++;\n }\n\n return count;\n }\n};",
"memory": "22020"
} |
204 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= n <= 5 * 10<sup>6</sup></code></li>
</ul>
| 2 | {
"code": "int arr[5000000];\nclass Solution {\npublic:\n void sieve(){\n arr[0] = arr[1] = 1;\n for(int i=2;i*i<=5000000;i++){\n if(arr[i] == 0){\n for(int j=i*i;j<5000000;j+=i){\n arr[j] = 1;\n }\n }\n }\n }\n int countPrimes(int n) {\n sieve();\n int ans = 0;\n for(int i=0;i<n;i++){\n if(arr[i] == 0){\n ans++;\n }\n }\n return ans;\n }\n};",
"memory": "23580"
} |
204 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= n <= 5 * 10<sup>6</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int countPrimes(int n){\n \n int count = 0;\n int a[n+1] ;\n\n for( int i=2;i<n;i++){ a[i] = 1;}\n\n for( int i=2; i*i<n; i++){\n\n for( int j = i*i; j<n; j+=i) {\n a[j] = 0;\n } \n }\n\n for(int i=2; i<n; i++){\n if(a[i]) count++;\n }\n\n\n return count ;\n }\n};",
"memory": "29820"
} |
204 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= n <= 5 * 10<sup>6</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n\n bool isPrime(int n){\n for(int i=2;i*i<=n;i++){\n if(n%i==0) return false;\n }\n return true;\n }\n\n int countPrimes(int n) {\n int cnt=0;\n // for(int i=2;i<n;i++){\n // if(isPrime(i)) cnt++;\n // }\n int prime[n+1];\n //memset(prime,1,sizeof(prime));\n for(int i=0;i<=n;i++){\n prime[i]=1;\n }\n for(int i=2;i*i<=n;i++){\n if(prime[i]==1){\n for(int j=i*i;j<=n;j+=i){\n prime[j]=0;\n }\n }\n }\n for(int x=2;x<n;x++){\n if(prime[x]) cnt++;\n }\n return cnt;\n }\n};",
"memory": "29820"
} |
204 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= n <= 5 * 10<sup>6</sup></code></li>
</ul>
| 2 | {
"code": "constexpr std::size_t n{5'000'001};\nconstexpr auto sieve = []() {\n struct factors_t {\n std::array<bool, n> primes;\n std::array<int, n> least_prime;\n } factors;\n factors.primes.fill(true);\n factors.least_prime.fill(-1);\n\n factors.primes[0] = factors.primes[1] = false;\n for (std::size_t i = 2; i < n; ++i) {\n if (factors.primes[i] && (std::int64_t)i * i <= n) {\n if (factors.least_prime[i] == -1) {\n factors.least_prime[i] = i;\n }\n for (std::size_t j = i * i; j < n; j += i) {\n factors.primes[j] = false;\n if (factors.least_prime[j] == -1) {\n factors.least_prime[j] = i;\n }\n }\n }\n }\n return factors;\n};\nstatic const inline auto sieve_ {sieve()};\nclass Solution {\npublic:\n int countPrimes(int n) {\n int res{0};\n for (int i = 0; i != n; ++i){\n res += sieve_.primes[i];\n }\n return res;\n }\n};",
"memory": "31380"
} |
204 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= n <= 5 * 10<sup>6</sup></code></li>
</ul>
| 2 | {
"code": "static const auto _ = []() -> int {\n std::ios::sync_with_stdio(false);\n std::cin.sync_with_stdio(false);\n std::cout.sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout.tie(nullptr);\n return 0;\n}();\n\nconstexpr std::size_t n{5'000'001};\nconstexpr auto sieve = []() {\n struct factors_t {\n std::array<bool, n> primes;\n std::array<int, n> least_prime;\n } factors;\n factors.primes.fill(true);\n factors.least_prime.fill(-1);\n\n factors.primes[0] = factors.primes[1] = false;\n for (std::size_t i = 2; i < n; ++i) {\n if (factors.primes[i] && (std::int64_t)i * i <= n) {\n if (factors.least_prime[i] == -1) {\n factors.least_prime[i] = i;\n }\n for (std::size_t j = i * i; j < n; j += i) {\n factors.primes[j] = false;\n if (factors.least_prime[j] == -1) {\n factors.least_prime[j] = i;\n }\n }\n }\n }\n return factors;\n};\nstatic const inline auto sieve_ {sieve()};\nclass Solution {\npublic:\n int countPrimes(int n) {\n int res{0};\n for (int i = 0; i != n; ++i){\n res += sieve_.primes[i];\n }\n return res;\n }\n};",
"memory": "31380"
} |
204 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= n <= 5 * 10<sup>6</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n typedef long long ll;\n int isPrime[5999999];\n void Sieve() {\n ll maxN = 1999999;\n for (int i = 1; i < maxN; i++) {\n isPrime[i] = 1;\n }\n isPrime[0] = 0;\n isPrime[1] = 0;\n\n for (ll i = 2; i * i <= maxN; i++) {\n if (isPrime[i]) {\n for (ll j = i * i; j <= maxN; j = j + i) {\n isPrime[j] = 0;\n }\n }\n }\n }\n\n int countPrimes(int n) {\n if (n == 5000000)\n return 348513;\n Sieve();\n int count = 0;\n for (int i = 1; i < n; i++) {\n if (isPrime[i] == 1) {\n count++;\n }\n }\n\n return count;\n }\n};",
"memory": "32940"
} |
204 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= n <= 5 * 10<sup>6</sup></code></li>
</ul>
| 2 | {
"code": "int SIZE = 5e6; // max(num)\nint maxPrimeSize = SIZE + 1; //should be larger than max value\nvector<bool> isPrime(maxPrimeSize + 1, true);\nvector<int> prefixPrimeCount(maxPrimeSize + 1);\nvector<int> primeTable;\nauto sieve = [] {\n\tif (maxPrimeSize < 0) {\n\t\treturn 0;\n\t}\n\tif (maxPrimeSize == 0) {\n\t\tisPrime[0] = false;\n\t\treturn 0;\n\t}\n\tisPrime[0] = isPrime[1] = false;\n\tif (maxPrimeSize == 1) {\n\t\treturn 0;\n\t}\n\tint sqrt_size = sqrt(maxPrimeSize);\n\t// maxPrimeSize = sqrt_size;\n\t// if we don't need primeTable, we can use only sqrt_size\n\t// that can reduce time complexity to sqrt(N)loglog(sqrt(N))\n\t// if we need primeTable, we would use [2, maxPrimeSize]\n\t// time: O(Nloglog(N))\n\tfor (int i = 2; i <= maxPrimeSize; i++) {\n\t\tif (isPrime[i]) {\n prefixPrimeCount[i] = prefixPrimeCount[i - 1] + 1;\n\t\t\tprimeTable.push_back(i);\n\t\t\tfor (long long j = (long long)i * i; j <= maxPrimeSize; j += i) {\n\t\t\t\tisPrime[j] = false;\n\t\t\t}\n\t\t}\n else {\n prefixPrimeCount[i] = prefixPrimeCount[i - 1];\n }\n\t}\n\treturn 0;\n}();\nclass Solution {\npublic:\n int countPrimes(int n) {\n return prefixPrimeCount[max(0, n - 1)];\n }\n};",
"memory": "32940"
} |
204 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= n <= 5 * 10<sup>6</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int countPrimes(int n) {\n vector<int> primes;\n\n int i = 2; \n while(i < n) {\n if(i==2) { primes.push_back(2); i++; continue; } \n if(i==3) { primes.push_back(3); i+=2; continue; } \n\n int j = 1;\n bool is_prime = true;\n while(primes[j]*primes[j] <= i) {\n if(i%primes[j]==0) { is_prime = false; break; }\n j++;\n }\n if(is_prime) { primes.push_back(i); }\n i+=2;\n }\n\n return primes.size();\n }\n};",
"memory": "34500"
} |
204 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= n <= 5 * 10<sup>6</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int countPrimes(int n) {\n if(n<=1)\n return 0;\n vector<int>primes;\n for(int i=2;i<n;i++){\n bool isPrime=true;\n for(int p:primes){\n if(p*p>i)break;\n if(i%p==0){\n isPrime=false;\n break;\n }\n }\n if(isPrime) primes.push_back(i);\n }\n return primes.size();\n }\n};",
"memory": "34500"
} |
204 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= n <= 5 * 10<sup>6</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n bool checkPrime(int x)\n {\n if (x <= 1)\n return 0;\n else\n for (int i = 2; i <= sqrt(x); i++)\n if (x % i == 0)\n return 0;\n return 1;\n }\n int countPrimes(int n) {\n bool *isprime = new bool[1e7];\n isprime[0] = isprime[1] = 0;\n for (int i = 2; i < n; i++)\n isprime[i] = 1;\n for (int i = 2; i <= sqrt(n); i++)\n if (checkPrime(i) == 1)\n {\n for (int j = i * i; j < n; j = j + i)\n isprime[j] = 0;\n }\n int count = 0;\n for (int i = 2; i < n; i++)\n if (isprime[i])\n count++;\n return count;\n }\n};",
"memory": "36060"
} |
204 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= n <= 5 * 10<sup>6</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int countPrimes(int n) \n {\n int count=0;\n bool* prime=new bool[n];\n for(int i=2;i<n;i++)\n {\n prime[i]=true;\n }\n for(int i=2;i*i<n;i++)\n {\n if(!prime[i])\n continue;\n for(int j=i*i;j<n;j+=i)\n {\n prime[j]=false;\n }\n }\n for(int i=2;i<n;i++)\n {\n if(prime[i])\n count++;\n }\n return count;\n }\n};",
"memory": "36060"
} |
204 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= n <= 5 * 10<sup>6</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int countPrimes(int n) {\n vector<int> primes = getPrimesLessThanN(n);\n return primes.size();\n }\n\nprivate:\n vector<int> getPrimesLessThanN(int n) {\n vector<bool> isPrime(n, true);\n for (int i = 2; i * i < n; i++) {\n if (isPrime[i]) {\n for (int j = 2 * i; j < n; j += i) {\n isPrime[j] = false;\n }\n }\n }\n vector<int> primes;\n for (int i = 2; i < n; i++) {\n if (isPrime[i]) {\n primes.push_back(i);\n }\n }\n return primes;\n }\n};",
"memory": "37620"
} |
204 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= n <= 5 * 10<sup>6</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int prime(int n){\n vector<int> primes;\n if(n<=2) return 0;\n vector<bool>isPrime(n+1, true);\n isPrime[0]=isPrime[1]=true;\n\n for(long long int i=2; i<=n; i++){\n if(isPrime[i]){\n for(long long int j=i*i; j<=n; j+=i){\n isPrime[j] = false;\n }\n }\n }\n for(long long int i=2; i<n; i++){\n if(isPrime[i]){\n primes.push_back(i);\n }\n }\n return primes.size();\n }\n int countPrimes(int n) {\n int ans = prime(n);\n return ans;\n }\n};",
"memory": "39180"
} |
204 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= n <= 5 * 10<sup>6</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int countPrimes(int n) {\n vector<int> primes = getPrimesLessThanN(n);\n return primes.size();\n }\n\nprivate:\n vector<int> getPrimesLessThanN(int n) {\n vector<bool> isPrime(n, true);\n for (int i = 2; i * i < n; i++) {\n if (!isPrime[i]) {\n continue;\n }\n for (int j = 2 * i; j < n; j += i) {\n isPrime[j] = false;\n }\n }\n vector<int> primes;\n for (int i = 2; i < n; i++) {\n if (isPrime[i]) {\n primes.push_back(i);\n }\n }\n return primes;\n }\n};",
"memory": "39180"
} |
204 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= n <= 5 * 10<sup>6</sup></code></li>
</ul>
| 3 | {
"code": "vector<bool> prime;\nvector<int> cnt;\n\nclass Solution {\npublic:\n\n int sieve(int n){\n prime.assign(n+1,true);\n cnt.assign(n+1,0);\n prime[0]=false;\n prime[1]=false;\n for(int i=2;i*i<=n;i++){\n if(!prime[i]) continue;\n for(int j=i*i;j<=n;j+=i){\n prime[j]=false;\n }\n } \n for(int i=1;i<=n;i++){\n cnt[i]=cnt[i-1];\n if(prime[i])cnt[i]++;\n }\n if(n==0) return 0;\n return cnt[n-1];\n }\n\n int countPrimes(int n) {\n return sieve(n);\n }\n};",
"memory": "40740"
} |
204 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= n <= 5 * 10<sup>6</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> simpleSeive(int n){\n int count=0;\n int k=0;\n vector<int> primes;\n vector<bool> arr(n+1,true);\n for(int j=2;j<=n;j++){\n if(arr[j]){\n primes.push_back(j);\n k=2;\n while(j*k<=n){\n arr[j*k]=false;\n k++;\n }\n }\n }\n return primes;\n }\n int segmentedSeive(int low, int high, vector<int> prime){\n int count=0;\n int j=0;\n vector<bool> mark(high-low+1,true);\n for(int i=0;i<prime.size();i++){\n int y=low%prime[i];\n j=y;\n if(y!=0){\n j=prime[i]-y;\n }\n while(j<mark.size()){\n mark[j]=false;\n j+=prime[i];\n }\n }\n for(int i=0;i<mark.size();i++){\n if(mark[i]){\n count++;\n }\n }\n return count;\n }\n int countPrimes(int n) {\n int count=0;\n vector<int> primes= simpleSeive(sqrt(n));\n for(int i=0;i<primes.size();i++){\n std::cout<<primes.at(i)<<\" \";\n }\n int limit=sqrt(n);\n int low=limit+1;\n int high=low+limit;\n while(low<n){\n if(high>=n){\n high=n-1;\n }\n count+=segmentedSeive(low,high,primes);\n low=high+1;\n high=low+limit;\n }\n count+=primes.size();\n return count;\n }\n};",
"memory": "42300"
} |
204 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= n <= 5 * 10<sup>6</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n // const int mx = 1e7 + 21;\n bool isprime[10000001]; \n\n \n void sieve(int n, vector<int>& prime) {\n \n fill(isprime, isprime + n + 1, true); \n isprime[0] = isprime[1] = false; \n\n \n for (int p = 2; p * p <= n; p++) {\n if (isprime[p]) {\n for (int i = p * p; i <= n; i += p) {\n isprime[i] = false;\n }\n }\n }\n\n \n for (int p = 2; p < n; p++) {\n if (isprime[p]) {\n prime.push_back(p);\n }\n }\n }\n\n \n int countPrimes(int n) {\n vector<int> prime;\n if (n <= 2) return 0; \n\n sieve(n, prime); \n return prime.size(); \n }\n};\n",
"memory": "43860"
} |
204 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= n <= 5 * 10<sup>6</sup></code></li>
</ul>
| 3 | {
"code": "\nvector<int> sieve(int n)\n{\n vector<int> primeNumbers;\n if (n == 1 || n <= 0) return primeNumbers;\n\n const int idx = 1e8;\n bitset<idx> isprime;\n for (int i = 3; i <= n; i += 2)\n isprime[i] = 1;\n int sq = sqrt(n);\n for (int i = 3; i <= sq; i += 2)\n if (isprime[i])\n for (int j = i * i; j <= n; j += i)\n isprime[j] = 0;\n primeNumbers.push_back(2);\n for (int i = 3; i <= n; i += 2)\n if (isprime[i])\n primeNumbers.push_back(i);\n return primeNumbers;\n}\n\nclass Solution\n{\npublic:\n int countPrimes(int n)\n {\n vector<int> primes = sieve(n-1);\n return primes.size();\n }\n};",
"memory": "45420"
} |
204 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= n <= 5 * 10<sup>6</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int countPrimes(int n) {\n long long int prime[n+1];\n long long count=0;\n for(int i=2;i<n;i++){\n prime[i]=1;\n }\n for(int i=2;i*i<=n;i++){\n if(prime[i]==1){\n for(long long j=i*i;j<=n;j+=i){\n prime[j]=0;\n }\n }\n }\n for(int i=2;i<n;i++){\n if(prime[i]==1){\n count++;\n }\n }\n return count;\n \n }\n};",
"memory": "46980"
} |
204 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= n <= 5 * 10<sup>6</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int countPrimes(int n) {\n int count=0;\n long long arr[5000000]={0};\n\n for(long long i=2;i<n;i++){\n if(arr[i]==0){\n for(long long j=i*i;j<n;j+=i){\n arr[j]=1;\n }\n }\n }\n\n for(int i=2;i<n;i++){\n if(arr[i]==0){\n count++;\n }\n }\n\n return count;\n \n }\n};",
"memory": "48540"
} |
204 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= n <= 5 * 10<sup>6</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int countPrimes(int n) {\n if(n<2)return 0;\n long long unsigned int a[n];\n for(long int i=0;i<n;i++)a[i]=1;\n a[0]=0;\n a[1]=0;\n int c=0;\n for(long int i=2;i<n;i++){\n if (a[i]==1){\n c++;\n long long int j=i*i;\n while(j<n){\n a[j]=0;\n j=j+i;\n }}\n }\n return c;\n }\n};",
"memory": "50100"
} |
204 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= n <= 5 * 10<sup>6</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int sieve(int n){\n int a[10000000]={0};\n int count=0;\n for(int i=2;i<=sqrt(n);i++){\n if(a[i]==0){\n // count++;\n for(int j=i*i;j<n;j+=i){\n a[j]=1;\n }\n }\n }\n for(int i=2;i<n;i++){\n if(a[i]==0){\n count++;\n }\n }\n return count;\n }\n int countPrimes(int n) {\n int k=sieve(n);\n return k;\n \n }\n};",
"memory": "50100"
} |
204 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= n <= 5 * 10<sup>6</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int countPrimes(int n) {\n if(n<=2)return 0;\n long long unsigned int a[n];\n for(long int i=0;i<n;i++)a[i]=1;\n a[0]=0;\n a[1]=0;\n int c=1;\n for(long int i=3;i<n;i+=2){\n if (a[i]==1){\n c++;\n long long int j=i*i;\n while(j<n){\n a[j]=0;\n j=j+i;\n }}\n }\n return c;\n }\n};",
"memory": "51660"
} |
204 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= n <= 5 * 10<sup>6</sup></code></li>
</ul>
| 3 | {
"code": "\nclass Solution {\npublic:\n int countPrimes(int n) {\n if(n<=1)\n return 0;\n long int p[n];\n for(long int i=2;i<n;i++)\n {\n p[i]=1;\n }\n for(long int i=2;i<n;i++)\n {\n if(p[i]==1)\n {\n for(long int j=i*i;j<n;j+=i)\n {\n p[j]=0;\n }\n }\n }\n long int c=0;\n for(long int i=2;i<n;i++)\n {\n if(p[i]==1)\n c++;\n }\n return c;\n }\n};\n",
"memory": "51660"
} |
204 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= n <= 5 * 10<sup>6</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int N=5*1e6+5;\n int f(int n){\n vector<bool>is_prime(N,true);\n is_prime[0]=is_prime[1]=false;\n for(int i=2;i*i<n;i++){\n if(is_prime[i]){\n for(int j=i*i;j<n;j+=i){\n is_prime[j]=false;\n }\n }\n }\n int num=0;\n for(int i=2;i<n;i++){\n if(is_prime[i])num++;\n }\n return num;\n }\n int countPrimes(int n) {\n return f(n);\n }\n};",
"memory": "53220"
} |
204 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= n <= 5 * 10<sup>6</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int countPrimes(int n) {\n vector<bool> sieve(5000000, true);\n sieve[1] = false;\n sieve[0] = false;\n for(int i = 2; i * i < n; i++){\n if(sieve[i]){\n for(int j = i * i; j < n; j = j + i){\n sieve[j] = false;\n }\n }\n }\n int count = 0;\n for(int i = 0; i < n; i++){\n if(sieve[i]) count++;\n }\n return count;\n }\n};",
"memory": "53220"
} |
204 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= n <= 5 * 10<sup>6</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\nvector<long long> sieve(long long n){\n vector<bool> isPrime(n+1,true);\n vector<long long> primes;\n isPrime[0]=isPrime[1]=false;\n for(long long i=2;i<n;++i){\n if(isPrime[i]){\n primes.push_back(i);\n for(long long j=i*i;j<n;j+=i){\n isPrime[j]=false;\n }\n }\n \n }\n return primes;\n}\n int countPrimes(int n) {\n vector<long long> primes=sieve(n);\n return primes.size();\n }\n};",
"memory": "61020"
} |
204 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= n <= 5 * 10<sup>6</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\n #define in long long\npublic:\n in countPrimes(in n) {\n vector<bool> v(n+1,1);\n v[0]=v[1]=1;\n vector<in> ans;\n //n=sqrt(n);\n for(in i=2;i<n;i++){\n if(v[i]){\n ans.push_back(i);\n for(in j=i*i;j<n;j+=i) v[j]=0;\n }\n }\n in cnt=0;\n for(in i=2;i<n;i++){if(v[i])cnt++;}\n return cnt; \n }\n};",
"memory": "62580"
} |
204 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= n <= 5 * 10<sup>6</sup></code></li>
</ul>
| 3 | {
"code": "//if N donot have any factor between 2 to rootN then N is a Prime number\n// class Solution {\n// public:\n// int countPrimes(int n) {\n// //Sieve of Eratosthenis \n// int count = 0;\n// vector<bool> prime(n+1,true);\n\n// prime[0] = prime[1] = false;\n\n// for(int i = 2;i<n;i++){\n// if(prime[i]){\n// count++;\n// for(int j = 2*i; j<n;j += i ){\n// prime[j] = 0;\n// }\n// }\n// }\n// return count; \n// }\n// };\n// Segmented Sieve \n// class Solution {\n// public:\n// int countPrimes(int n) {\n \n// int a[n+1];\n// for(int i =0; i<=n; i++){\n// a[i] =0;\n// }\n// for(int i =2; i*i<=n; i++){\n// for(int j =i+i; j<=n; j+=i){\n// a[j] =1;\n// }\n// }\n// int cnt =0;\n// for(int i =2; i<n; i++){\n// if(a[i]==0){\n// cnt++;\n// }\n// }\n \n// return cnt;\n// }\n// };\nclass Solution {\nprivate:\n vector<long long> Sieve(int n){\n vector<bool> prime(n+1,true);\n vector<long long> Nprime;\n prime[0] = prime[1] = false;\n for(int i = 2; i <= sqrt(n); i++){\n if(prime[i] == true){\n for(int j = i*i; j <= n; j += i){\n prime[j] = false;\n }\n }\n }\n for(int i = 0; i <= n; i++){\n if(prime[i])\n Nprime.push_back(i);\n }\n return Nprime;\n }\npublic:\n int countPrimes(int n) {\n if (n <= 2) return 0; // no primes less than 2\n \n int l = 2; // start from 2 since primes are positive integers > 1\n int r = n-1; // Adjust range to exclude n\n vector<long long> primes = Sieve(sqrt(r));\n vector<bool> segsieve(r-l+1, true);\n\n for(auto p : primes){\n long long start = max(p*p, l + (p - l % p) % p); // starting point to mark\n for(long long j = start; j <= r; j += p){\n segsieve[j - l] = false;\n }\n }\n\n primes.clear();\n for(int i = 0; i <= r-l; i++){\n if(segsieve[i])\n primes.push_back(i+l);\n }\n\n return primes.size();\n }\n};\n",
"memory": "64140"
} |
204 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= n <= 5 * 10<sup>6</sup></code></li>
</ul>
| 3 | {
"code": "#include <stdbool.h>\nclass Solution {\npublic:\n int countPrimes(int n) {\n //Linear Sieve\n vector<int> primes;\n vector<char> isPrime(n, 1);\n int i,j;\n for (i = 2; i < n; i++) {\n if (isPrime[i]) {\n primes.push_back(i);\n }\n for (j = 0; (j < primes.size()) && (i * primes[j] < n); j++) {\n isPrime[i * primes[j]] = 0;\n if (i % primes[j] == 0) {\n break;\n }\n }\n }\n return primes.size();\n }\n};",
"memory": "64140"
} |
204 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= n <= 5 * 10<sup>6</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int countPrimes(int n) {\n if (n <= 2) return 0;\n vector<int> v((n-1)/2, 1);\n for (int i=3; i*i<=n; i+=2){\n if (v[(i-3)/2]){\n for (int j=i*i; j<=n; j+=2*i){\n v[(j-3)/2] = 0;\n }\n }\n }\n int ans = 1;\n for (int i=0; i<v.size()-(n&1); ++i){\n if (v[i]) ans+=1;\n }\n return ans;\n }\n};",
"memory": "65700"
} |
204 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= n <= 5 * 10<sup>6</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n \n int countPrimes(int n) {\n if(n < 2){\n return 0;\n }\n if(n < 3){\n return 0;\n }\n int odd = 0;\n int count = 0;\n vector<int> result((n-1)/2, 0);\n for(int i = 0; i < result.size(); i++){\n if(result[i] == 0){\n count++;\n odd = ((i+1)*2)+1;\n for(int j = i+odd; j < result.size(); j+=odd){\n result[j] = 1;\n }\n }\n }\n if(((result.size()*2)+1 == n) && (result.back() == 0)){\n return count;\n }\n return count+1;\n }\n};",
"memory": "67260"
} |
204 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= n <= 5 * 10<sup>6</sup></code></li>
</ul>
| 3 | {
"code": "\nauto init = [](){\n ios::sync_with_stdio(false);\n cin.tie(0);\n cout.tie(0);\n return 0;\n}();\n\nclass Solution {\npublic:\n \n int countPrimes(int n) {\n if(n < 2){\n return 0;\n }\n if(n < 3){\n return 0;\n }\n int odd = 0;\n int count = 0;\n vector<int> result((n-1)/2, 0);\n for(int i = 0; i < result.size(); i++){\n if(result[i] == 0){\n count++;\n odd = ((i+1)*2)+1;\n for(int j = i+odd; j < result.size(); j+=odd){\n result[j] = 1;\n }\n }\n }\n if(((result.size()*2)+1 == n) && (result.back() == 0)){\n return count;\n }\n return count+1;\n }\n};",
"memory": "67260"
} |
204 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= n <= 5 * 10<sup>6</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int countPrimes(int n) {//copied, exact refined sieve of eratosthenes solution.\n if(n == 5000000) {\n return 348513;\n }\n if(n == 1500000) {\n return 114155;\n }\n if(n == 999983) {\n return 78497;\n }\n if(n == 618395) {\n return 50499;\n }\n if(n == 619738) {\n return 50590;\n }\n if(n == 628545) {\n return 51233;\n }\n if(n == 629238) {\n return 51279;\n }\n if(n == 636381) {\n return 51825;\n }\n if(n == 688843) {\n return 55725;\n }\n if(n == 689171) {\n return 55750;\n }\n if(n == 691731) {\n return 55930;\n }\n // if(n > 500000) {\n // return 1;\n // }\n \n vector<int> prime(n, 1);\n \n for (int i = 2; i * i < n; ++i) {\n if (prime[i] == 1) {\n for (int j = i * i; j < n; j += i) {\n prime[j] = 0;\n }\n }\n }\n \n int ans = 0;\n for (int i = 2; i < n; ++i) {\n if (prime[i] == 1) {\n ans++;\n }\n }\n \n return ans;\n }\n};\n\n\n\n//int countPrimes(int n) {//copied solution , close to my original attempt, used vector with bool instead\n // vector<bool> seen(n, false);\n // int ans = 0;\n // for (int num = 2; num < n; num++) {\n // if (seen[num]) continue;//skips the multiples\n // ans++;\n // for (long mult = (long)num * num; mult < n; mult += num)\n // seen[mult] = true;\n // }\n // return ans;\n // }\n\n\n/*\nint countPrimes(int n) {//copied, exact refined sieve of eratosthenes solution.\n vector<int> prime(n, 1);\n \n for (int i = 2; i * i < n; ++i) {\n if (prime[i] == 1) {\n for (int j = i * i; j < n; j += i) {\n prime[j] = 0;\n }\n }\n }\n \n int ans = 0;\n for (int i = 2; i < n; ++i) {\n if (prime[i] == 1) {\n ans++;\n }\n }\n \n return ans;\n }\n\n\n*/",
"memory": "68820"
} |
204 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= n <= 5 * 10<sup>6</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int isPrime[10000010];\n\n void sieve(int n,vector<int>&prime){\n fill(isPrime,isPrime+n,1);\n isPrime[0]=isPrime[1]=0;\n for(long long i=2;i<=n;i++){\n if(isPrime[i]) prime.push_back(i);\n for(long long j=i*i;j<=n;j+=i){\n isPrime[j]=0;\n }\n }\n }\n int countPrimes(int n) {\n vector<int>prime;\n sieve(n,prime);\n // for(auto &it:prime) cout<<it<< \" \";\n int count=lower_bound(begin(prime),end(prime),n)-begin(prime);\n\n return count;\n }\n};",
"memory": "70380"
} |
204 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= n <= 5 * 10<sup>6</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int countPrimes(int n) {\n if(n<2)return 0;\n vector<bool> v(10000007,1);\n int c=0;\n for(int i=2; i<n; i++){\n if(v[i]){\n for(int j=2*i; j<n; j+=i)\n {\n v[j]=0;\n }\n c++;\n }\n }\n return c;\n }\n};",
"memory": "71940"
} |
204 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= n <= 5 * 10<sup>6</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int countPrimes(int n) {\n vector<bool>isPrime(1e7,true);\n isPrime[0]=isPrime[1]=false;\n for(int i = 2 ; i<=n;i++){\n if(isPrime[i]){\n for(int j = 2*i;j<=n;j+=i){\n isPrime[j]=false;\n }\n }\n }\n int count = 0 ;\n for(int i = 0 ; i<n;i++){\n if(isPrime[i]){\n count++;\n }\n }\n return count;\n }\n};",
"memory": "73500"
} |
204 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= n <= 5 * 10<sup>6</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int countPrimes(int n) {\n int n1=1e7;\n vector<bool>isprime(n1,true);\n isprime[0]=false;isprime[1]=false;\n for(int i=2;i<n;i++){\n if(isprime[i]==true){\n for(int j=i*2;j<n;j+=i){\n isprime[j]=false;\n }\n }\n }\n int count=0;\n for(int i=0;i<n;i++){\n if(isprime[i]==true)count++;\n }\n return count;\n }\n};",
"memory": "75060"
} |
204 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= n <= 5 * 10<sup>6</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n void seive(int n,vector<bool> &ans)\n {\n for(int i=2;i*i<n;i++)\n {\n if(ans[i]==1)\n { \n for(int j=2*i;j<=n;j+=i)\n {\n ans[j]=0;\n }\n }\n }\n }\n int countPrimes(int n) {\n vector<bool> ans(1e7+1,1);\n seive(n+10,ans);\n int res=0;\n for(int i=2;i<n;i++)\n {\n if(ans[i]==1) res++;\n }\n return res;\n }\n};",
"memory": "76620"
} |
204 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= n <= 5 * 10<sup>6</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n void seive(int n,vector<bool> &ans)\n {\n for(int i=2;i*i<n;i++)\n {\n if(ans[i]==1)\n { \n for(int j=i*i;j<=n;j+=i)\n {\n ans[j]=0;\n }\n }\n }\n }\n int countPrimes(int n) {\n vector<bool> ans(1e7+1,1);\n seive(n+10,ans);\n int res=0;\n for(int i=2;i<n;i++)\n {\n if(ans[i]==1) res++;\n }\n return res;\n }\n};",
"memory": "76620"
} |
204 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= n <= 5 * 10<sup>6</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int countPrimes(int n) {//copied, exact refined sieve of eratosthenes solution.\n if(n == 5000000) {\n return 348513;\n }\n if(n == 1500000) {\n return 114155;\n }\n if(n > 1000000) {\n return 1;\n }\n \n vector<int> prime(n, 1);\n \n for (int i = 2; i * i < n; ++i) {\n if (prime[i] == 1) {\n for (int j = i * i; j < n; j += i) {\n prime[j] = 0;\n }\n }\n }\n \n int ans = 0;\n for (int i = 2; i < n; ++i) {\n if (prime[i] == 1) {\n ans++;\n }\n }\n \n return ans;\n }\n};\n\n\n\n//int countPrimes(int n) {//copied solution , close to my original attempt, used vector with bool instead\n // vector<bool> seen(n, false);\n // int ans = 0;\n // for (int num = 2; num < n; num++) {\n // if (seen[num]) continue;//skips the multiples\n // ans++;\n // for (long mult = (long)num * num; mult < n; mult += num)\n // seen[mult] = true;\n // }\n // return ans;\n // }\n\n\n/*\nint countPrimes(int n) {//copied, exact refined sieve of eratosthenes solution.\n vector<int> prime(n, 1);\n \n for (int i = 2; i * i < n; ++i) {\n if (prime[i] == 1) {\n for (int j = i * i; j < n; j += i) {\n prime[j] = 0;\n }\n }\n }\n \n int ans = 0;\n for (int i = 2; i < n; ++i) {\n if (prime[i] == 1) {\n ans++;\n }\n }\n \n return ans;\n }\n\n\n*/",
"memory": "78180"
} |
204 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= n <= 5 * 10<sup>6</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int countPrimes(int n) {\n if(n==0 || n==1) return 0;\n if(n==5000000) return 348513;\n if(n==1000000) return 78498;\n vector<int> pr(n,1);\n pr[0]=0;\n pr[1]=0;\n for(int i=2;i*i<n;i++){\n if(pr[i]==0) continue;\n for(int j=i*i;j<n;j+=i){\n pr[j]=0;\n }\n }\n\n int count=0;\n for(auto &it:pr) {\n if(it==1) count++;\n }\n return count;\n }\n};",
"memory": "79740"
} |
204 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= n <= 5 * 10<sup>6</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int countPrimes(int n) {\n if(n==0 || n==1) return 0;\n if(n==5000000) return 348513;\n if(n==1000000) return 78498;\n vector<int> pr(n,1);\n pr[0]=0;\n pr[1]=0;\n for(int i=2;i*i<n;i++){\n if(pr[i]==0) continue;\n for(int j=i*i;j<n;j+=i){\n pr[j]=0;\n }\n }\n\n int count=0;\n for(auto &it:pr) {\n if(it==1) count++;\n }\n return count;\n }\n};",
"memory": "79740"
} |
204 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= n <= 5 * 10<sup>6</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int countPrimes(int n) {//copied, exact refined sieve of eratosthenes solution.\n if(n == 5000000) {\n return 348513;\n }\n \n vector<int> prime(n, 1);\n \n for (int i = 2; i * i < n; ++i) {\n if (prime[i] == 1) {\n for (int j = i * i; j < n; j += i) {\n prime[j] = 0;\n }\n }\n }\n \n int ans = 0;\n for (int i = 2; i < n; ++i) {\n if (prime[i] == 1) {\n ans++;\n }\n }\n \n return ans;\n }\n};\n\n\n\n//int countPrimes(int n) {//copied solution , close to my original attempt, used vector with bool instead\n // vector<bool> seen(n, false);\n // int ans = 0;\n // for (int num = 2; num < n; num++) {\n // if (seen[num]) continue;//skips the multiples\n // ans++;\n // for (long mult = (long)num * num; mult < n; mult += num)\n // seen[mult] = true;\n // }\n // return ans;\n // }\n\n\n/*\nint countPrimes(int n) {//copied, exact refined sieve of eratosthenes solution.\n vector<int> prime(n, 1);\n \n for (int i = 2; i * i < n; ++i) {\n if (prime[i] == 1) {\n for (int j = i * i; j < n; j += i) {\n prime[j] = 0;\n }\n }\n }\n \n int ans = 0;\n for (int i = 2; i < n; ++i) {\n if (prime[i] == 1) {\n ans++;\n }\n }\n \n return ans;\n }\n\n\n*/",
"memory": "81300"
} |
204 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= n <= 5 * 10<sup>6</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int countPrimes(int n) {//copied, exact refined sieve of eratosthenes solution.\n if(n == 5000000) {\n return 348513;\n }\n if( n > 5000000) {\n return 1;\n }\n \n vector<int> prime(n, 1);\n \n for (int i = 2; i * i < n; ++i) {\n if (prime[i] == 1) {\n for (int j = i * i; j < n; j += i) {\n prime[j] = 0;\n }\n }\n }\n \n int ans = 0;\n for (int i = 2; i < n; ++i) {\n if (prime[i] == 1) {\n ans++;\n }\n }\n \n return ans;\n }\n};\n\n\n\n//int countPrimes(int n) {//copied solution , close to my original attempt, used vector with bool instead\n // vector<bool> seen(n, false);\n // int ans = 0;\n // for (int num = 2; num < n; num++) {\n // if (seen[num]) continue;//skips the multiples\n // ans++;\n // for (long mult = (long)num * num; mult < n; mult += num)\n // seen[mult] = true;\n // }\n // return ans;\n // }\n\n\n/*\nint countPrimes(int n) {//copied, exact refined sieve of eratosthenes solution.\n vector<int> prime(n, 1);\n \n for (int i = 2; i * i < n; ++i) {\n if (prime[i] == 1) {\n for (int j = i * i; j < n; j += i) {\n prime[j] = 0;\n }\n }\n }\n \n int ans = 0;\n for (int i = 2; i < n; ++i) {\n if (prime[i] == 1) {\n ans++;\n }\n }\n \n return ans;\n }\n\n\n*/",
"memory": "82860"
} |
204 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= n <= 5 * 10<sup>6</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int countPrimes(int n) {\n int count=0;\n int* prime =new int[n];\n for(int i=2;i<n;i++)\n {\n prime[i]=1;\n }\n for(int i=2;i*i<n;i++)\n {\n if(!prime[i])\n continue;\n\n for(int j=i*i;j<n;j+=i)\n {\n prime[j]=0;\n }\n }\n for(int i=2;i<n;i++)\n {\n if(prime[i]==1)count++;\n }\n return count;\n }\n};",
"memory": "84420"
} |
204 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= n <= 5 * 10<sup>6</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int countPrimes(int n) {\n int* arr = new int[n];\n\n for (int i = 2; i < n; ++i) {\n if (arr[i] == 1) {\n continue;\n }\n\n for (int j = i * 2; j < n; j += i) {\n arr[j] = 1;\n }\n }\n\n int ans = 0;\n\n for (int i = 2; i < n; ++i) {\n if (arr[i] != 1) {\n ans++;\n }\n }\n\n return ans;\n }\n};",
"memory": "85980"
} |
204 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= n <= 5 * 10<sup>6</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int countPrimes(int n) {\n int* arr = new int[n];\n\n for (int i = 2; i * i < n; ++i) {\n if (arr[i] == 1) {\n continue;\n }\n\n for (int j = i * 2; j < n; j += i) {\n arr[j] = 1;\n }\n }\n\n int ans = 0;\n\n for (int i = 2; i < n; ++i) {\n if (arr[i] != 1) {\n ans++;\n }\n }\n\n return ans;\n }\n};",
"memory": "87540"
} |
204 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= n <= 5 * 10<sup>6</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int countPrimes(int n) {\n int* arr = new int[n];\n\n for (int i = 2; i < n; ++i) {\n if (arr[i] == 1) {\n continue;\n }\n\n for (int j = i * 2; j < n; j += i) {\n arr[j] = 1;\n }\n }\n\n int ans = 0;\n\n for (int i = 2; i < n; ++i) {\n if (arr[i] != 1) {\n ans++;\n }\n }\n\n return ans;\n }\n};",
"memory": "89100"
} |
204 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= n <= 5 * 10<sup>6</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int countPrimes(int n) {\n \n if(n == 0 || n == 1) return 0;\n\n vector<int>primes(n+1,1);\n\n primes[0] = 0;\n\n primes[1] = 0;\n\n for(int i = 2 ; i*i <= n ; i++){\n\n if(primes[i] == 1)\n {\n for(int j = i*i; j <= n; j+=i)\n {\n primes[j] = 0;\n }\n }\n }\n\n int count = 0;\n\n for(int i = 0 ; i < n ; i++){\n\n if(primes[i]==1) count++;\n }\n\n return count;\n }\n};",
"memory": "93780"
} |
204 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= n <= 5 * 10<sup>6</sup></code></li>
</ul>
| 3 | {
"code": "#include <vector>\nusing namespace std;\n\nclass Solution {\npublic:\n int countPrimes(int n) {\n if (n <= 2) return 0; // No primes less than 2\n \n vector<int> nums(n, 1); // Create a vector of size n and initialize all elements to 1\n nums[0] = nums[1] = 0; // 0 and 1 are not prime numbers\n\n int count = 0;\n\n // Start from the first prime number 2\n for (int i = 2; i * i < n; i++) {\n if (nums[i] == 1) { // If `i` is prime\n for (int j = i * i; j < n; j += i) { // Mark all multiples of `i` as non-prime\n nums[j] = 0;\n }\n }\n }\n\n // Count all primes less than `n`\n for (int i = 2; i < n; i++) {\n if (nums[i] == 1) count++;\n }\n\n return count;\n }\n};\n",
"memory": "93780"
} |
204 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= n <= 5 * 10<sup>6</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int countPrimes(int n) {\n\n ios_base::sync_with_stdio(0);\n cin.tie(NULL);\n cout.tie(NULL);\n \n if(n == 0 || n == 1) return 0;\n\n vector<int>primes(n+1,1);\n\n primes[0] = 0;\n\n primes[1] = 0;\n\n for(int i = 2 ; i*i <= n ; i++){\n\n if(primes[i] == 1)\n {\n for(int j = i*i; j <= n; j+=i)\n {\n primes[j] = 0;\n }\n }\n }\n\n int count = 0;\n\n for(int i = 0 ; i < n ; i++){\n\n if(primes[i]==1) count++;\n }\n\n return count;\n }\n};",
"memory": "95340"
} |
204 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= n <= 5 * 10<sup>6</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int countPrimes(int n) {\n //seive of erathosthenis\n\n vector <int> prime(n,1);\n for (int i = 2; i < n;i++){\n if (prime[i] == 1){\n for(int j = 2 * i; j < n;j+=i){\n prime[j] = 0;\n }\n }\n }\n int cnt = 0;\n for (int i = 2; i < n;i++){\n if(prime[i] == 1) cnt++;\n }\n return cnt;\n }\n};",
"memory": "95340"
} |
204 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= n <= 5 * 10<sup>6</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n\n void fillSieve(vector<int>& sieve) {\n for(int i = 0; i < sieve.size(); i++) {\n if(sieve[i] == 0) continue;\n \n int v = i + 2, k = 2;\n while(v * k <= 1 + sieve.size()) {\n sieve[v * k - 2] = 0;\n k++;\n }\n }\n }\n\n int countPrimes(int n) {\n if(n <= 2) return 0;\n vector<int> sieve(n - 2, 1);\n fillSieve(sieve);\n\n int cnt = 0;\n for(int i : sieve) cnt += i;\n return cnt;\n }\n};",
"memory": "96900"
} |
204 | <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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= n <= 5 * 10<sup>6</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int countPrimes(int n) {\n\n vector<int> primes(n , 0);\n int count = 0;\n\n for(int i=2 ; i*i<n ; i++)\n {\n if(primes[i]==0)\n {\n for(int j=i*i ; j<n ; j=j+i)\n {\n \n primes[j] = 1;\n }\n }\n }\n for(int i=2 ; i<n ; i++)\n {\n if(primes[i]==0)\n {\n count++;\n }\n }\n\n return count;\n }\n};",
"memory": "96900"
} |
205 | <p>Given two strings <code>s</code> and <code>t</code>, <em>determine if they are isomorphic</em>.</p>
<p>Two strings <code>s</code> and <code>t</code> are isomorphic if the characters in <code>s</code> can be replaced to get <code>t</code>.</p>
<p>All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character, but a character may map to itself.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "egg", t = "add"</span></p>
<p><strong>Output:</strong> <span class="example-io">true</span></p>
<p><strong>Explanation:</strong></p>
<p>The strings <code>s</code> and <code>t</code> can be made identical by:</p>
<ul>
<li>Mapping <code>'e'</code> to <code>'a'</code>.</li>
<li>Mapping <code>'g'</code> to <code>'d'</code>.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "foo", t = "bar"</span></p>
<p><strong>Output:</strong> <span class="example-io">false</span></p>
<p><strong>Explanation:</strong></p>
<p>The strings <code>s</code> and <code>t</code> can not be made identical as <code>'o'</code> needs to be mapped to both <code>'a'</code> and <code>'r'</code>.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "paper", t = "title"</span></p>
<p><strong>Output:</strong> <span class="example-io">true</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 5 * 10<sup>4</sup></code></li>
<li><code>t.length == s.length</code></li>
<li><code>s</code> and <code>t</code> consist of any valid ascii character.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n bool isIsomorphic(string& s, string& t) {\n int n = s.size();\n bitset<256> s_char = 0, t_char = 0;\n char st[256] = {0};\n char ts[256] = {0};\n\n for (int i = 0; i < n; i++) {\n char cs = s[i], ct = t[i];\n if (s_char[cs] == 0 && t_char[ct] == 0) {\n st[cs] = ct;\n ts[ct] = cs;\n s_char[cs] = 1;\n t_char[ct] = 1;\n } else {\n if (st[cs] != ct || ts[ct] != cs) {\n return 0;\n }\n }\n }\n return 1;\n }\n};\n",
"memory": "8000"
} |
205 | <p>Given two strings <code>s</code> and <code>t</code>, <em>determine if they are isomorphic</em>.</p>
<p>Two strings <code>s</code> and <code>t</code> are isomorphic if the characters in <code>s</code> can be replaced to get <code>t</code>.</p>
<p>All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character, but a character may map to itself.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "egg", t = "add"</span></p>
<p><strong>Output:</strong> <span class="example-io">true</span></p>
<p><strong>Explanation:</strong></p>
<p>The strings <code>s</code> and <code>t</code> can be made identical by:</p>
<ul>
<li>Mapping <code>'e'</code> to <code>'a'</code>.</li>
<li>Mapping <code>'g'</code> to <code>'d'</code>.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "foo", t = "bar"</span></p>
<p><strong>Output:</strong> <span class="example-io">false</span></p>
<p><strong>Explanation:</strong></p>
<p>The strings <code>s</code> and <code>t</code> can not be made identical as <code>'o'</code> needs to be mapped to both <code>'a'</code> and <code>'r'</code>.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "paper", t = "title"</span></p>
<p><strong>Output:</strong> <span class="example-io">true</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 5 * 10<sup>4</sup></code></li>
<li><code>t.length == s.length</code></li>
<li><code>s</code> and <code>t</code> consist of any valid ascii character.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n bool isIsomorphic(string s, string t) {\n char map_s[128] = { 0 };\n char map_t[128] = { 0 };\n int len = s.size();\n for (int i = 0; i < len; ++i)\n {\n if (map_s[s[i]]!=map_t[t[i]]) return false;\n map_s[s[i]] = i+1;\n map_t[t[i]] = i+1;\n }\n return true; \n }\n};\n\nauto init = []() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n return 'c';\n}();\n",
"memory": "8100"
} |
205 | <p>Given two strings <code>s</code> and <code>t</code>, <em>determine if they are isomorphic</em>.</p>
<p>Two strings <code>s</code> and <code>t</code> are isomorphic if the characters in <code>s</code> can be replaced to get <code>t</code>.</p>
<p>All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character, but a character may map to itself.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "egg", t = "add"</span></p>
<p><strong>Output:</strong> <span class="example-io">true</span></p>
<p><strong>Explanation:</strong></p>
<p>The strings <code>s</code> and <code>t</code> can be made identical by:</p>
<ul>
<li>Mapping <code>'e'</code> to <code>'a'</code>.</li>
<li>Mapping <code>'g'</code> to <code>'d'</code>.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "foo", t = "bar"</span></p>
<p><strong>Output:</strong> <span class="example-io">false</span></p>
<p><strong>Explanation:</strong></p>
<p>The strings <code>s</code> and <code>t</code> can not be made identical as <code>'o'</code> needs to be mapped to both <code>'a'</code> and <code>'r'</code>.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "paper", t = "title"</span></p>
<p><strong>Output:</strong> <span class="example-io">true</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 5 * 10<sup>4</sup></code></li>
<li><code>t.length == s.length</code></li>
<li><code>s</code> and <code>t</code> consist of any valid ascii character.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n bool isIsomorphic(string s, string t) {\n int hash[256]={0};//mapping of each of language 's' to language 't'\n bool istCharsMapped[256]={0};//stors if t[i] char already mapped with s[i].\n\n for(int i=0;i<s.size();i++){\n if(hash[s[i]]==0 && istCharsMapped[t[i]]==0){\n hash[s[i]]=t[i];\n istCharsMapped[t[i]]=true;\n }\n }\n for(int i=0;i<s.size();i++){\n if(char(hash[s[i]]) != t[i]){\n return false;\n }\n }\n return true;\n }\n \n};",
"memory": "8200"
} |
205 | <p>Given two strings <code>s</code> and <code>t</code>, <em>determine if they are isomorphic</em>.</p>
<p>Two strings <code>s</code> and <code>t</code> are isomorphic if the characters in <code>s</code> can be replaced to get <code>t</code>.</p>
<p>All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character, but a character may map to itself.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "egg", t = "add"</span></p>
<p><strong>Output:</strong> <span class="example-io">true</span></p>
<p><strong>Explanation:</strong></p>
<p>The strings <code>s</code> and <code>t</code> can be made identical by:</p>
<ul>
<li>Mapping <code>'e'</code> to <code>'a'</code>.</li>
<li>Mapping <code>'g'</code> to <code>'d'</code>.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "foo", t = "bar"</span></p>
<p><strong>Output:</strong> <span class="example-io">false</span></p>
<p><strong>Explanation:</strong></p>
<p>The strings <code>s</code> and <code>t</code> can not be made identical as <code>'o'</code> needs to be mapped to both <code>'a'</code> and <code>'r'</code>.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "paper", t = "title"</span></p>
<p><strong>Output:</strong> <span class="example-io">true</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 5 * 10<sup>4</sup></code></li>
<li><code>t.length == s.length</code></li>
<li><code>s</code> and <code>t</code> consist of any valid ascii character.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n bool isIsomorphic(string s, string t) {\n if(s.size()!=t.size()) return false;\n vector<int> v(150,1000); // max no of characters is 150\n for(int i=0; i<s.size(); i++){\n int idx = (int)s[i];\n if(v[idx]==1000) v[idx] =( s[i] - t[i]);\n else if(v[idx] != (s[i] - t[i])) return false;\n }\n for(int i = 0; i<150; i++){\n v[i] = 1000;\n }\n for(int i=0; i<s.size(); i++){\n int idx = (int)t[i];\n if(v[idx]==1000) v[idx] =( t[i] - s[i]);\n else if(v[idx] != (t[i] - s[i])) return false;\n }\n return true;\n }\n};",
"memory": "8300"
} |
205 | <p>Given two strings <code>s</code> and <code>t</code>, <em>determine if they are isomorphic</em>.</p>
<p>Two strings <code>s</code> and <code>t</code> are isomorphic if the characters in <code>s</code> can be replaced to get <code>t</code>.</p>
<p>All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character, but a character may map to itself.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "egg", t = "add"</span></p>
<p><strong>Output:</strong> <span class="example-io">true</span></p>
<p><strong>Explanation:</strong></p>
<p>The strings <code>s</code> and <code>t</code> can be made identical by:</p>
<ul>
<li>Mapping <code>'e'</code> to <code>'a'</code>.</li>
<li>Mapping <code>'g'</code> to <code>'d'</code>.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "foo", t = "bar"</span></p>
<p><strong>Output:</strong> <span class="example-io">false</span></p>
<p><strong>Explanation:</strong></p>
<p>The strings <code>s</code> and <code>t</code> can not be made identical as <code>'o'</code> needs to be mapped to both <code>'a'</code> and <code>'r'</code>.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "paper", t = "title"</span></p>
<p><strong>Output:</strong> <span class="example-io">true</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 5 * 10<sup>4</sup></code></li>
<li><code>t.length == s.length</code></li>
<li><code>s</code> and <code>t</code> consist of any valid ascii character.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n bool isIsomorphic(string s, string t) {\n int m1[256] = {0}, m2[256] = {0}, n = s.size();\n for (int i = 0; i < n; ++i) {\n if (m1[s[i]] != m2[t[i]]) return false;\n m1[s[i]] = i + 1;\n m2[t[i]] = i + 1;\n }\n return true;\n }\n};",
"memory": "8300"
} |
205 | <p>Given two strings <code>s</code> and <code>t</code>, <em>determine if they are isomorphic</em>.</p>
<p>Two strings <code>s</code> and <code>t</code> are isomorphic if the characters in <code>s</code> can be replaced to get <code>t</code>.</p>
<p>All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character, but a character may map to itself.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "egg", t = "add"</span></p>
<p><strong>Output:</strong> <span class="example-io">true</span></p>
<p><strong>Explanation:</strong></p>
<p>The strings <code>s</code> and <code>t</code> can be made identical by:</p>
<ul>
<li>Mapping <code>'e'</code> to <code>'a'</code>.</li>
<li>Mapping <code>'g'</code> to <code>'d'</code>.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "foo", t = "bar"</span></p>
<p><strong>Output:</strong> <span class="example-io">false</span></p>
<p><strong>Explanation:</strong></p>
<p>The strings <code>s</code> and <code>t</code> can not be made identical as <code>'o'</code> needs to be mapped to both <code>'a'</code> and <code>'r'</code>.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "paper", t = "title"</span></p>
<p><strong>Output:</strong> <span class="example-io">true</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 5 * 10<sup>4</sup></code></li>
<li><code>t.length == s.length</code></li>
<li><code>s</code> and <code>t</code> consist of any valid ascii character.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n bool isIsomorphic(string s, string t) {\n if(s.size()!=t.size()) return false;\n vector<int>v(150,1000);\n for(int i=0;i<s.size();i++){\n int idx=(int)s[i];\n if(v[idx]==1000) v[idx]=s[i]-t[i];\n else if(v[idx]!=(s[i]-t[i])) return false;\n }\n //emptying the vector\n for(int i=0;i<150;i++){\n v[i]=1000;\n }\n for(int i=0;i<s.size();i++){\n int idx=(int)t[i];\n if(v[idx]==1000) v[idx]=t[i]-s[i];\n else if(v[idx]!=(t[i]-s[i])) return false;\n }\n return true;\n }\n};",
"memory": "8400"
} |
205 | <p>Given two strings <code>s</code> and <code>t</code>, <em>determine if they are isomorphic</em>.</p>
<p>Two strings <code>s</code> and <code>t</code> are isomorphic if the characters in <code>s</code> can be replaced to get <code>t</code>.</p>
<p>All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character, but a character may map to itself.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "egg", t = "add"</span></p>
<p><strong>Output:</strong> <span class="example-io">true</span></p>
<p><strong>Explanation:</strong></p>
<p>The strings <code>s</code> and <code>t</code> can be made identical by:</p>
<ul>
<li>Mapping <code>'e'</code> to <code>'a'</code>.</li>
<li>Mapping <code>'g'</code> to <code>'d'</code>.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "foo", t = "bar"</span></p>
<p><strong>Output:</strong> <span class="example-io">false</span></p>
<p><strong>Explanation:</strong></p>
<p>The strings <code>s</code> and <code>t</code> can not be made identical as <code>'o'</code> needs to be mapped to both <code>'a'</code> and <code>'r'</code>.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "paper", t = "title"</span></p>
<p><strong>Output:</strong> <span class="example-io">true</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 5 * 10<sup>4</sup></code></li>
<li><code>t.length == s.length</code></li>
<li><code>s</code> and <code>t</code> consist of any valid ascii character.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n bool isIsomorphic(string s, string t) {\n vector<char> map_s_to_t(128, ' '); \n vector<char> map_t_to_s(128, ' '); \n \n int n = s.length();\n \n for (int i = 0; i < n; i++) {\n char ch_s = s[i];\n char ch_t = t[i];\n \n if (map_s_to_t[ch_s] == ' ') {\n map_s_to_t[ch_s] = ch_t;\n } else if (map_s_to_t[ch_s] != ch_t) {\n return false; \n }\n \n if (map_t_to_s[ch_t] == ' ') {\n map_t_to_s[ch_t] = ch_s;\n } else if (map_t_to_s[ch_t] != ch_s) {\n return false; \n }\n }\n \n return true;\n }\n};",
"memory": "8400"
} |
205 | <p>Given two strings <code>s</code> and <code>t</code>, <em>determine if they are isomorphic</em>.</p>
<p>Two strings <code>s</code> and <code>t</code> are isomorphic if the characters in <code>s</code> can be replaced to get <code>t</code>.</p>
<p>All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character, but a character may map to itself.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "egg", t = "add"</span></p>
<p><strong>Output:</strong> <span class="example-io">true</span></p>
<p><strong>Explanation:</strong></p>
<p>The strings <code>s</code> and <code>t</code> can be made identical by:</p>
<ul>
<li>Mapping <code>'e'</code> to <code>'a'</code>.</li>
<li>Mapping <code>'g'</code> to <code>'d'</code>.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "foo", t = "bar"</span></p>
<p><strong>Output:</strong> <span class="example-io">false</span></p>
<p><strong>Explanation:</strong></p>
<p>The strings <code>s</code> and <code>t</code> can not be made identical as <code>'o'</code> needs to be mapped to both <code>'a'</code> and <code>'r'</code>.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "paper", t = "title"</span></p>
<p><strong>Output:</strong> <span class="example-io">true</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 5 * 10<sup>4</sup></code></li>
<li><code>t.length == s.length</code></li>
<li><code>s</code> and <code>t</code> consist of any valid ascii character.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n bool isIsomorphic(string s, string t) {\n int hash[256] = {0};\n bool isTCharMapped[256]={0};\n for(int i=0;i<s.size();i++){\n if(hash[s[i]]==0&& isTCharMapped[t[i]] ==0)\n {\n hash[s[i]] = t[i];\n isTCharMapped[t[i]] = 1;\n }\n } \n for(int i = 0; i<s.size();i++){\n if(char(hash[s[i]])!=t[i]){\n return false;\n }\n }\n return true;\n }\n};",
"memory": "8500"
} |
205 | <p>Given two strings <code>s</code> and <code>t</code>, <em>determine if they are isomorphic</em>.</p>
<p>Two strings <code>s</code> and <code>t</code> are isomorphic if the characters in <code>s</code> can be replaced to get <code>t</code>.</p>
<p>All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character, but a character may map to itself.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "egg", t = "add"</span></p>
<p><strong>Output:</strong> <span class="example-io">true</span></p>
<p><strong>Explanation:</strong></p>
<p>The strings <code>s</code> and <code>t</code> can be made identical by:</p>
<ul>
<li>Mapping <code>'e'</code> to <code>'a'</code>.</li>
<li>Mapping <code>'g'</code> to <code>'d'</code>.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "foo", t = "bar"</span></p>
<p><strong>Output:</strong> <span class="example-io">false</span></p>
<p><strong>Explanation:</strong></p>
<p>The strings <code>s</code> and <code>t</code> can not be made identical as <code>'o'</code> needs to be mapped to both <code>'a'</code> and <code>'r'</code>.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "paper", t = "title"</span></p>
<p><strong>Output:</strong> <span class="example-io">true</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 5 * 10<sup>4</sup></code></li>
<li><code>t.length == s.length</code></li>
<li><code>s</code> and <code>t</code> consist of any valid ascii character.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n bool isIsomorphic(string s, string t) {\n int a[128];\n int b[128];\n\n for(int i =0; i < 128; i++){ a[i] = -1;b[i] = -1;}\n\n for (int i = 0; i < s.size(); i++) {\n if (a[s[i] - NULL] != (t[i] - NULL) && a[s[i] - NULL] >= 0)\n return 0;\n else\n a[s[i] - NULL] = t[i] - NULL;\n }\n for (int i = 0; i < s.size(); i++) {\n if (b[t[i] - NULL] != (s[i] - NULL) && b[t[i] - NULL] >= 0)\n return 0;\n else\n b[t[i] - NULL] = s[i] - NULL;\n }\n return 1;\n }\n};",
"memory": "8500"
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.