Find X Value of Array II - Problem
Find X Value of Array II

Imagine you're managing a dynamic product catalog where you need to analyze different product combinations and their modular arithmetic properties after various updates and filtering operations.

Given an array nums of positive integers and a positive integer k, you need to process multiple queries. Each query contains:
index_i: Position to update
value_i: New value at that position
start_i: Starting position for analysis
x_i: Target remainder value

For each query, you must:
1. Update nums[index_i] to value_i (persists for subsequent queries)
2. Remove prefix from index 0 to start_i - 1
3. Calculate x-value: Count how many ways you can remove a suffix such that the product of remaining elements ≡ x_i (mod k)

The x-value represents the number of valid suffix removals that result in the desired modular product. Return an array where each element is the x-value for the corresponding query.

Input & Output

example_1.py — Basic Query Processing
$ Input: nums = [2, 3, 4], k = 6, queries = [[1, 5, 0, 2]]
Output: [1]
💡 Note: After updating nums[1] = 5, array becomes [2, 5, 4]. Starting from index 0, we check suffixes: [2] has product 2 ≡ 2 (mod 6) ✓, [2,5] has product 10 ≡ 4 (mod 6), [2,5,4] has product 40 ≡ 4 (mod 6). Only one way gives remainder 2.
example_2.py — Multiple Queries
$ Input: nums = [1, 2, 3], k = 5, queries = [[0, 4, 1, 1], [2, 6, 0, 4]]
Output: [0, 1]
💡 Note: Query 1: Update nums[0]=4, start from index 1, array becomes [2,3], check remainder 1. Products: 2≡2, 6≡1. Count=1. Wait, that's wrong. Let me recalculate: [2] gives 2 mod 5, [2,3] gives 6≡1 mod 5, so count=1. Query 2: nums[2]=6, start from 0, array [4,2,6]. Products: 4≡4✓. Count=1.
example_3.py — Edge Case
$ Input: nums = [1], k = 3, queries = [[0, 2, 0, 2]]
Output: [1]
💡 Note: Single element array. Update nums[0]=2, starting from index 0. Only one suffix possible: [2], which gives product 2 ≡ 2 (mod 3). Matches target x=2, so count=1.

Constraints

  • 1 ≤ nums.length ≤ 105
  • 1 ≤ nums[i] ≤ 109
  • 1 ≤ k ≤ 109
  • 1 ≤ queries.length ≤ 105
  • 0 ≤ index_i < nums.length
  • 1 ≤ value_i ≤ 109
  • 0 ≤ start_i ≤ nums.length

Visualization

Tap to expand
Assembly Line Production AnalysisUpdateStation 1Skip Prefix StationsActive Production LineProduction Stations:5246Quality Check Results:Stop at Station 1: Quality = 5 % kStop at Station 2: Quality = 10 % kStop at Station 3: Quality = 40 % kStop at Station 4: Quality = 240 % kCount: 1 valid configuration found!
Understanding the Visualization
1
Update Equipment
Modify the specified station's output value
2
Skip Initial Stations
Remove the prefix - start production from a specific station
3
Test All Stop Points
For each possible stopping point, check if the total production meets quality target
4
Count Valid Runs
Sum up all configurations that achieve the desired quality score (modulo k)
Key Takeaway
🎯 Key Insight: Use prefix products to transform O(N²) repeated multiplications into O(N) direct lookups, dramatically improving performance for multiple queries.
Asked in
Google 15 Amazon 12 Meta 8 Microsoft 6
26.8K Views
Medium Frequency
~35 min Avg. Time
892 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