values. Cut it into contiguous non-empty pieces so that every element lands in exactly one piece and the first and last numbers of each piece share a divisor greater than 1. In a one-element piece, that element is both the first and the last.
What sits inside a piece does not matter; only its two ends are checked.
Return the smallest number of pieces. If no such split exists, return -1.values=[6, 5, 9, 10, 25] → 2
The piece [6, 5, 9] starts with 6 and ends with 9, which share the divisor 3. The piece [10, 25] has ends sharing 5. A single piece fails because 6 and 25 share no divisor above 1.
values=[4, 9, 2, 3] → 2
Take [4, 9, 2] (ends 4 and 2, GCD 2) and [3]. One piece is impossible since 4 and 3 share no divisor above 1.
values=[8, 1] → -1
The 1 cannot sit in any piece, as its GCD with anything is 1. The answer is -1.
values=[6, 5, 9, 10, 25]
2
Run checks the first 3 tests in your browser. Submit checks all 177 tests on the server, hidden ones included.