Product Remainder Counts After Array Updates

Quick Overview

Process persistent array updates and count suffix removals whose remaining product has a requested remainder. Each query fixes the starting position and leaves a nonempty array.

Product Remainder Counts After Array Updates

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Other

# Product Remainder Counts After Array Updates You are given a positive integer array `nums`, an integer `k`, and queries of the form `[index, value, start, x]`. Process the queries in order. For each query: 1. Replace `nums[index]` with `value`. This update remains in effect for later queries. 2. Temporarily remove every element before index `start`. 3. Choose a suffix to remove from the remaining array, leaving at least one element. The removed suffix may be empty. Count how many suffix choices make the product of the elements left in the array have remainder `x` modulo `k`. Return these counts in query order. Only the point updates persist; neither removal changes the array used by later queries. ### C++ Interface ```cpp class Solution { public: vector<int> resultArray(vector<int>& nums, int k, vector<vector<int>>& queries); }; ``` ### Examples **Example 1** ```text nums = [2,3,5,2] k = 4 queries = [[1,2,0,0],[0,1,1,2],[2,3,0,2]] Output: [3,2,2] ``` After the first update, the possible products are `2, 4, 20, 40`; three have remainder zero. The second query uses the updated array `[1,2,5,2]` starting at index `1`, giving products `2, 10, 20`; two have remainder two. The final array is `[1,2,3,2]`, whose products starting at index zero are `1, 2, 6, 12`; two have remainder two. **Example 2** ```text nums = [6,7,8] k = 1 queries = [[2,9,2,0],[0,2,0,0]] Output: [1,3] ``` The first query leaves only the last position available. The second has three possible nonempty arrays to retain. Every product has remainder zero modulo one. ### Constraints - `1 <= nums.length <= 100000`. - `1 <= nums[i] <= 1000000000`. - `1 <= k <= 5`. - `1 <= queries.length <= 20000`. - Every query contains exactly `[index, value, start, x]`. - `0 <= index, start < nums.length`. - `1 <= value <= 1000000000` and `0 <= x < k`. ### Clarifying Questions - Is `start` chosen freely? No. Each query fixes the prefix removal; only the suffix removal is counted. - Can the entire array remaining after the prefix removal be kept? Yes, by removing the empty suffix. Removing all remaining elements is forbidden. - Can an update before `start` affect a later query? Yes. The update persists even when that position is outside the current query's remaining array. - Is `k` necessarily prime? No. Every integer from one through five is allowed. ### What a Strong Answer Covers - The distinction between persistent replacements and temporary removals. - Exact counting of suffix choices, including removing nothing and excluding an empty result. - Correct behavior for repeated updates, `start` at either end, and `k = 1`. - Time and memory bounds suitable for the full array and query limits. ### Follow-up Questions - For a fixed query, when can the answer equal `nums.length - start`, and when can it be zero? - Can a replacement outside the current remaining array change this query's answer? Explain which positions are relevant. ```hint Summarize a range For each segment, keep its total product remainder and the counts of its nonempty prefix product remainders. Work out how to concatenate two adjacent summaries in order, then support point updates and a query on nums[start..n-1]. ```

Overview: Process persistent array updates and count suffix removals whose remaining product has a requested remainder. Each query fixes the starting position and leaves a nonempty array.

Google logo
Google
May 2, 2026
hardSoftware EngineerOtherCoding & Algorithms
0
0

Product Remainder Counts After Array Updates

You are given a positive integer array nums, an integer k, and queries of the form [index, value, start, x]. Process the queries in order.

For each query:

  1. Replace nums[index] with value . This update remains in effect for later queries.
  2. Temporarily remove every element before index start .
  3. Choose a suffix to remove from the remaining array, leaving at least one element. The removed suffix may be empty.

Count how many suffix choices make the product of the elements left in the array have remainder x modulo k. Return these counts in query order. Only the point updates persist; neither removal changes the array used by later queries.

C++ Interface

class Solution {
public:
    vector<int> resultArray(vector<int>& nums, int k, vector<vector<int>>& queries);
};

Examples

Example 1

nums = [2,3,5,2]
k = 4
queries = [[1,2,0,0],[0,1,1,2],[2,3,0,2]]
Output: [3,2,2]

After the first update, the possible products are 2, 4, 20, 40; three have remainder zero. The second query uses the updated array [1,2,5,2] starting at index 1, giving products 2, 10, 20; two have remainder two. The final array is [1,2,3,2], whose products starting at index zero are 1, 2, 6, 12; two have remainder two.

Example 2

nums = [6,7,8]
k = 1
queries = [[2,9,2,0],[0,2,0,0]]
Output: [1,3]

The first query leaves only the last position available. The second has three possible nonempty arrays to retain. Every product has remainder zero modulo one.

Constraints

  • 1 <= nums.length <= 100000 .
  • 1 <= nums[i] <= 1000000000 .
  • 1 <= k <= 5 .
  • 1 <= queries.length <= 20000 .
  • Every query contains exactly [index, value, start, x] .
  • 0 <= index, start < nums.length .
  • 1 <= value <= 1000000000 and 0 <= x < k .

Clarifying Questions Guidance

  • Is start chosen freely? No. Each query fixes the prefix removal; only the suffix removal is counted.
  • Can the entire array remaining after the prefix removal be kept? Yes, by removing the empty suffix. Removing all remaining elements is forbidden.
  • Can an update before start affect a later query? Yes. The update persists even when that position is outside the current query's remaining array.
  • Is k necessarily prime? No. Every integer from one through five is allowed.

What a Strong Answer Covers Guidance

  • The distinction between persistent replacements and temporary removals.
  • Exact counting of suffix choices, including removing nothing and excluding an empty result.
  • Correct behavior for repeated updates, start at either end, and k = 1 .
  • Time and memory bounds suitable for the full array and query limits.

Follow-up Questions Guidance

  • For a fixed query, when can the answer equal nums.length - start , and when can it be zero?
  • Can a replacement outside the current remaining array change this query's answer? Explain which positions are relevant.