Largest Subset with GCD 1 - GeeksforGeeks

22 Apr, 2022
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
 
 
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)
 
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++
Java
Python3
C#
PHP
Javascript
// C++ program to find size of the largest subset with GCD 1#include <iostream>using namespace std; // Function to return gcd of a and bint gcd(int a, int b){    if (a == 0)        return b;    return gcd(b%a, a);} // Function to find largest subset with GCD 1int 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 functionint main(){    int A[] = {2, 18, 6, 3};    int n = sizeof(A)/sizeof(A[0]);    cout << largestGCD1Subset(A, n);    return 0;}
// Java program to find size of the// largest subset with GCD 1import java.*; 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.
# python program to find size of the# largest subset with GCD 1 # Function to return gcd of a and bdef gcd( a, b):         if (a == 0):        return b             return gcd(b%a, a)  # Function to find largest subset# with GCD 1def 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 codeA = [2, 18, 6, 3]n = len(A)print (largestGCD1Subset(A, n)) # This code is Contributed by Sam007.
// C# program to find size of the// largest subset with GCD 1using 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 program to find size of the// largest subset with GCD 1 // Function to return gcd of a and bfunction gcd($a, $b){    if ($a == 0)        return $b;    return gcd($b % $a, $a);} // Function to find largest subset// with GCD 1function 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?>
<script> // Javascript program to find size of the// largest subset with GCD 1 // Function to return gcd of a and bfunction gcd(a, b){    if (a == 0)        return b;    return gcd(b % a, a);} // Function to find largest subset// with GCD 1function 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 programlet 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. 
Sam007
jit_t
_saurabh_jaiswal
surinderdawra388
GCD-LCM
Mathematical
School Programming
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Program to print prime numbers from 1 to N.
Modular multiplicative inverse
Fizz Buzz Implementation
Singular Value Decomposition (SVD)
Segment Tree | Set 1 (Sum of given range)
Python Dictionary
Arrays in C/C++
Inheritance in C++
Reverse a string in Java
C++ Classes and Objects