Count Primes - Complete Solution Guide
Count Primes is LeetCode problem 204, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
Given an integer n , return the number of prime numbers that are strictly less than n . Example 1: Input: n = 10 Output: 4 Explanation: There are 4 prime numbers less than 10, they are 2, 3, 5, 7. Example 2: Input: n = 0 Output: 0 Example 3: Input: n = 1 Output: 0 Constraints: 0 <= n <= 5 * 10 6
Detailed Explanation
The problem asks us to count the number of prime numbers strictly less than a given integer `n`. A prime number is a natural number greater than 1 that is not a product of two smaller natural numbers. In other words, a prime number has only two distinct positive divisors: 1 and itself. For example, the prime numbers less than 10 are 2, 3, 5, and 7. The input is a non-negative integer `n`, and the output is the number of primes less than `n`. The constraint is that `0 <= n <= 5 * 10^6`.
Solution Approach
The solution uses the Sieve of Eratosthenes to efficiently find all prime numbers less than `n`. We create a boolean array `is_prime` of size `n`, where `is_prime[i]` represents whether the number `i` is prime. Initially, we assume all numbers are prime (except 0 and 1). Then, we iterate through the array, starting from 2. If a number `p` is marked as prime, we mark all its multiples (starting from p*p) as composite (not prime). After this process, the number of `True` values in the `is_prime` array represents the number of primes less than `n`.
Step-by-Step Algorithm
- Step 1: Handle the base cases where n <= 2. In these cases, there are no prime numbers less than n, so return 0.
- Step 2: Create a boolean array `is_prime` of size `n`, initialized to `True` for all indices. This array will be used to mark prime numbers.
- Step 3: Mark `is_prime[0]` and `is_prime[1]` as `False`, since 0 and 1 are not prime.
- Step 4: Iterate from `p = 2` to the square root of `n`. If `is_prime[p]` is `True` (meaning `p` is prime), then mark all multiples of `p` starting from `p*p` as `False` (composite). The multiples are marked with a step of `p`.
- Step 5: Count the number of `True` values in the `is_prime` array. This count represents the number of prime numbers less than `n`.
- Step 6: Return the count of prime numbers.
Key Insights
- Insight 1: The Sieve of Eratosthenes is an efficient algorithm for finding all prime numbers up to a specified integer.
- Insight 2: We only need to iterate up to the square root of `n` when marking composite numbers. This is because if a number `n` is composite, it must have a factor less than or equal to its square root.
- Insight 3: The space complexity can become an issue for large n, but the problem constraint limits the size of n.
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(n)
Topics
This problem involves: Array, Math, Enumeration, Number Theory.
Companies
Asked at: Accenture, Cognizant, Intel, Nokia, Salesforce, Tech Mahindra, Walmart Labs, Wipro, tcs.