Given an array arr[] of size N, the task is to split the entire array into a minimum number of subarrays such that for each subarray, the GCD of the first and last element of the subarray is greater than 1.
Examples:
Input: arr[] = {2, 3, 4, 4, 4, 3}
Output: 2
Explanation:
Split the given array into [2, 3, 4, 4, 4] and [3].
The first subarray has gcd(2, 4) = 2 which is more than 1 and
The second subarray has gcd(3, 3) = 3 which is also more than 1.Input: arr[] = {1, 2, 3}
Output: 0
Explanation:
There is no possible splitting of the given array into subarrays in which the GCD of first and last element of the subarray is more than 1.
Naive Approach: The simplest approach to solve the problem is to perform all possible splits in the given array and for each split, check if all the subarrays have GCD of its first and last element greater than 1 or not. For all subarrays for which it is found to be true, store the count of subarrays. Finally, print the minimum count obtained among those splits.
Time Complexity: O(2N)
Auxiliary Space: O(1)
Efficient Approach: The approach is based on the idea that the first and the last element of the original array will always be used. The first element will be used in the first subarray split, the last element will be used in the last subarray split. To minimize the count of valid subarrays, follow the steps below:
- Fix a right pointer to the last element of the original array arr[], and find the leftmost element in the original array such that GCD(left, right) > 1. If such an element is not found, there is no valid answer.
- If such an element is found, that means we have found a valid subarray. Then change the right pointer to (left – 1), and again start searching for a valid subarray.
- Repeat the above step until right is more than 0 and keep on increasing the count of subarrays found till now.
- Print the value of count after all the above steps.
Below is the implementation of the above approach:
C++
// C++ program for the above approach#include <bits/stdc++.h>using namespace std;// Function to find the// minimum number of subarraysint minSubarrays(int arr[], int n){ // Right pointer int right = n - 1; // Left pointer int left = 0; // Count of subarrays int subarrays = 0; while (right >= 0) { for (left = 0; left <= right; left += 1) { // Find GCD(left, right) if (__gcd(arr[left], arr[right]) > 1) { // Found a valid large // subarray between // arr[left, right] subarrays += 1; right = left - 1; break; } // Searched the arr[0..right] // and found no subarray // having size >1 and having // gcd(left, right) > 1 if (left == right && __gcd(arr[left], arr[right]) == 1) { return 0; } } } return subarrays;}// Driver Codeint main(){ int N = 6; int arr[] = { 2, 3, 4, 4, 4, 3 }; // Function call cout << minSubarrays(arr, N); return 0;} |
Java
// Java program for the above approachimport java.util.*;class GFG{// Function to find the// minimum number of subarraysstatic int minSubarrays(int arr[], int n){ // Right pointer int right = n - 1; // Left pointer int left = 0; // Count of subarrays int subarrays = 0; while (right >= 0) { for (left = 0; left <= right; left += 1) { // Find GCD(left, right) if (__gcd(arr[left], arr[right]) > 1) { // Found a valid large // subarray between // arr[left, right] subarrays += 1; right = left - 1; break; } // Searched the arr[0..right] // and found no subarray // having size >1 and having // gcd(left, right) > 1 if (left == right && __gcd(arr[left], arr[right]) == 1) { return 0; } } } return subarrays;}static int __gcd(int a, int b) { return b == 0 ? a : __gcd(b, a % b); }// Driver Codepublic static void main(String[] args){ int N = 6; int arr[] = {2, 3, 4, 4, 4, 3}; // Function call System.out.print(minSubarrays(arr, N));}}// This code is contributed by Rajput-Ji |
Python3
# Python3 program for the above approachfrom math import gcd# Function to find the# minimum number of subarraysdef minSubarrays(arr, n): # Right pointer right = n - 1 # Left pointer left = 0 # Count of subarrays subarrays = 0 while (right >= 0): for left in range(right + 1): # Find GCD(left, right) if (gcd(arr[left], arr[right]) > 1): # Found a valid large # subarray between # arr[left, right] subarrays += 1 right = left - 1 break # Searched the arr[0..right] # and found no subarray # having size >1 and having # gcd(left, right) > 1 if (left == right and __gcd(arr[left], arr[right]) == 1): return 0 return subarrays# Driver Codeif __name__ == '__main__': N = 6 arr = [ 2, 3, 4, 4, 4, 3 ] # Function call print(minSubarrays(arr, N)) # This code is contributed by mohit kumar 29 |
C#
// C# program for the above approachusing System;class GFG{// Function to find the// minimum number of subarraysstatic int minSubarrays(int[] arr, int n){ // Right pointer int right = n - 1; // Left pointer int left = 0; // Count of subarrays int subarrays = 0; while (right >= 0) { for (left = 0; left <= right; left += 1) { // Find GCD(left, right) if (__gcd(arr[left], arr[right]) > 1) { // Found a valid large // subarray between // arr[left, right] subarrays += 1; right = left - 1; break; } // Searched the arr[0..right] // and found no subarray // having size >1 and having // gcd(left, right) > 1 if (left == right && __gcd(arr[left], arr[right]) == 1) { return 0; } } } return subarrays;} static int __gcd(int a, int b) { return b == 0 ? a : __gcd(b, a % b); } // Driver Codepublic static void Main(){ int N = 6; int[] arr = {2, 3, 4, 4, 4, 3}; // Function call Console.Write(minSubarrays(arr, N));}}// This code is contributed by Chitranayal |
2
Time Complexity: O(N)
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Check if an array can be split into subarrays with GCD exceeding K
- Pair of integers having least GCD among all given pairs having GCD exceeding K
- Split an Array to maximize subarrays having equal count of odd and even elements for a cost not exceeding K
- Construct a Matrix with no element exceeding X and sum of two adjacent elements not exceeding Y
- Count ways to split array into two subarrays with equal GCD
- Maximize count of pairs (i, j) from two arrays having element from first array not exceeding that from second array
- Count of ways to split an Array into three contiguous Subarrays having increasing Sum
- Split N natural numbers into two sets having GCD of their sums greater than 1
- Count ways to split array into two equal sum subarrays by replacing each array element to 0 once
- Split a given array into K subarrays minimizing the difference between their maximum and minimum
- Split array into maximum subarrays such that every distinct element lies in a single subarray
- Split array into two subarrays such that difference of their maximum is minimum
- Split array into two subarrays such that difference of their sum is minimum
- Split array into subarrays at minimum cost by minimizing count of repeating elements in each subarray
- Minimum index to split array into subarrays with co-prime products
- Split array into minimum number of subsets having maximum pair sum at most K
- Smallest subsequence having GCD equal to GCD of given array
- Split array into two subsequences having minimum count of pairs with sum equal to X
- Rearrange array to make it non-decreasing by swapping pairs having GCD equal to minimum array element
- Split array to three subarrays such that sum of first and third subarray is equal and maximum
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

