Minimum Split Into Subarrays With GCD Greater Than One

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
from math import gcd

class Solution:
    def minimumSplits(self, nums: List[int]) -> int:
        splits = 0  # Initialize the number of splits
        current_gcd = 1  # Initialize the current GCD

        for num in nums:
            current_gcd = gcd(current_gcd, num) # Calculate GCD with current number
            if current_gcd == 1:
                splits += 1 # Increment splits if GCD becomes 1
                current_gcd = num # Reset the current GCD

        return splits