LeetCode Problem

How to Solve Maximize Count of Distinct Primes After Split

This problem requires updating an array and tracking distinct prime numbers after each modification. Using a sieve to preprocess primes and a data structure to maintain counts allows each query to be processed efficiently. The approach balances math insights with array manipulation for optimal performance under tight constraints.

GhostInterview Help

Need help with Maximize Count of Distinct Primes After Split without spending extra time grinding it?

GhostInterview can read Maximize Count of Distinct Primes After Split from a screenshot, generate the answer path, explain the complexity, and support solver-first interview workflows when you need direct help fast.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.

Problem #3569Array plus MathReviewed 2026-03-08
Difficulty
Hard
Primary pattern
Array plus Math
Answer-first problem summary
Step-by-step approach and complexity
GhostInterview solver workflow

This problem requires updating an array and tracking distinct prime numbers after each modification. Using a sieve to preprocess primes and a data structure to maintain counts allows each query to be processed efficiently. The approach balances math insights with array manipulation for optimal performance under tight constraints.

Problem Statement

Given an integer array nums of length n and a 2D array queries where each query is [index, value], update nums[index] to value for each query. After each update, compute the maximum number of distinct prime numbers that can exist in the updated array.

Note that changes made in one query persist for subsequent queries. Your goal is to return an array of results representing the count of distinct primes after each update.

Examples

Example 1

Input: nums = [2,1,3,1,2], queries = [[1,2],[3,3]]

Output: [3,4]

Example 2

Input: nums = [2,1,4], queries = [[0,1]]

Output: [0]

Constraints

  • 2 <= n == nums.length <= 5 * 104
  • 1 <= queries.length <= 5 * 104
  • 1 <= nums[i] <= 105
  • 0 <= queries[i][0] < nums.length
  • 1 <= queries[i][1] <= 105

Solution Approach

Preprocess Primes with a Sieve

Generate all primes up to the maximum possible value in nums using the Sieve of Eratosthenes. This allows constant-time primality checks for each number during updates.

Maintain Frequency Counts

Track the frequency of each prime in the current array. When an element is updated, adjust counts for any primes involved to quickly determine the number of distinct primes without rescanning the array.

Process Queries Efficiently

Iterate through queries, update nums, adjust prime frequencies, and record the current distinct prime count after each change. This ensures each query runs in near O(1) time relative to primes affected.

Complexity Analysis

MetricValue
TimeDepends on the final approach
SpaceDepends on the final approach

Time complexity depends on preprocessing primes (O(maxNum log log maxNum)) and processing each query efficiently using frequency maps. Space complexity is dominated by storing prime flags and counts, both O(maxNum).

What Interviewers Usually Probe

  • Are you using preprocessing to check primality efficiently?
  • How do you track distinct primes after each update without rescanning the array?
  • Can you handle multiple queries while maintaining correctness under persistent changes?

Common Pitfalls or Variants

Common pitfalls

  • Failing to preprocess primes, leading to repeated slow primality checks.
  • Updating counts incorrectly, which can produce wrong distinct prime results.
  • Not persisting previous updates before processing the next query.

Follow-up variants

  • Compute the sum of distinct primes after each update instead of the count.
  • Find distinct primes in a sliding window after multiple updates.
  • Allow deletions and insertions in nums, requiring dynamic prime tracking.

How GhostInterview Helps

  • Automatically identifies primes in nums and updates frequency counts per query.
  • Provides optimized handling of array updates while maintaining distinct prime counts.
  • Highlights edge cases where updates overwrite previously counted primes.

Topic Pages

FAQ

What is the main challenge in Maximize Count of Distinct Primes After Split?

The challenge is efficiently tracking distinct primes after each update while handling persistent changes to the array.

Should I recompute primes from scratch after each query?

No, preprocessing all primes using a sieve allows constant-time checks and avoids redundant computation.

How does the array plus math pattern apply here?

Array manipulation handles updates, while number theory and the sieve help quickly identify and count distinct primes.

Can segment trees help in this problem?

Yes, segment trees can maintain prime frequency counts for range queries or batch updates if needed.

What is a common mistake when updating counts?

Forgetting to decrement the count of primes from the old value before incrementing for the new value can lead to incorrect results.

GhostInterview Solver

Need direct help with Maximize Count of Distinct Primes After Split instead of spending more time grinding it?

Download GhostInterview when you want a LeetCode solver, not another long practice loop. Capture Maximize Count of Distinct Primes After Split from a screenshot, get the answer path and complexity, and use supported stealth workflows that stay outside captured layers.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.