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^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 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;     for (int i=1; i

Java

 // Java program to find size of the // largest subset with GCD 1 import 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;                    for (int i=1; i

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;     for i in range(1, n):                    currentGCD = gcd(currentGCD, A[i])            # If current GCD becomes 1 at         # any momemnt, 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;         for (int i = 1; i < n; i++)         {             currentGCD =                      gcd(currentGCD, A[i]);                    // If current GCD becomes 1 at             // any momemnt, 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



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 contribute.geeksforgeeks.org or mail your article to contribute@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

Improved By : Sam007, jit_t