Largest subarray with GCD one
Last Updated :
07 Jul, 2022
There is an array with n elements. Find length of the largest subarray having GCD equal to 1. If no subarray with GCD 1, then print -1.
Examples :
Input : 1 3 5
Output : 3
Input : 2 4 6
Output :-1
A simple solution is to consider every subarray and find its GCD and keep track of largest subarray with GCD one. Finally return length of the largest subarray with GCD 1.
An efficient solution is based on fact that if any two elements have GCD equals to one, then whole array has GCD one. So the output is either -1 or length of array.
C++
// C++ program, to find length of the largest
// subarray with GCD equals to 1.
#include<bits/stdc++.h>
using namespace std;
int findLargest(int arr[], int n)
{
/*If gcd of any subarray is 1 then gcd of
any number with the sub array will be 1.
so if we are getting any subarray with
gcd 1, then maximum number of element of
the subarray will be equal to the number
of elements of the array. Else it will be -1.*/
int gcd = arr[0];
for (int i=1; i<n; i++)
gcd = __gcd(gcd, arr[i]);
return (gcd == 1)? n : -1;
}
// Driver code
int main()
{
int arr[] = {1, 3, 5, 7};
int n = sizeof(arr)/sizeof(int);
cout << "Length of the largest subarray = "
<< findLargest(arr, n);
return 0;
}
Java
// Java program, to find length of the
// largest subarray with GCD equals to 1.
class GFG {
static int ___gcd(int a, int b)
{
// Everything divides 0
if (a == 0 || b == 0)
return 0;
// base case
if (a == b)
return a;
// a is greater
if (a > b)
return ___gcd(a - b, b);
return ___gcd(a, b - a);
}
static int findLargest(int arr[],
int n)
{
/*If gcd of any subarray is 1
then gcd of any number with the
sub array will be 1. so if we
are getting any subarray with
gcd 1, then maximum number of
element of the subarray will
be equal to the number of
elements of the array. Else
it will be -1.*/
int gcd = arr[0];
for (int i = 1; i < n; i++)
gcd = ___gcd(gcd, arr[i]);
return (gcd == 1)? n : -1;
}
// Driver code
public static void main (String[] args)
{
int arr[] = {1, 3, 5, 7};
int n = arr.length;
System.out.print("Length of the "
+ "largest subarray = "
+ findLargest(arr, n));
}
}
// This code is contributed by Anant Agarwal.
Python3
# Python program, to find
# length of the largest
# subarray with GCD equals to 1.
def ___gcd(a,b):
# Everything divides 0
if (a == 0 or b == 0):
return 0
# base case
if (a == b):
return a
# a is greater
if (a > b):
return ___gcd(a-b, b)
return ___gcd(a, b-a)
def findLargest(arr, n):
'''If gcd of any subarray is 1 then gcd of
any number with the sub array will be 1.
so if we are getting any subarray with
gcd 1, then maximum number of element of
the subarray will be equal to the number
of elements of the array. Else it will be -1.'''
gcd = arr[0]
for i in range(1,n):
gcd = ___gcd(gcd, arr[i])
return n if (gcd == 1) else -1
# Driver code
arr=[1, 3, 5, 7]
n=len(arr)
print("Length of the largest subarray = ",
findLargest(arr, n))
# This code is contributed
# by Anant Agarwal.
C#
// C# program, to find length of the
// largest subarray with GCD equals to 1.
using System;
class GFG {
static int ___gcd(int a, int b)
{
// Everything divides 0
if (a == 0 || b == 0)
return 0;
// base case
if (a == b)
return a;
// a is greater
if (a > b)
return ___gcd(a - b, b);
return ___gcd(a, b - a);
}
static int findLargest(int []arr,
int n)
{
// If gcd of any subarray is 1
// then gcd of any number with the
// sub array will be 1. so if we
// are getting any subarray with
// gcd 1, then maximum number of
// element of the subarray will
// be equal to the number of
// elements of the array. Else
// it will be -1.
int gcd = arr[0];
for (int i = 1; i < n; i++)
gcd = ___gcd(gcd, arr[i]);
return (gcd == 1)? n : -1;
}
// Driver code
public static void Main ()
{
int []arr = {1, 3, 5, 7};
int n = arr.Length;
Console.Write("Length of the "
+ "largest subarray = "
+ findLargest(arr, n));
}
}
// This code is contributed by Nitin Mittal.
PHP
<?php
// PHP program, to find length
// of the largest subarray with
// GCD equals to 1.
function ___gcd($a, $b)
{
// Everything divides 0
if ($a == 0 || $b == 0)
return 0;
// base case
if ($a == $b)
return $a;
// a is greater
if ($a > $b)
return ___gcd($a - $b, $b);
return ___gcd($a, $b - $a);
}
function findLargest($arr, $n)
{
/*If gcd of any subarray is 1
then gcd of any number with the
sub array will be 1. so if we
are getting any subarray with
gcd 1, then maximum number of
element of the subarray will
be equal to the number of
elements of the array. Else
it will be -1.*/
$gcd = $arr[0];
for ($i = 1; $i < $n; $i++)
$gcd = ___gcd($gcd, $arr[$i]);
return ($gcd == 1)? $n : -1;
}
// Driver code
$arr = array(1, 3, 5, 7);
$n = count($arr);
echo "Length of the " .
"largest subarray = " .
findLargest($arr, $n);
// This code is contributed by Sam007
?>
JavaScript
<script>
// Javascript program, to find length of the
// largest subarray with GCD equals to 1.
function ___gcd(a, b)
{
// Everything divides 0
if (a == 0 || b == 0)
return 0;
// base case
if (a == b)
return a;
// a is greater
if (a > b)
return ___gcd(a - b, b);
return ___gcd(a, b - a);
}
function findLargest(arr, n)
{
/*If gcd of any subarray is 1
then gcd of any number with the
sub array will be 1. so if we
are getting any subarray with
gcd 1, then maximum number of
element of the subarray will
be equal to the number of
elements of the array. Else
it will be -1.*/
let gcd = arr[0];
for (let i = 1; i < n; i++)
gcd = ___gcd(gcd, arr[i]);
return (gcd == 1)? n : -1;
}
// Driver Code
let arr = [1, 3, 5, 7];
let n = arr.length;
document.write("Length of the "
+ "largest subarray = "
+ findLargest(arr, n));
</script>
OutputLength of the largest subarray = 4
Time Complexity: O(log(min(n)))
Auxiliary Space: O(1)
Similar Reads
Largest Subset with GCD 1 Given n integers, we need to find size of the largest subset with GCD equal to 1. Input Constraint : n <= 10^5, A[i] <= 10^5Examples: Input : A = {2, 3, 5}Output : 3Explanation: The largest subset with a GCD greater than 1 is {2, 3, 5}, and the GCD of all the elements in the subset is 3.Input
6 min read
Longest subarray with GCD greater than 1 Given an array arr[] consisting of N integers, the task is to find the maximum length of subarray having the Greatest Common Divisor (GCD) of all the elements greater than 1. Examples: Input: arr[] = {4, 3, 2, 2}Output: 2Explanation:Consider the subarray {2, 2} having GCD as 2(> 1) which is of ma
15+ min read
Smallest Subarray with given GCD Given an array arr[] of n numbers and an integer k, find length of the minimum sub-array with gcd equals to k.Example: Input: arr[] = {6, 9, 7, 10, 12, 24, 36, 27}, K = 3 Output: 2 Explanation: GCD of subarray {6,9} is 3. GCD of subarray {24,36,27} is also 3, but {6,9} is the smallest Note: Time com
15+ min read
Longest sub-sequence with maximum GCD Given an array arr[] of length N, the task is to find the length of the longest sub-sequence with the maximum possible GCD.Examples: Input: arr[] = {2, 1, 2} Output: 2 {2}, {2} and {2, 2} are the subsequences with the maximum possible GCD.Input: arr[] = {1, 2, 3} Output: 1 {3} is the required subseq
4 min read
Smallest subarray with GCD as 1 | Segment Tree Given an array arr[], the task is to find the smallest sub-arrays with GCD equal to 1. If there is no such sub-array then print -1. Examples: Input: arr[] = {2, 6, 3} Output: 3 {2, 6, 3} is the only sub-array with GCD = 1. Input: arr[] = {2, 2, 2} Output: -1 Approach: This problem can be solved in O
10 min read