Skip to content
Related Articles
Get the best out of our app
GFG App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Largest Subset with GCD 1

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

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^5

Examples: 

Input : A = {2, 3, 5}
Output : 3

Input : A = {3, 18, 12}
Output : -1

 

 

Naive Solution :

We find GCD of all possible subsets and find the largest subset whose GCD is 1. Total time taken will be equal to the time taken to evaluate GCD of all possible subsets. Total possible subsets are 2n. In worst case there are n elements in subset and time taken to calculate its GCD will be n * log(n) 
Extra space required to hold current subset is O(n)
 

Time complexity : O(n * log(n) * 2^n)
Space Complexity : O(n)

 

Optimized O(n) solution :

Let say we find a subset with GCD 1, if we add a new element to it then GCD still remains 1. Hence if a subset exists with GCD 1 then GCD of the complete set is also 1. Hence we first find GCD of the complete set, if its 1 then complete set is that subset else no subset exist with GCD 1. 
 

C++




// C++ program to find size of the largest subset with GCD 1
#include <iostream>
using namespace std;
  
// Function to return gcd of a and b
int gcd(int a, int b)
{
    if (a == 0)
        return b;
    return gcd(b%a, a);
}
  
// Function to find largest subset with GCD 1
int largestGCD1Subset(int A[], int n)
{
    // finding gcd of whole array
    int currentGCD = A[0];
    for (int i=1; i<n; i++)
    {
        currentGCD = gcd(currentGCD, A[i]);
  
        // If current GCD becomes 1 at any moment,
        // then whole array has GCD 1.
        if (currentGCD == 1)
            return n;
    }
  
    return 0;
}
  
// Driver program to test above function
int main()
{
    int A[] = {2, 18, 6, 3};
    int n = sizeof(A)/sizeof(A[0]);
    cout << largestGCD1Subset(A, n);
    return 0;
}


Java




// Java program to find size of the
// largest subset with GCD 1
import java.io.*;
  
class GFG {
      
    // Function to return gcd of
    // a and b
    static int gcd(int a, int b)
    {
        if (a == 0)
            return b;
        return gcd(b % a, a);
    }
  
    // Function to find largest
    // subset with GCD 1
    static int largestGCD1Subset(int A[],
                                   int n)
    {
          
        // finding gcd of whole array
        int currentGCD = A[0];
          
        for (int i=1; i<n; i++)
        {
            currentGCD = 
                    gcd(currentGCD, A[i]);
      
            // If current GCD becomes 1
            // at any moment, then whole
            // array has GCD 1.
            if (currentGCD == 1)
                return n;
        }
      
        return 0;
    }
      
    // Driver code
    public static void main (String[] args)
    {
        int A[] = {2, 18, 6, 3};
        int n =A.length;
          
        System.out.println(
                  largestGCD1Subset(A, n) );
    }
}
  
// This code is contributed by Sam007.


Python3




# python program to find size of the
# largest subset with GCD 1
  
# Function to return gcd of a and b
def gcd( a, b):
      
    if (a == 0):
        return b
          
    return gcd(b%a, a)
  
  
# Function to find largest subset
# with GCD 1
def largestGCD1Subset(A, n):
      
    # finding gcd of whole array
    currentGCD = A[0];
    for i in range(1, n):
          
        currentGCD = gcd(currentGCD, A[i])
  
        # If current GCD becomes 1 at
        # any moment, then whole 
        # array has GCD 1.
        if (currentGCD == 1):
            return n
    return 0
  
# Driver code
A = [2, 18, 6, 3]
n = len(A)
print (largestGCD1Subset(A, n))
  
# This code is Contributed by Sam007.


C#




// C# program to find size of the
// largest subset with GCD 1
using System;
          
public class GFG {
      
    // Function to return gcd of
    // a and b
    static int gcd(int a, int b)
    {
        if (a == 0)
            return b;
        return gcd(b % a, a);
    }
  
    // Function to find largest subset
    // with GCD 1
    static int largestGCD1Subset(int []A,
                                   int n)
    {
          
        // finding gcd of whole array
        int currentGCD = A[0];
        for (int i = 1; i < n; i++)
        {
            currentGCD = 
                    gcd(currentGCD, A[i]);
      
            // If current GCD becomes 1 at
            // any moment, then whole
            // array has GCD 1.
            if (currentGCD == 1)
                return n;
        }
      
        return 0;
    }
      
    // Driver method 
    public static void Main()
    {
        int []A = {2, 18, 6, 3};
        int n = A.Length;
          
        Console.Write(
              largestGCD1Subset(A, n));
    }
}
  
// This code is contributed by Sam007.


PHP




<?php
  
// php program to find size of the
// largest subset with GCD 1
  
// Function to return gcd of a and b
function gcd($a, $b)
{
    if ($a == 0)
        return $b;
    return gcd($b % $a, $a);
}
  
// Function to find largest subset
// with GCD 1
function largestGCD1Subset($A, $n)
{
      
    // finding gcd of whole array
    $currentGCD = $A[0];
    for ( $i = 1; $i < $n; $i++)
    {
        $currentGCD
           gcd($currentGCD, $A[$i]);
  
        // If current GCD becomes 1 
        // at any moment, then 
        // whole array has GCD 1.
        if ($currentGCD == 1)
            return $n;
    }
  
    return 0;
}
  
// Driver program
$A = array(2, 18, 6, 3);
$n = sizeof($A);
echo largestGCD1Subset($A, $n);
      
// This code is contributed by ajit 
?>


Javascript




<script>
  
// Javascript program to find size of the
// largest subset with GCD 1
  
// Function to return gcd of a and b
function gcd(a, b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}
  
// Function to find largest subset
// with GCD 1
function largestGCD1Subset(A, n)
{
      
    // finding gcd of whole array
    let currentGCD = A[0];
    for ( let i = 1; i < n; i++)
    {
        currentGCD = 
           gcd(currentGCD, A[i]);
  
        // If current GCD becomes 1 
        // at any moment, then 
        // whole array has GCD 1.
        if (currentGCD == 1)
            return n;
    }
  
    return 0;
}
  
// Driver program
let A = [2, 18, 6, 3];
let n = A.length;
document.write(largestGCD1Subset(A, n));
      
// This code is contributed by _saurabh_jaiswal
  
</script>


Output

4

Time Complexity : O(n* log(n))
Space Complexity : O(1)

This article is contributed by Pratik Chhajer. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
 


My Personal Notes arrow_drop_up
Last Updated : 16 Feb, 2023
Like Article
Save Article
Similar Reads
Related Tutorials