Watch 6 video solutions for Largest Prime from Consecutive Prime Sum, a medium level problem involving Array, Math, Number Theory. This walkthrough by Sanyam IIT Guwahati has 536 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
You are given an integer n.
Return the largest prime number less than or equal to n that can be expressed as the sum of one or more consecutive prime numbers starting from 2. If no such number exists, return 0.
Example 1:
Input: n = 20
Output: 17
Explanation:
The prime numbers less than or equal to n = 20 which are consecutive prime sums are:
2 = 2
5 = 2 + 3
17 = 2 + 3 + 5 + 7
The largest is 17, so it is the answer.
Example 2:
Input: n = 2
Output: 2
Explanation:
The only consecutive prime sum less than or equal to 2 is 2 itself.
Constraints:
1 <= n <= 5 * 105Problem Overview: You are given a constraint over prime numbers and need to compute the largest prime that can be formed by summing consecutive prime values. The challenge is not only generating primes efficiently but also checking many consecutive prime ranges without recomputing sums repeatedly.
Approach 1: Brute Force Consecutive Prime Enumeration (O(P^3) time, O(P) space)
Start by generating all primes up to the limit using a simple primality check. Then iterate over every possible start index of the prime list and expand the sequence one element at a time while recomputing the running sum. Each sum must be checked again for primality. This leads to repeated work: generating primes, recomputing sums, and performing primality checks multiple times. Time complexity grows quickly because there are O(P^2) ranges and each sum or prime check can take O(P). Space complexity stays O(P) for storing the prime list.
Approach 2: Preprocessing + Binary Search (O(N log log N + P log P) time, O(N) space)
The efficient solution precomputes primes using the Sieve of Eratosthenes. This preprocessing step marks all primes up to the limit in O(N log log N) time and stores them in an array. Next, build a prefix sum array where prefix[i] stores the sum of the first i primes. With prefix sums, any consecutive prime sum can be computed in O(1) using prefix[r] - prefix[l].
For each starting prime index, perform a binary search on the prefix array to find the farthest ending index whose sum does not exceed the limit. This avoids scanning every possible ending position. Compute the candidate sum using the prefix array and check if it is prime using the precomputed sieve. Track the largest valid prime encountered. Binary search reduces the search space for each start index, making the overall process far more efficient.
This approach combines ideas from Array prefix processing, fast prime generation from Number Theory, and search optimization techniques similar to Math-driven range queries. The prefix array eliminates repeated addition while the sieve eliminates expensive primality tests.
Recommended for interviews: Interviewers expect you to move beyond brute force quickly. Starting with a naive enumeration shows understanding of the problem structure, but the optimal solution uses prime preprocessing and prefix sums. Adding binary search to limit the search range demonstrates algorithmic maturity and knowledge of common optimization patterns.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Consecutive Prime Enumeration | O(P^3) | O(P) | Useful for understanding the problem or when input constraints are extremely small. |
| Preprocessing + Binary Search (Sieve + Prefix Sum) | O(N log log N + P log P) | O(N) | Best choice for medium or large limits. Efficient prime generation and fast range-sum checks. |