Minimum Split Into Subarrays With GCD Greater Than One - Solution & Explanation
Problem Statement
You are given an array nums consisting of positive integers.
Split the array into one or more disjoint subarrays such that:
- Each element of the array belongs to exactly one subarray, and
- The GCD of the elements of each subarray is strictly greater than
1.
Return the minimum number of subarrays that can be obtained after the split.
Note that:
- The GCD of a subarray is the largest positive integer that evenly divides all the elements of the subarray.
- A subarray is a contiguous part of the array.
Example 1:
Input: nums = [12,6,3,14,8] Output: 2 Explanation: We can split the array into the subarrays: [12,6,3] and [14,8]. - The GCD of 12, 6 and 3 is 3, which is strictly greater than 1. - The GCD of 14 and 8 is 2, which is strictly greater than 1. It can be shown that splitting the array into one subarray will make the GCD = 1.
Example 2:
Input: nums = [4,12,6,14] Output: 1 Explanation: We can split the array into only one subarray, which is the whole array.
Constraints:
1 <= nums.length <= 20002 <= nums[i] <= 109
Approach Overview
Problem Overview: Given an integer array, split it into the minimum number of contiguous subarrays such that the gcd of every subarray is strictly greater than 1. If a segment’s GCD becomes 1, that segment is invalid and must be split earlier.
Approach 1: Dynamic Programming with GCD Tracking (O(n^2 log A) time, O(n) space)
Define dp[i] as the minimum splits needed for the prefix ending at index i. For each position, iterate backward and continuously compute the GCD of the current segment using gcd(g, nums[j]). As long as the GCD remains greater than 1, update dp[i] from dp[j-1]. Once the GCD becomes 1, extending the segment further is useless. This approach demonstrates the core idea of segment GCD validity but becomes expensive because each index may scan many previous elements.
Approach 2: Greedy + Mathematics (O(n log A) time, O(1) space)
Traverse the array while maintaining the GCD of the current subarray. Start with the first number and keep updating current_gcd = gcd(current_gcd, nums[i]). If the GCD stays greater than 1, the current segment remains valid and you continue extending it. When the GCD becomes 1, the current segment can no longer satisfy the requirement. Split right before this element, increment the segment count, and restart the segment with nums[i] as the new base. Each element participates in a constant number of GCD operations, making the solution linear with a logarithmic factor from the GCD calculation.
This greedy logic works because once the running GCD drops to 1, adding more elements can never increase it back above 1. The only valid move is to start a new subarray immediately.
Key concepts come from array traversal, number theory (GCD properties), and optimization ideas often seen in dynamic programming problems.
Recommended for interviews: Greedy + Mathematics. Interviewers expect you to recognize the monotonic property of GCD: once it reaches 1, it cannot recover. The DP formulation shows understanding of the state definition, but the greedy observation demonstrates stronger algorithmic intuition and reduces complexity to O(n log A).
Solution
For each element in the array, if its greatest common divisor (gcd) with the previous element is 1, then it needs to be the first element of a new subarray. Otherwise, it can be placed in the same subarray with the previous elements.
Therefore, we first initialize a variable g, representing the gcd of the current subarray. Initially, g=0 and the answer variable ans=1.
Next, we traverse the array from front to back, maintaining the gcd g of the current subarray. If the gcd of the current element x and g is 1, then we need to make the current element the first element of a new subarray. Therefore, the answer increases by 1, and g is updated to x. Otherwise, the current element can be placed in the same subarray with the previous elements. Continue to traverse the array until the traversal ends.
The time complexity is O(n times log m), where n and m are the length of the array and the maximum value in the array, respectively. The space complexity is O(1).
Detailed Complexity Analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Dynamic Programming with GCD Tracking | O(n^2 log A) | O(n) | Useful for understanding the state transition and exploring all valid segment boundaries |
| Greedy + Mathematics (Running GCD) | O(n log A) | O(1) | Best general solution; optimal for large arrays with simple linear traversal |
Video Solution
leetcode 2436. Minimum Split Into Subarrays With GCD Greater Than One - gcd and check • Code-Yao • 195 views views
Watch 2 more video solutions →Frequently Asked Questions
Is Minimum Split Into Subarrays With GCD Greater Than One easy or hard?
Minimum Split Into Subarrays With GCD Greater Than One Python/Java solution
How to solve Minimum Split Into Subarrays With GCD Greater Than One in O(n)?
What is the best approach for Minimum Split Into Subarrays With GCD Greater Than One?
Is Minimum Split Into Subarrays With GCD Greater Than One asked at Google/Amazon/Meta?
What data structure is used in Minimum Split Into Subarrays With GCD Greater Than One?
What is the time complexity of Minimum Split Into Subarrays With GCD Greater Than One?
Ready to solve this problem?
Practice Minimum Split Into Subarrays With GCD Greater Than One with our built-in code editor and test cases.
Practice on FleetCodeProblem Info
Table of Contents
Practice this problem
Open in Editor