Maximize Count of Distinct Primes After Split

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 k with 1 <= k < n to split the array into the non-empty prefix nums[0..k-1] and suffix nums[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.

Example 1
Inputnums = [2,1,3,1,2], queries = [[1,2],[3,3]]
Output[3,4]
After the first query, the best split gives 1 distinct prime in the prefix and 2 in the suffix, and after the second query the best split gives 2 plus 2.
Example 2
Inputnums = [2,1,4], queries = [[0,1]]
Output[0]
After the query, nums contains no prime numbers, so the maximum sum is 0.

Constraints

  • 2 <= n == nums.length <= 5 * 10^4
  • 1 <= queries.length <= 5 * 10^4
  • 1 <= nums[i] <= 10^5
  • 0 <= queries[i][0] < nums.length
  • 1 <= queries[i][1] <= 10^5

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate