Good Subsequence Queries Problem
Description
LeetCode Problem 3901.
You are given an integer array nums of length n and an integer p.
A non-empty subsequence of nums is called good if:
- Its length is strictly less than
n. - The greatest common divisor (GCD) of its elements is exactly
p.
You are also given a 2D integer array queries of length q, where each queries[i] = [ind_i, val_i] indicates that you should update nums[ind_i] to val_i.
After each query, determine whether there exists any good subsequence in the current array.
Return the number of queries for which a good subsequence exists.
The term gcd(a, b) denotes the greatest common divisor of a and b.
Example 1:
Input: nums = [4,8,12,16], p = 2, queries = [[0,3],[2,6]]
Output: 1
Explanation:
i [ind_i, val_i] Operation Updated nums Any good Subsequence
0 [0, 3] Update nums[0] to 3 [3, 8, 12, 16] No, as no subsequence has GCD exactly p = 2
1 [2, 6] Update nums[2] to 6 [3, 8, 6, 16] Yes, subsequence [8, 6] has GCD exactly p = 2
Thus, the answer is 1.
Example 2:
Input: nums = [4,5,7,8], p = 3, queries = [[0,6],[1,9],[2,3]]
Output: 2
Explanation:
i [ind_i, val_i] Operation Updated nums Any good Subsequence
0 [0, 6] Update nums[0] to 6 [6, 5, 7, 8] No, as no subsequence has GCD exactly p = 3
1 [1, 9] Update nums[1] to 9 [6, 9, 7, 8] Yes, subsequence [6, 9] has GCD exactly p = 3
2 [2, 3] Update nums[2] to 3 [6, 9, 3, 8] Yes, subsequence [6, 9, 3] has GCD exactly p = 3
Thus, the answer is 2.
Example 3:
Input: nums = [5,7,9], p = 2, queries = [[1,4],[2,8]]
Output: 0
Explanation:
i [ind_i, val_i] Operation Updated nums Any good Subsequence
0 [1, 4] Update nums[1] to 4 [5, 4, 9] No, as no subsequence has GCD exactly p = 2
1 [2, 8] Update nums[2] to 8 [5, 4, 8] No, as no subsequence has GCD exactly p = 2
Thus, the answer is 0.
Constraints:
2 <= n == nums.length <= 5 * 10^41 <= nums[i] <= 5 * 10^41 <= queries.length <= 5 * 10^4queries[i] = [ind_i, val_i]1 <= val_i, p <= 5 * 10^40 <= ind_i <= n - 1
Solution
Approach: Segment Tree + GCD
Only multiples of p can be part of a subsequence whose GCD is exactly p. So keep a segment tree over the array where position i stores nums[i] if it is divisible by p, and 0 otherwise (gcd(0, x) = x). The root then holds g, the GCD of all multiples of p, and cnt is how many there are.
After each update, decide as follows:
- If
g != p, no subsequence has GCD exactlyp. - If
g == pandcnt < n, take all the multiples ofp: that is a subsequence shorter thannwith GCDp. - If
g == pandcnt == n, we must drop at least one element and still keep GCDp. Forn > 6this is always possible: after dividing byp, values are at most5 * 10^4, and a set with GCD 1 never needs more than 6 elements to keep it (the product of the first 7 primes already exceeds5 * 10^4), so some element is redundant. Forn <= 6, try removing each element in turn, using two range queries (prefix GCD and suffix GCD) on the tree.
Each update changes one leaf, which costs O(log n) GCD merges, and the brute-force check only happens for tiny arrays.
Complexity:
- Time complexity:
O((n + q) log n)times the cost of a GCD. - Space complexity:
O(n).
from math import gcd
class Node:
__slots__ = "l", "r", "g"
def __init__(self, l: int, r: int):
self.l = l
self.r = r
self.g = 0
class SegmentTree:
__slots__ = "tr"
def __init__(self, n: int):
self.tr: Optional[List[Node]] = [None] * (n << 2)
self.build(1, 1, n)
def build(self, u: int, l: int, r: int):
self.tr[u] = Node(l, r)
if l == r:
return
mid = (l + r) >> 1
self.build(u << 1, l, mid)
self.build(u << 1 | 1, mid + 1, r)
def pushup(self, u: int):
self.tr[u].g = gcd(self.tr[u << 1].g, self.tr[u << 1 | 1].g)
def modify(self, u: int, x: int, v: int):
if self.tr[u].l == self.tr[u].r:
self.tr[u].g = v
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) -> int:
if l > r:
return 0
if self.tr[u].l >= l and self.tr[u].r <= r:
return self.tr[u].g
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)
return gcd(self.query(u << 1, l, mid), self.query(u << 1 | 1, mid + 1, r))
class Solution:
def countGoodSubseq(self, nums: List[int], p: int, queries: List[List[int]]) -> int:
n = len(nums)
tree = SegmentTree(n)
cnt = 0
for i, x in enumerate(nums, 1):
if x % p == 0:
tree.modify(1, i, x)
cnt += 1
ans = 0
for idx, val in queries:
if nums[idx] % p == 0:
tree.modify(1, idx + 1, 0)
cnt -= 1
if val % p == 0:
tree.modify(1, idx + 1, val)
cnt += 1
nums[idx] = val
if tree.tr[1].g != p:
continue
if cnt < n or n > 6:
ans += 1
continue
for i in range(1, n + 1):
left_g = tree.query(1, 1, i - 1)
right_g = tree.query(1, i + 1, n)
if gcd(left_g, right_g) == p:
ans += 1
break
return ans