Skip to main content

Minimum Split Into Subarrays With GCD Greater Than One - Video Solutions

MediumArrayMathDynamic ProgrammingGreedyNumber Theory

leetcode 2436. Minimum Split Into Subarrays With GCD Greater Than One - gcd and check

Code-Yao
11:33195 views
3 video solutions available

Minimum Split Into Subarrays With GCD Greater Than One - Video Solution

Watch 3 video solutions for Minimum Split Into Subarrays With GCD Greater Than One, a medium level problem involving Array, Math, Dynamic Programming. This walkthrough by Code-Yao has 195 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.

Problem Statement

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, and
  • 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.

 

Example 1:

Input: nums = [12,6,3,14,8]
Output: 2
Explanation: We can split the array into the subarrays: [12,6,3] and [14,8].
- The GCD of 12, 6 and 3 is 3, which is strictly greater than 1.
- The GCD of 14 and 8 is 2, which is strictly greater than 1.
It can be shown that splitting the array into one subarray will make the GCD = 1.

Example 2:

Input: nums = [4,12,6,14]
Output: 1
Explanation: We can split the array into only one subarray, which is the whole array.

 

Constraints:

  • 1 <= nums.length <= 2000
  • 2 <= nums[i] <= 109
Read full problem with examples

Approach Overview

Problem Overview: Given an integer array, split it into the minimum number of contiguous subarrays such that the gcd of every subarray is strictly greater than 1. If a segment’s GCD becomes 1, that segment is invalid and must be split earlier.

Approach 1: Dynamic Programming with GCD Tracking (O(n^2 log A) time, O(n) space)

Define dp[i] as the minimum splits needed for the prefix ending at index i. For each position, iterate backward and continuously compute the GCD of the current segment using gcd(g, nums[j]). As long as the GCD remains greater than 1, update dp[i] from dp[j-1]. Once the GCD becomes 1, extending the segment further is useless. This approach demonstrates the core idea of segment GCD validity but becomes expensive because each index may scan many previous elements.

Approach 2: Greedy + Mathematics (O(n log A) time, O(1) space)

Traverse the array while maintaining the GCD of the current subarray. Start with the first number and keep updating current_gcd = gcd(current_gcd, nums[i]). If the GCD stays greater than 1, the current segment remains valid and you continue extending it. When the GCD becomes 1, the current segment can no longer satisfy the requirement. Split right before this element, increment the segment count, and restart the segment with nums[i] as the new base. Each element participates in a constant number of GCD operations, making the solution linear with a logarithmic factor from the GCD calculation.

This greedy logic works because once the running GCD drops to 1, adding more elements can never increase it back above 1. The only valid move is to start a new subarray immediately.

Key concepts come from array traversal, number theory (GCD properties), and optimization ideas often seen in dynamic programming problems.

Recommended for interviews: Greedy + Mathematics. Interviewers expect you to recognize the monotonic property of GCD: once it reaches 1, it cannot recover. The DP formulation shows understanding of the state definition, but the greedy observation demonstrates stronger algorithmic intuition and reduces complexity to O(n log A).

Complexity Analysis

ApproachTimeSpaceWhen to Use
Dynamic Programming with GCD TrackingO(n^2 log A)O(n)Useful for understanding the state transition and exploring all valid segment boundaries
Greedy + Mathematics (Running GCD)O(n log A)O(1)Best general solution; optimal for large arrays with simple linear traversal