Maximize Count of Distinct Primes After Split - Problem
You are given an integer array nums having length n and a 2D integer array queries where queries[i] = [idx, val].
For each query:
- Update
nums[idx] = val - Choose an integer
kwith1 <= k < nto split the array into the non-empty prefixnums[0..k-1]and suffixnums[k..n-1]such that the sum of the counts of distinct prime values in each part is maximum
Note: The changes made to the array in one query persist into the next query.
Return an array containing the result for each query, in the order they are given.
Input & Output
Example 1 — Basic Case
$
Input:
nums = [4,2,6,10], queries = [[1,3],[3,7]]
›
Output:
[1,2]
💡 Note:
Query 1: nums becomes [4,3,6,10]. All possible splits give 1 distinct prime total (the prime 3 appears in either left or right part). Query 2: nums becomes [4,3,6,7]. Split at k=1 gives left=[4] (0 primes) and right=[3,6,7] (2 primes: 3,7) for total=2. Other splits also give total=2. Maximum is 2.
Example 2 — Small Array
$
Input:
nums = [2,3], queries = [[0,5]]
›
Output:
[2]
💡 Note:
After update: nums = [5,3]. Only one split possible at k=1: left=[5] (1 prime) + right=[3] (1 prime) = 2 total distinct primes.
Example 3 — No Primes
$
Input:
nums = [4,6,8,9], queries = [[1,10]]
›
Output:
[0]
💡 Note:
After update: nums = [4,10,8,9]. No matter how we split, there are no prime numbers, so the result is 0.
Constraints
- 2 ≤ nums.length ≤ 103
- 1 ≤ queries.length ≤ 103
- 1 ≤ nums[i], val ≤ 106
- 0 ≤ idx < nums.length
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code