Find X Value of Array II Problem
Description
LeetCode Problem 3525.
You are given an array of positive integers nums and a positive integer k. You are also given a 2D array queries, where queries[i] = [index_i, value_i, start_i, x_i].
You are allowed to perform an operation once on nums, where you can remove any suffix from nums such that nums remains non-empty.
The x-value of nums for a given x is defined as the number of ways to perform this operation so that the product of the remaining elements leaves a remainder of x modulo k.
For each query in queries you need to determine the x-value of nums for x_i after performing the following actions:
- Update
nums[index_i]tovalue_i. Only this step persists for the rest of the queries. - Remove the prefix
nums[0..(start_i - 1)](wherenums[0..(-1)]will be used to represent the empty prefix).
Return an array result of size queries.length where result[i] is the answer for the i^th query.
A prefix of an array is a subarray that starts from the beginning of the array and extends to any point within it.
A suffix of an array is a subarray that starts at any point within the array and extends to the end of the array.
Note that the prefix and suffix to be chosen for the operation can be empty.
Note that x-value has a different definition in this version.
Example 1:
Input: nums = [1,2,3,4,5], k = 3, queries = [[2,2,0,2],[3,3,3,0],[0,1,0,1]]
Output: [2,2,2]
Explanation:
- For query 0, nums becomes [1, 2, 2, 4, 5], and the empty prefix must be removed. The possible operations are:
- Remove the suffix [2, 4, 5]. nums becomes [1, 2].
- Remove the empty suffix. nums becomes [1, 2, 2, 4, 5] with a product 80, which gives remainder 2 when divided by 3.
- For query 1, nums becomes [1, 2, 2, 3, 5], and the prefix [1, 2, 2] must be removed. The possible operations are:
- Remove the empty suffix. nums becomes [3, 5].
- Remove the suffix [5]. nums becomes [3].
- For query 2, nums becomes [1, 2, 2, 3, 5], and the empty prefix must be removed. The possible operations are:
- Remove the suffix [2, 2, 3, 5]. nums becomes [1].
- Remove the suffix [3, 5]. nums becomes [1, 2, 2].
Example 2:
Input: nums = [1,2,4,8,16,32], k = 4, queries = [[0,2,0,2],[0,2,0,1]]
Output: [1,0]
Explanation:
- For query 0, nums becomes [2, 2, 4, 8, 16, 32]. The only possible operation is:
- Remove the suffix [2, 4, 8, 16, 32].
- For query 1, nums becomes [2, 2, 4, 8, 16, 32]. There is no possible way to perform the operation.
Example 3:
Input: nums = [1,1,2,1,1], k = 2, queries = [[2,1,0,1]]
Output: [5]
Constraints:
1 <= nums[i] <= 10^91 <= nums.length <= 10^51 <= k <= 51 <= queries.length <= 2 * 10^4queries[i] == [index_i, value_i, start_i, x_i]0 <= index_i <= nums.length - 11 <= value_i <= 10^90 <= start_i <= nums.length - 10 <= x_i <= k - 1
Solution
Approach: Segment Tree of Prefix Remainders
Each query first sets nums[index] = value (the change stays), then removes the first start elements. Only a suffix can still be removed, so the remaining array is a non-empty prefix of nums[start..]. We therefore need the number of prefixes of nums[start..] whose product has remainder x modulo k.
Since k <= 5, every segment tree node can store the product of its range modulo k and an array cnt[r], the number of prefixes of the range with product remainder r. Two neighbouring nodes L and R merge as follows: the product is L.prod * R.prod % k; prefixes inside L are kept; and each prefix of R extended by all of L adds R.cnt[r] to remainder L.prod * r % k.
After the point update, query the range [start, n - 1], merging nodes from left to right, and read cnt[x].
Complexity: O((n + q) * k log n) time and O(n * k) space.
class Node:
__slots__ = "l", "r", "prod", "cnt"
def __init__(self, l: int, r: int, k: int):
self.l = l
self.r = r
self.prod = 1
self.cnt = [0] * k
class SegmentTree:
__slots__ = "k", "tr"
def __init__(self, nums: List[int], k: int):
self.k = k
n = len(nums)
self.tr = [None] * (n << 2)
self.build(1, 1, n, nums)
def merge(self, a: Node, b: Node) -> Tuple[int, List[int]]:
k = self.k
prod = a.prod * b.prod % k
cnt = a.cnt[:]
for r, c in enumerate(b.cnt):
cnt[a.prod * r % k] += c
return prod, cnt
def pushup(self, u: int):
prod, cnt = self.merge(self.tr[u << 1], self.tr[u << 1 | 1])
self.tr[u].prod = prod
self.tr[u].cnt = cnt
def build(self, u: int, l: int, r: int, nums: List[int]):
self.tr[u] = Node(l, r, self.k)
if l == r:
v = nums[l - 1] % self.k
self.tr[u].prod = v
self.tr[u].cnt[v] = 1
return
mid = (l + r) >> 1
self.build(u << 1, l, mid, nums)
self.build(u << 1 | 1, mid + 1, r, nums)
self.pushup(u)
def modify(self, u: int, x: int, v: int):
if self.tr[u].l == self.tr[u].r:
v %= self.k
self.tr[u].prod = v
self.tr[u].cnt = [0] * self.k
self.tr[u].cnt[v] = 1
return
mid = (self.tr[u].l + self.tr[u].r) >> 1
if x <= mid:
self.modify(u << 1, x, v)
else:
self.modify(u << 1 | 1, x, v)
self.pushup(u)
def query(self, u: int, l: int, r: int) -> Node:
if self.tr[u].l >= l and self.tr[u].r <= r:
return self.tr[u]
mid = (self.tr[u].l + self.tr[u].r) >> 1
if r <= mid:
return self.query(u << 1, l, r)
if l > mid:
return self.query(u << 1 | 1, l, r)
left = self.query(u << 1, l, r)
right = self.query(u << 1 | 1, l, r)
prod, cnt = self.merge(left, right)
res = Node(0, 0, self.k)
res.prod = prod
res.cnt = cnt
return res
class Solution:
def resultArray(
self, nums: List[int], k: int, queries: List[List[int]]
) -> List[int]:
n = len(nums)
tree = SegmentTree(nums, k)
ans = []
for idx, val, start, x in queries:
tree.modify(1, idx + 1, val)
ans.append(tree.query(1, start + 1, n).cnt[x])
return ans