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
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