Maximum Valid Split Positions I - Solution & Explanation
Practice this problemProblem Statement
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 <= 10001 <= nums[i] <= 109
Approach Overview
Problem Overview: Given an array of positive integers, you can remove exactly one element to split the array into a non-empty prefix and suffix. A split is valid if the GCD (greatest common divisor) of the prefix and the GCD of the suffix are coprime (i.e., their GCD is 1). Return the maximum number of valid split positions across all possible removals.
Approach 1: Brute Force with GCD Recalculation (O(n^2 log A) Time, O(1) Space)
For each possible removal index, compute the GCD of the prefix and suffix from scratch using a loop. This means for every removal you iterate through the entire array, leading to O(n^2) GCD computations. While simple to implement, it fails for large inputs because each GCD call takes O(log A) time, making the total O(n^2 log A). Use this only for small arrays or to verify correctness of an optimal solution.
Approach 2: Prefix and Suffix GCD Precomputation (O(n log A) Time, O(n) Space)
Precompute two arrays: prefixGCD[i] = GCD of elements from index 0 to i, and suffixGCD[i] = GCD of elements from index i to n-1. After removing index k, the prefix GCD is prefixGCD[k-1] (if k > 0) and the suffix GCD is suffixGCD[k+1] (if k < n-1). Then check if gcd(prefixGCD[k-1], suffixGCD[k+1]) == 1. This reduces each query to O(log A) for the final GCD check, giving total O(n log A). This is the optimal approach because it avoids redundant GCD computations and is easy to implement in any language.
Recommended for interviews: Interviewers expect the prefix/suffix precomputation approach because it demonstrates understanding of how to avoid repeated work. The brute force shows you can reason about the problem, but the optimal solution shows you can optimize using auxiliary arrays. Always start with brute force to clarify the problem, then transition to the optimal solution.
This problem is a classic example of array manipulation combined with mathematics (GCD). You can also relate it to prefix-sum techniques, though here we use prefix GCD instead of sum.
Solution
Since the array length satisfies n leq 1000, we can enumerate the index of the removed element (including the case where nothing is removed) to obtain the array arr, compute the score of arr, and take the maximum over all cases.
For an array arr of length m, we precompute the prefix GCD array pre and the suffix GCD array suf, where pre[i] = \gcd(arr[0..i]) and suf[i] = \gcd(arr[i..m - 1]). A split position i is valid if and only if pre[i] = suf[i + 1], so the score of arr is the number of indices satisfying this condition.
The time complexity is O(n^2 times log M), and the space complexity is O(n). Here, n is the length of the array nums, and M is the maximum value in the array nums.
Code
Python
Java
C++
Go
TypeScript
Detailed Complexity Analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force | O(n^2 log A) | O(1) | Small arrays or verifying correctness |
| Prefix & Suffix GCD Precomputation | O(n log A) | O(n) | General case, optimal for large inputs |
Video Solutions (2)
LeetCode Biweekly Contest 190 🔥 | 3 Problems Solved | Q1–Q3 | 4034, 4035, 4036
Frequently Asked Questions
Is Maximum Valid Split Positions I easy or hard?
Maximum Valid Split Positions I Python/Java solution
How to solve Maximum Valid Split Positions I in O(n)?
What is the best approach for Maximum Valid Split Positions I?
Is Maximum Valid Split Positions I asked at Google/Amazon/Meta?
What data structure is used in Maximum Valid Split Positions I?
What is the time complexity of Maximum Valid Split Positions I?
Ready to solve this problem?
Practice Maximum Valid Split Positions I with our built-in code editor and test cases.
Practice on FleetCodeProblem Info
Table of Contents
Practice this problem
Open in Editor