Minimum Stability Factor of Array Problem
Description
LeetCode Problem 3605.
You are given an integer array nums and an integer maxC.
A subarray is called stable if the highest common factor (HCF) of all its elements is greater than or equal to 2.
The stability factor of an array is defined as the length of its longest stable subarray.
You may modify at most maxC elements of the array to any integer.
Return the minimum possible stability factor of the array after at most maxC modifications. If no stable subarray remains, return 0.
Note:
- The highest common factor (HCF) of an array is the largest integer that evenly divides all the array elements.
- A subarray of length 1 is stable if its only element is greater than or equal to 2, since
HCF([x]) = x.
Example 1:
Input: nums = [3,5,10], maxC = 1
Output: 1
Explanation:
- The stable subarray [5, 10] has HCF = 5, which has a stability factor of 2.
- Since maxC = 1, one optimal strategy is to change nums[1] to 7, resulting in nums = [3, 7, 10].
- Now, no subarray of length greater than 1 has HCF >= 2. Thus, the minimum possible stability factor is 1.
Example 2:
Input: nums = [2,6,8], maxC = 2
Output: 1
Explanation:
- The subarray [2, 6, 8] has HCF = 2, which has a stability factor of 3.
- Since maxC = 2, one optimal strategy is to change nums[1] to 3 and nums[2] to 5, resulting in nums = [2, 3, 5].
- Now, no subarray of length greater than 1 has HCF >= 2. Thus, the minimum possible stability factor is 1.
Example 3:
Input: nums = [2,4,9,6], maxC = 1
Output: 2
Explanation:
- The stable subarrays are:
- [2, 4] with HCF = 2 and stability factor of 2.
- [9, 6] with HCF = 3 and stability factor of 2.
- Since maxC = 1, the stability factor of 2 cannot be reduced due to two separate stable subarrays. Thus, the minimum possible stability factor is 2.
Constraints:
1 <= n == nums.length <= 10^51 <= nums[i] <= 10^90 <= maxC <= n
Solution
Approach: Binary Search + Greedy + Sparse Table
If the stability factor can be made at most L, it can also be made at most L + 1, so binary search on L.
To check L, no window of length L + 1 may have gcd at least 2. Scan left to right: when the window starting at i has gcd at least 2, change its last element (to 1, which breaks every window that contains it), count one change, and continue after that element. Changing the last element covers as many following windows as possible, so the greedy is optimal. The check passes if the count is at most maxC.
Window gcds come from a sparse table of gcds, which answers any range with one gcd operation because gcd is unaffected by overlap.
Complexity: O(n log n log A) time for the table and for the search, and O(n log n) extra space, where A is the largest value.
from math import gcd
class Solution:
def minStable(self, nums: List[int], maxC: int) -> int:
n = len(nums)
# sparse table of gcds
st = [nums[:]]
j = 1
while (1 << j) <= n:
prev = st[-1]
half = 1 << (j - 1)
st.append([gcd(prev[i], prev[i + half]) for i in range(n - (1 << j) + 1)])
j += 1
def window_gcd(l, r):
k = (r - l + 1).bit_length() - 1
return gcd(st[k][l], st[k][r - (1 << k) + 1])
def changes(L):
# fewest changes so that no window of length L + 1 has gcd >= 2
cnt = 0
i = 0
while i + L < n:
if window_gcd(i, i + L) >= 2:
cnt += 1
i += L + 1
else:
i += 1
return cnt
lo, hi = 0, n
while lo < hi:
mid = (lo + hi) // 2
if changes(mid) <= maxC:
hi = mid
else:
lo = mid + 1
return lo