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
kwhere1 โค 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
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.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code