Minimum Split Into Subarrays With GCD Greater Than One - Problem
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
- 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.
Input & Output
Example 1 — Basic Split
$
Input:
nums = [12,6,3,14,8]
›
Output:
2
💡 Note:
Split into [12,6,3] with GCD=3 and [14,8] with GCD=2. Both have GCD > 1, so minimum splits = 2.
Example 2 — Single Element
$
Input:
nums = [4]
›
Output:
1
💡 Note:
Single element [4] has GCD=4 > 1, so we need only 1 subarray.
Example 3 — Each Element Separate
$
Input:
nums = [6,10,15]
›
Output:
3
💡 Note:
GCD(6,10)=2, but adding 15 makes GCD(6,10,15)=1. So split as [6],[10],[15] with 3 subarrays.
Constraints
- 1 ≤ nums.length ≤ 1000
- 1 ≤ nums[i] ≤ 109
Visualization
Tap to expand
Understanding the Visualization
1
Input Array
Array of positive integers [12,6,3,14,8]
2
Find Valid Splits
Each subarray must have GCD > 1
3
Minimize Count
Return minimum number of subarrays needed
Key Takeaway
🎯 Key Insight: Greedily extend subarrays while GCD > 1, or use DP for guaranteed optimal solution
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code