Open In App

Largest subarray with GCD one

Last Updated : 07 Jul, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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 
Recommended Practice

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>


Output

Length of the largest subarray = 4

Time Complexity: O(log(min(n))) 
Auxiliary Space: O(1)



Similar Reads

Maximum subarray sum possible after removing at most one subarray
Given an array arr[] consisting of N integers, the task is to find the maximum sum of any subarray possible after removing at most one subarray from the given array. Examples: Input: arr[] = {-1, 5, 2, -1, 6}Output: 13Explanation:The maximum sum can be obtained by selecting the subarray {5, 2, -1, 6} and removing the subarray {-1}. Input: arr[] = {
9 min read
Smallest subsequence having GCD equal to GCD of given array
Given an array arr[] of size N, the task is to find the smallest subsequence of the given array whose GCD of the subsequence is equal to the GCD of the given array. If more than one such subsequence exists, then print anyone of them. Examples: Input: arr[] = {4, 6, 12}Output: 4 6Explanation: Smallest subsequence having gcd equal to gcd of the given
7 min read
Pair of integers having least GCD among all given pairs having GCD exceeding K
Given an array arr[][] containing pairs of integers in increasing order of GCDs, and an integer K, the task is to find a pair of integers whose GCD is at least K and is also the least among all possible GCDs exceeding K. If there exists no such pair, then print -1.Examples: Input: arr[][] = [(3, 6), (15, 30), (25, 75), (30, 120)], K = 16 Output: (2
7 min read
Count all possible values of K less than Y such that GCD(X, Y) = GCD(X+K, Y)
Given two integers X and Y, the task is to find the number of integers, K, such that gcd(X, Y) is equal to gcd(X+K, Y), where 0 &lt; K &lt;Y. Examples: Input: X = 3, Y = 15Output: 4Explanation: All possible values of K are {0, 3, 6, 9} for which GCD(X, Y) = GCD(X + K, Y). Input: X = 2, Y = 12Output: 2Explanation: All possible values of K are {0, 8}
8 min read
Find N distinct integers with GCD of sequence as 1 and GCD of each pair greater than 1
Given an integer N, the task is to find a sequence of N distinct positive integers such that the Greatest Common Divisor of the sequence is 1 and GCD of all possible pairs of elements is greater than 1. Input: N = 4Output: 84 60 105 70Explanation: The GCD(84, 60, 105, 70) is 1 and the GCD of all possible pair of elements i.e, {(84, 60), (84, 105),
5 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(NlogN) using a segment-tree data structure. The s
10 min read
Length of the Smallest Subarray that must be removed in order to Maximise the GCD
Given an array arr[] of N elements, the task is to find the length of the smallest subarray such that when this subarray is removed from the array, the GCD of the resultant array is maximum. Note: The resulting array should be non-empty.Examples: Input: N = 4, arr[] = {3, 6, 1, 2} Output: 2 Explanation: If we remove the subarray {1, 2} then the res
7 min read
Length of longest subarray of length at least 2 with maximum GCD
Given an array arr[] of length N, the task is the find the length of longest subarray of length at least 2 with maximum possible GCD value.Examples: Input: arr[] = {1, 2, 2} Output: 2 Explanation: Possible sub-arrays of size greater than 2 and there GCD’s are: 1) {1, 2} -&gt; 1 2) {2, 2} -&gt; 2 3) {1, 2, 3} -&gt; 1 Here, the maximum GCD value is 2
7 min read
Python program to Find the Jumbo GCD subarray
Given an array, write a program to find the maximum GCD among all the subarray of the size &gt;= 2 of the given array. Examples: Input list: [2, 3, 4, 4, 4] Output: 4 Input list: [3, 7, 2, 9, 18, 5, 1, 13 ] Output: 9Approach:Import the math module for pythonIntroduce a variable(say, V1) to store the gcd of each element of the list while looping thr
2 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(&gt; 1) which is of maximum length. Input: arr[] = {410, 52, 51, 180, 22
16 min read
Article Tags :
Practice Tags :