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 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.

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
Maximize Count of Distinct Primes After Split INPUT nums array: 4 [0] 2 [1] 6 [2] 10 [3] queries: [1, 3] [3, 7] Prime numbers in range: 2, 3, 5, 7, 11... 4=2x2, 6=2x3, 10=2x5 Primes: {2, 3, 5} ALGORITHM STEPS 1 Update Array Apply query: nums[idx]=val 2 Prefix Prime Count Distinct primes in [0..k-1] 3 Suffix Prime Count Distinct primes in [k..n-1] 4 Maximize Split Find best k for max sum Query 1: [1,3] --> [4,3,6,10] k=1: {2} + {2,3,5} = 1+3 = 4? No k=2: {3} + {2,3,5} = 1+3 = 4? No Best split: 3 distinct primes Query 2: [3,7] --> [4,3,6,7] k=2: {2,3} + {2,3,7} = 2+3 = 5? No Best split: 3 distinct primes FINAL RESULT Output Array: [3, 3] Query 1 Result: 3 Array: [4,3,6,10] Max distinct primes: 3 (primes: 2, 3, 5) Query 2 Result: 3 Array: [4,3,6,7] Max distinct primes: 3 (primes: 2, 3, 7) OK - Verified Key Insight: Precompute prefix and suffix distinct prime counts. For each split point k, the answer is prefix[k] + suffix[k]. Track primes using sets for each factor. Updates modify both structures. Time: O(Q * N * sqrt(max_val)) | Space: O(N) for prefix/suffix arrays TutorialsPoint - Maximize Count of Distinct Primes After Split | Prefix/Suffix Precomputation Approach
Asked in
Google 25 Amazon 18 Microsoft 15
8.9K Views
Medium Frequency
~35 min Avg. Time
234 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