# 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 2^{n}. 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 momemnt, ` ` ` `// 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; ` `} ` |

*chevron_right*

*filter_none*

## 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[` `0` `]; ` ` ` ` ` `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 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. ` |

*chevron_right*

*filter_none*

## 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 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. ` |

*chevron_right*

*filter_none*

## 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 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. ` |

*chevron_right*

*filter_none*

## 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 momemnt, 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 ` `?> ` |

*chevron_right*

*filter_none*

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.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- Largest subset where absolute difference of any two element is a power of 2
- Find the length of the Largest subset such that all elements are Pairwise Coprime
- Find the largest and second largest value in a Linked List
- Probability such that two subset contains same number of elements
- Count number of subsets whose median is also present in the same subset
- Array with GCD of any of its subset belongs to the given array
- Largest number M less than N such that XOR of M and N is even
- Largest subarray with GCD one
- Largest Even and Odd N-digit numbers
- Product of N with its largest odd digit
- Find the largest possible k-multiple set
- Find largest factor of N such that N/F is less than K
- Largest Coprime Set Between two integers
- Series with largest GCD and sum equals to n
- Largest subsequence having GCD greater than 1
- Find the largest multiple of 2, 3 and 5
- Largest right circular cylinder within a frustum
- Largest right circular cylinder within a cube
- Largest number that divides x and is co-prime with y
- Largest palindromic number in an array