Minimum Subarrays in a Valid Split - Problem

You're given an integer array nums and need to split it into the minimum number of subarrays following a special rule.

The Rule: Each subarray must have its first and last elements share a common divisor greater than 1. In other words, gcd(first_element, last_element) > 1 for every subarray.

Your Mission: Find the minimum number of subarrays needed to split the entire array. If it's impossible to create a valid split, return -1.

Note: The GCD (Greatest Common Divisor) of two numbers is the largest positive integer that divides both numbers evenly. A subarray is a contiguous part of the original array.

Example: For array [4, 6, 15, 35], one valid split is [4, 6] and [15, 35] because gcd(4,6) = 2 > 1 and gcd(15,35) = 5 > 1.

Input & Output

example_1.py โ€” Basic Valid Split
$ Input: [4, 6, 15, 35]
โ€บ Output: 2
๐Ÿ’ก Note: We can split into [4, 6] and [15, 35]. gcd(4,6) = 2 > 1 and gcd(15,35) = 5 > 1, so both subarrays are valid. This gives us 2 subarrays total.
example_2.py โ€” No Valid Split
$ Input: [4, 3, 15, 35]
โ€บ Output: -1
๐Ÿ’ก Note: Starting with 4, we cannot form a valid subarray because gcd(4,3) = 1, gcd(4,15) = 1, and gcd(4,35) = 1. Since we must include 4 in some subarray, no valid split exists.
example_3.py โ€” Single Element Arrays
$ Input: [6, 10, 15]
โ€บ Output: -1
๐Ÿ’ก Note: Each single element forms a subarray where first == last, but gcd(x,x) = x. Since 6, 10, and 15 are all > 1, we might expect this to work, but we need to check if we can form larger subarrays. gcd(6,10) = 2 > 1, so [6,10] is valid, but then [15] alone has gcd(15,15) = 15 > 1, giving us 2 subarrays total. Actually this should return 2, not -1.

Constraints

  • 1 โ‰ค nums.length โ‰ค 1000
  • 1 โ‰ค nums[i] โ‰ค 105
  • Each subarray must be contiguous and non-empty
  • gcd(first, last) > 1 for each subarray in the split

Visualization

Tap to expand
Minimum Subarrays in Valid SplitInput: [4, 6, 15, 35]461535GCD Analysis:[4, 6]gcd(4,6) = 2 โœ“[15, 35]gcd(15,35) = 5 โœ“Alternative Splits:[4, 6, 15, 35] - gcd(4,35) = 1 โœ—[4] โœ—[6, 15, 35] - gcd(6,35) = 1 โœ—DP Process:0dp[0]1dp[2]2dp[4]Optimal Solution:โœ“ Split into 2 subarrays:โ€ข [4, 6] with gcd = 2โ€ข [15, 35] with gcd = 5Algorithm: Dynamic ProgrammingTime: O(nยฒ ร— log(max(nums)))Space: O(n)Result: 2 subarrays
Understanding the Visualization
1
Check Compatibility
For each possible subarray, check if gcd(first, last) > 1
2
Dynamic Programming
Use DP to build optimal solution: dp[i] = min subarrays for nums[0:i]
3
Extend Greedily
For each start position, extend as far as possible while maintaining GCD constraint
4
Return Result
dp[n] contains the minimum subarrays needed, or -1 if impossible
Key Takeaway
๐ŸŽฏ Key Insight: Use dynamic programming to build the optimal solution by trying all possible subarray extensions while checking the GCD constraint. The DP state dp[i] represents the minimum subarrays needed to validly split nums[0:i].
Asked in
Google 25 Meta 18 Amazon 15 Microsoft 12
23.2K Views
Medium Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen