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>...
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(in...
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>...
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 ...
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>...
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 ...
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>...
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...
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>...
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 ...
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>...
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...
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>...
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) ...
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>...
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 ...
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>...
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...
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>...
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...
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>...
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--) s...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> left = 5, right = 7 <strong>Ou...
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(lef...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> left = 5, right = 7 <strong>Ou...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> left = 5, right = 7 <strong>Ou...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> left = 5, right = 7 <strong>Ou...
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 ...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> left = 5, right = 7 <strong>Ou...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> left = 5, right = 7 <strong>Ou...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> left = 5, right = 7 <strong>Ou...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> left = 5, right = 7 <strong>Ou...
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 grpLe...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 10 <strong>Output:</strong> 4 <strong>Explanation:</strong> There are 4 prime numbers less ...
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;\...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 10 <strong>Output:</strong> 4 <strong>Explanation:</strong> There are 4 prime numbers less ...
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 ...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 10 <strong>Output:</strong> 4 <strong>Explanation:</strong> There are 4 prime numbers less ...
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 pr...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 10 <strong>Output:</strong> 4 <strong>Explanation:</strong> There are 4 prime numbers less ...
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) + ...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 10 <strong>Output:</strong> 4 <strong>Explanation:</strong> There are 4 prime numbers less ...
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 ...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 10 <strong>Output:</strong> 4 <strong>Explanation:</strong> There are 4 prime numbers less ...
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_p...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 10 <strong>Output:</strong> 4 <strong>Explanation:</strong> There are 4 prime numbers less ...
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 i...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 10 <strong>Output:</strong> 4 <strong>Explanation:</strong> There are 4 prime numbers less ...
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 fo...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 10 <strong>Output:</strong> 4 <strong>Explanation:</strong> There are 4 prime numbers less ...
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 = ...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 10 <strong>Output:</strong> 4 <strong>Explanation:</strong> There are 4 prime numbers less ...
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_ba...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 10 <strong>Output:</strong> 4 <strong>Explanation:</strong> There are 4 prime numbers less ...
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...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 10 <strong>Output:</strong> 4 <strong>Explanation:</strong> There are 4 prime numbers less ...
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_...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 10 <strong>Output:</strong> 4 <strong>Explanation:</strong> There are 4 prime numbers less ...
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 ...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 10 <strong>Output:</strong> 4 <strong>Explanation:</strong> There are 4 prime numbers less ...
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 ...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 10 <strong>Output:</strong> 4 <strong>Explanation:</strong> There are 4 prime numbers less ...
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(in...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 10 <strong>Output:</strong> 4 <strong>Explanation:</strong> There are 4 prime numbers less ...
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[...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 10 <strong>Output:</strong> 4 <strong>Explanation:</strong> There are 4 prime numbers less ...
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;\...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 10 <strong>Output:</strong> 4 <strong>Explanation:</strong> There are 4 prime numbers less ...
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(i...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 10 <strong>Output:</strong> 4 <strong>Explanation:</strong> There are 4 prime numbers less ...
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 ...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 10 <strong>Output:</strong> 4 <strong>Explanation:</strong> There are 4 prime numbers less ...
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 ...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 10 <strong>Output:</strong> 4 <strong>Explanation:</strong> There are 4 prime numbers less ...
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...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 10 <strong>Output:</strong> 4 <strong>Explanation:</strong> There are 4 prime numbers less ...
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 st...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 10 <strong>Output:</strong> 4 <strong>Explanation:</strong> There are 4 prime numbers less ...
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++) {...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 10 <strong>Output:</strong> 4 <strong>Explanation:</strong> There are 4 prime numbers less ...
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) {\...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 10 <strong>Output:</strong> 4 <strong>Explanation:</strong> There are 4 prime numbers less ...
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_p...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 10 <strong>Output:</strong> 4 <strong>Explanation:</strong> There are 4 prime numbers less ...
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=fals...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 10 <strong>Output:</strong> 4 <strong>Explanation:</strong> There are 4 prime numbers less ...
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 *ispri...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 10 <strong>Output:</strong> 4 <strong>Explanation:</strong> There are 4 prime numbers less ...
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...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 10 <strong>Output:</strong> 4 <strong>Explanation:</strong> There are 4 prime numbers less ...
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 (isP...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 10 <strong>Output:</strong> 4 <strong>Explanation:</strong> There are 4 prime numbers less ...
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...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 10 <strong>Output:</strong> 4 <strong>Explanation:</strong> There are 4 prime numbers less ...
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 (!is...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 10 <strong>Output:</strong> 4 <strong>Explanation:</strong> There are 4 prime numbers less ...
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...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 10 <strong>Output:</strong> 4 <strong>Explanation:</strong> There are 4 prime numbers less ...
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 whil...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 10 <strong>Output:</strong> 4 <strong>Explanation:</strong> There are 4 prime numbers less ...
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 ...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 10 <strong>Output:</strong> 4 <strong>Explanation:</strong> There are 4 prime numbers less ...
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[...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 10 <strong>Output:</strong> 4 <strong>Explanation:</strong> There are 4 prime numbers less ...
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 ...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 10 <strong>Output:</strong> 4 <strong>Explanation:</strong> There are 4 prime numbers less ...
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...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 10 <strong>Output:</strong> 4 <strong>Explanation:</strong> There are 4 prime numbers less ...
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 ...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 10 <strong>Output:</strong> 4 <strong>Explanation:</strong> There are 4 prime numbers less ...
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 ...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 10 <strong>Output:</strong> 4 <strong>Explanation:</strong> There are 4 prime numbers less ...
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 ...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 10 <strong>Output:</strong> 4 <strong>Explanation:</strong> There are 4 prime numbers less ...
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...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 10 <strong>Output:</strong> 4 <strong>Explanation:</strong> There are 4 prime numbers less ...
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 ...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 10 <strong>Output:</strong> 4 <strong>Explanation:</strong> There are 4 prime numbers less ...
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] ...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 10 <strong>Output:</strong> 4 <strong>Explanation:</strong> There are 4 prime numbers less ...
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 ...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 10 <strong>Output:</strong> 4 <strong>Explanation:</strong> There are 4 prime numbers less ...
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 ...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 10 <strong>Output:</strong> 4 <strong>Explanation:</strong> There are 4 prime numbers less ...
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 ...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 10 <strong>Output:</strong> 4 <strong>Explanation:</strong> There are 4 prime numbers less ...
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...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 10 <strong>Output:</strong> 4 <strong>Explanation:</strong> There are 4 prime numbers less ...
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 ...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 10 <strong>Output:</strong> 4 <strong>Explanation:</strong> There are 4 prime numbers less ...
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...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 10 <strong>Output:</strong> 4 <strong>Explanation:</strong> There are 4 prime numbers less ...
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 ...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 10 <strong>Output:</strong> 4 <strong>Explanation:</strong> There are 4 prime numbers less ...
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 ...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 10 <strong>Output:</strong> 4 <strong>Explanation:</strong> There are 4 prime numbers less ...
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 ...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 10 <strong>Output:</strong> 4 <strong>Explanation:</strong> There are 4 prime numbers less ...
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 ...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 10 <strong>Output:</strong> 4 <strong>Explanation:</strong> There are 4 prime numbers less ...
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 ...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 10 <strong>Output:</strong> 4 <strong>Explanation:</strong> There are 4 prime numbers less ...
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;\...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 10 <strong>Output:</strong> 4 <strong>Explanation:</strong> There are 4 prime numbers less ...
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...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 10 <strong>Output:</strong> 4 <strong>Explanation:</strong> There are 4 prime numbers less ...
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...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 10 <strong>Output:</strong> 4 <strong>Explanation:</strong> There are 4 prime numbers less ...
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...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 10 <strong>Output:</strong> 4 <strong>Explanation:</strong> There are 4 prime numbers less ...
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 ...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 10 <strong>Output:</strong> 4 <strong>Explanation:</strong> There are 4 prime numbers less ...
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 ...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 10 <strong>Output:</strong> 4 <strong>Explanation:</strong> There are 4 prime numbers less ...
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) ...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 10 <strong>Output:</strong> 4 <strong>Explanation:</strong> There are 4 prime numbers less ...
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 ...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 10 <strong>Output:</strong> 4 <strong>Explanation:</strong> There are 4 prime numbers less ...
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...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 10 <strong>Output:</strong> 4 <strong>Explanation:</strong> There are 4 prime numbers less ...
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 ...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 10 <strong>Output:</strong> 4 <strong>Explanation:</strong> There are 4 prime numbers less ...
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 ...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 10 <strong>Output:</strong> 4 <strong>Explanation:</strong> There are 4 prime numbers less ...
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 ...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 10 <strong>Output:</strong> 4 <strong>Explanation:</strong> There are 4 prime numbers less ...
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 fo...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 10 <strong>Output:</strong> 4 <strong>Explanation:</strong> There are 4 prime numbers less ...
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 a...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 10 <strong>Output:</strong> 4 <strong>Explanation:</strong> There are 4 prime numbers less ...
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 =...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 10 <strong>Output:</strong> 4 <strong>Explanation:</strong> There are 4 prime numbers less ...
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 ...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 10 <strong>Output:</strong> 4 <strong>Explanation:</strong> There are 4 prime numbers less ...
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 ...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 10 <strong>Output:</strong> 4 <strong>Explanation:</strong> There are 4 prime numbers less ...
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 ...
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 characte...
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...
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 characte...
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...
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 characte...
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 && istCh...
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 characte...
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...
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 characte...
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 retur...
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 characte...
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[...
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 characte...
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[...
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 characte...
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 ...
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 characte...
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 ...