Split array into minimum number of subarrays having GCD of its first and last element exceeding 1
Last Updated :
10 Aug, 2021
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 subarrays
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;
}
// Driver Code
int 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 approach
import java.util.*;
class GFG{
// Function to find the
// minimum number of subarrays
static 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 Code
public 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 approach
from math import gcd
# Function to find the
# minimum number of subarrays
def 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 Code
if __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 approach
using System;
class GFG{
// Function to find the
// minimum number of subarrays
static 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 Code
public 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
JavaScript
<script>
// javascript program for the above approach
// Function to find the
// minimum number of subarrays
function minSubarrays(arr , n) {
// Right pointer
var right = n - 1;
// Left pointer
var left = 0;
// Count of subarrays
var 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;
}
function __gcd(a , b) {
return b == 0 ? a : __gcd(b, a % b);
}
// Driver Code
var N = 6;
var arr = [ 2, 3, 4, 4, 4, 3 ];
// Function call
document.write(minSubarrays(arr, N));
// This code contributed by umadevi9616
</script>
Time Complexity: O(N^2 )
Auxiliary Space: O(1)
Similar Reads
GCD (Greatest Common Divisor) Practice Problems for Competitive Programming GCD (Greatest Common Divisor) or HCF (Highest Common Factor) of two numbers is the largest positive integer that divides both of the numbers.GCD of Two NumbersFastest Way to Compute GCDThe fastest way to find the Greatest Common Divisor (GCD) of two numbers is by using the Euclidean algorithm. The E
4 min read
Program to Find GCD or HCF of Two Numbers Given two numbers a and b, the task is to find the GCD of the two numbers.Note: The GCD (Greatest Common Divisor) or HCF (Highest Common Factor) of two numbers is the largest number that divides both of them. Examples:Input: a = 20, b = 28Output: 4Explanation: The factors of 20 are 1, 2, 4, 5, 10 an
15+ min read
Check if two numbers are co-prime or not Two numbers A and B are said to be Co-Prime or mutually prime if the Greatest Common Divisor of them is 1. You have been given two numbers A and B, find if they are Co-prime or not.Examples : Input : 2 3Output : Co-PrimeInput : 4 8Output : Not Co-PrimeThe idea is simple, we find GCD of two numbers a
5 min read
GCD of more than two (or array) numbers Given an array arr[] of non-negative numbers, the task is to find GCD of all the array elements. In a previous post we find GCD of two number.Examples:Input: arr[] = [1, 2, 3]Output: 1Input: arr[] = [2, 4, 6, 8]Output: 2Using Recursive GCDThe GCD of three or more numbers equals the product of the pr
11 min read
Program to find LCM of two numbers LCM of two numbers is the smallest number which can be divided by both numbers. Input : a = 12, b = 18Output : 3636 is the smallest number divisible by both 12 and 18Input : a = 5, b = 11Output : 5555 is the smallest number divisible by both 5 and 11[Naive Approach] Using Conditional Loop This appro
8 min read
LCM of given array elements In this article, we will learn how to find the LCM of given array elements.Given an array of n numbers, find the LCM of it. Example:Input : {1, 2, 8, 3}Output : 24LCM of 1, 2, 8 and 3 is 24Input : {2, 7, 3, 9, 4}Output : 252Table of Content[Naive Approach] Iterative LCM Calculation - O(n * log(min(a
14 min read
Find the other number when LCM and HCF given Given a number A and L.C.M and H.C.F. The task is to determine the other number B. Examples: Input: A = 10, Lcm = 10, Hcf = 50. Output: B = 50 Input: A = 5, Lcm = 25, Hcf = 4. Output: B = 20 Formula: A * B = LCM * HCF B = (LCM * HCF)/AExample : A = 15, B = 12 HCF = 3, LCM = 60 We can see that 3 * 60
4 min read
Minimum insertions to make a Co-prime array Given an array of N elements, find the minimum number of insertions to convert the given array into a co-prime array. Print the resultant array also.Co-prime Array : An array in which every pair of adjacent elements are co-primes. i.e, gcd(a, b) = 1 . Examples : Input : A[] = {2, 7, 28}Output : 1Exp
6 min read
Find the minimum possible health of the winning player Given an array health[] where health[i] is the health of the ith player in a game, any player can attack any other player in the game. The health of the player being attacked will be reduced by the amount of health the attacking player has. The task is to find the minimum possible health of the winn
4 min read
Minimum squares to evenly cut a rectangle Given a rectangular sheet of length l and width w. we need to divide this sheet into square sheets such that the number of square sheets should be as minimum as possible.Examples: Input :l= 4 w=6 Output :6 We can form squares with side of 1 unit, But the number of squares will be 24, this is not min
4 min read