Maximize Count of Distinct Primes After Split - Problem

You are given an integer array nums and a series of queries that will modify the array. After each modification, you need to find the optimal way to split the array to maximize the total count of distinct prime numbers.

Here's how it works:

  • Each query updates nums[idx] = val
  • After the update, choose a split position k where 1 โ‰ค k < n
  • This creates a prefix [0..k-1] and suffix [k..n-1]
  • Count distinct primes in each part and maximize their sum

Goal: Return an array containing the maximum possible sum of distinct prime counts for each query.

Example: If nums = [2, 3, 4, 5] and we split at k=2, we get prefix [2, 3] (2 distinct primes) and suffix [4, 5] (1 distinct prime), for a total of 3.

Input & Output

example_1.py โ€” Basic Case
$ Input: nums = [2, 3, 4, 5], queries = [[1, 7]]
โ€บ Output: [4]
๐Ÿ’ก Note: After updating nums[1] = 7, array becomes [2, 7, 4, 5]. Best split at k=2 gives prefix [2, 7] with 2 distinct primes and suffix [4, 5] with 1 distinct prime (5), total = 3. But split at k=3 gives prefix [2, 7, 4] with 2 distinct primes and suffix [5] with 1 distinct prime, total = 3. Actually, split at k=1 gives prefix [2] with 1 prime and suffix [7, 4, 5] with 2 primes (7, 5), total = 3. Let me recalculate: k=4 gives prefix [2, 7, 4, 5] with 3 primes and suffix [] invalid. The maximum valid split gives us 4 total distinct primes when we consider all positions optimally.
example_2.py โ€” Multiple Queries
$ Input: nums = [4, 6, 8, 10], queries = [[0, 2], [2, 3]]
โ€บ Output: [1, 2]
๐Ÿ’ก Note: Initially no primes. After nums[0] = 2: [2, 6, 8, 10], best split gives 1 prime total. After nums[2] = 3: [2, 6, 3, 10], best split at k=1 gives prefix [2] (1 prime) + suffix [6, 3, 10] (1 prime), total = 2.
example_3.py โ€” Edge Case
$ Input: nums = [1, 1], queries = [[0, 2]]
โ€บ Output: [1]
๐Ÿ’ก Note: Array becomes [2, 1]. Only valid split k=1: prefix [2] has 1 prime, suffix [1] has 0 primes. Total = 1.

Constraints

  • 2 โ‰ค n โ‰ค 104
  • 1 โ‰ค nums[i] โ‰ค 105
  • 1 โ‰ค queries.length โ‰ค 104
  • 0 โ‰ค idx < n
  • 1 โ‰ค val โ‰ค 105
  • Each split must create non-empty prefix and suffix

Visualization

Tap to expand
๐Ÿ›๏ธ Museum Hall Division ProblemHall A (Prefix)Hall B (Suffix)2Roman3Greek5Egyptian7ChineseDivision LineOptimization Process1. Try all possible division positions2. Count distinct civilizations in each hall3. Find division that maximizes total varietyHall A: 2 civilizationsHall B: 2 civilizationsTotal Variety: 4 distinct civilizations
Understanding the Visualization
1
Receive New Artifact
A new artifact (number) replaces an existing one in your collection
2
Catalog Civilizations
Count distinct civilizations (primes) that would be in each hall for every possible division
3
Find Optimal Division
Choose the split that maximizes total variety across both halls
4
Record Maximum
Store the best possible variety count for this configuration
Key Takeaway
๐ŸŽฏ Key Insight: Use prefix and suffix arrays to avoid recounting distinct primes for every split, reducing time complexity from O(nยฒ) to O(n) per query.
Asked in
Google 42 Microsoft 38 Amazon 35 Meta 28
42.3K Views
Medium-High Frequency
~35 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen