↓Skip to main content
  1. LeetCode/

LeetCode 2761: Prime Pairs With Target Sum

·2 mins· ·
LeetCode Medium Math Number-Theory Sieve Enumeration Array
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2026-10-12

  • Submissions: Accepted (first try)

Approach
#

  1. Sieve every prime < n (n up to 10^6, so a sieve beats trial division per number).
  2. For x from 2 to n / 2: if x and n - x are both prime, [x, n - x] is a pair. Since x <= n - x, the result comes out sorted by x.

My Solution
#

My accepted version ran a linear sieve, then converging pointers over the prime list looking for sums of n (like Two Sum II), taking 5.7 seconds. Checking is_prime[n - x] directly is simpler:

from math import isqrt

class Solution:
    def findPrimePairs(self, n: int) -> List[List[int]]:
        if n < 4:
            return []
        is_prime = [False, False] + [True] * (n - 1)
        for i in range(2, isqrt(n) + 1):
            if is_prime[i]:
                for j in range(i * i, n + 1, i):
                    is_prime[j] = False
        return [[x, n - x] for x in range(2, n // 2 + 1) if is_prime[x] and is_prime[n - x]]

Complexity
#

Time O(n log log n), space O(n).

Mistakes to Avoid
#

  • Stop x at n / 2, or each pair appears twice.
  • Equal pairs like [5, 5] count too.

Interview Explanation
#

I sieve the primes up to n, then walk x from 2 to n / 2, adding [x, n - x] when both are prime; the order is naturally increasing. Time O(n log log n).

Related

LeetCode 2523: Closest Prime Numbers in Range
·2 mins
LeetCode Medium Math Number-Theory Sieve Array
LeetCode 2523 note: sieve the primes in range, since only adjacent primes can be closest; or trial-divide rightward from left and stop at the first twin primes with gap 2
LeetCode 3618: Split Array by Prime Indices
·2 mins
LeetCode Medium Math Number-Theory Sieve Array
LeetCode 3618 note: primality is about the index; a sieve of Eratosthenes marks all of 0..n-1 at once, faster than trial division per index
LeetCode 204: Count Primes
·2 mins
LeetCode Medium Array Math Enumeration Number-Theory
LeetCode 204 note: the sieve of Eratosthenes, crossing out multiples of each prime p starting at p², with the outer loop only up to √n
LeetCode 1998: GCD Sort of an Array
·3 mins
LeetCode Hard Math Number-Theory Union-Find Sieve Sorting Array
LeetCode 1998 note: numbers in one component can be permuted freely; union each number with its prime factors via SPF, and the array sorts iff each position’s old and new values share a group
LeetCode 3984: Divisible Game
·3 mins
LeetCode Medium Dynamic-Programming Kadane Math Number-Theory Enumeration
LeetCode 3984 note: replacing a composite k by one of its prime factors only helps (more values divisible, smaller k), so try every prime factor of the numbers plus 2, running Kadane once per candidate
LeetCode 2709: Greatest Common Divisor Traversal
·3 mins
LeetCode Hard Math Number-Theory Union-Find Sieve Gcd Array
LeetCode 2709 note: every pair of indices connects ⇔ one component; factor with SPF and union each number with its prime factors in Union-Find, treating 1 specially