Maximum Valid Split Positions II Problem
Description
LeetCode Problem 4037.
You are given an integer array nums.
You may remove at most one element from nums. Let arr be the array of remaining elements in their original order, and let m be its length.
A split position i of arr is valid if:
0 <= i < m - 1, andgcd(arr[0..i]) == gcd(arr[i + 1..m - 1]).
An array of length 1 has no valid split positions.
The score of arr is the number of valid split positions in it.
Return the maximum possible score of arr.
Here, gcd(a) denotes the greatest common divisor of all elements in the array a.
Example 1:
Input: nums = [10,30,15,10]
Output: 2
Explanation:
One optimal solution is to remove nums[2] = 15. Then arr = [10, 30, 10].
The split positions are:
Split Position i gcd(arr[0..i]) gcd(arr[i + 1..m - 1])
0 10 10
1 10 10
All split positions are valid. Thus, the answer is 2.
Example 2:
Input: nums = [2,10,14]
Output: 1
Explanation:
One optimal solution is to not remove any element. Then arr = [2, 10, 14].
The split positions are:
Split Position i gcd(arr[0..i]) gcd(arr[i + 1..m - 1])
0 2 2
1 2 14
Only the split position at index 0 is valid. Thus, the answer is 1.
Example 3:
Input: nums = [2,4]
Output: 0
Explanation:
The only remaining array that has a split position is arr = [2, 4].
The split positions are:
Split Position i gcd(arr[0..i]) gcd(arr[i + 1..m - 1])
0 2 4
There are no valid split positions. Thus, the answer is 0.
Constraints:
2 <= nums.length <= 10^51 <= nums[i] <= 10^9
Solution
Approach: Prefix and suffix GCDs, trying only a few removals
As in the first version, for an array arr the split i is valid exactly when pre[i] == suf[i + 1], where pre and suf are the prefix and suffix GCD arrays. With n up to 10^5 we cannot afford an O(n) recount for every removed index.
Each prefix GCD divides the previous one, so whenever it changes it at least halves: the prefix GCD changes at most O(log M) times, and the same holds for the suffix GCD. Suppose the prefix GCD does not change at index i and the suffix GCD does not change there either. Then removing nums[i] leaves every other prefix and suffix GCD as it was, and the only effect is to merge the neighbouring split positions i - 1 and i, which were both valid or both invalid. The score can only drop, so such a removal is never needed.
So only the indices where the prefix GCD or the suffix GCD changes are tried (the prefix and suffix GCD arrays of nums show them; the first and last index always count), at most about 2 log M of them. Compute the score of each resulting array with calc and take the maximum together with the score of the untouched array.
Complexity: time O(n log^2 M), space O(n), where n = nums.length and M is the largest value.
from itertools import accumulate
from math import gcd
from operator import eq
class Solution:
def maxValidSplits(self, nums: List[int]) -> int:
n = len(nums)
def calc(arr):
pre = list(accumulate(arr, gcd))
suf = list(accumulate(reversed(arr), gcd))[::-1]
return sum(map(eq, pre[:-1], suf[1:]))
pre = list(accumulate(nums, gcd))
suf = list(accumulate(reversed(nums), gcd))[::-1]
ans = sum(map(eq, pre[:-1], suf[1:]))
for i in range(n):
# only removing an element where the prefix or the suffix gcd changes can help
if i == 0 or i == n - 1 or pre[i] != pre[i - 1] or suf[i] != suf[i + 1]:
ans = max(ans, calc(nums[:i] + nums[i + 1:]))
return ans