# Largest subsequence having GCD greater than 1

Given an array arr[], find the largest subsequence such that GCD of all those subsequence are greater than 1. For exampple:-

```Input: 3, 6, 2, 5, 4
Output: 3
Explanation: There are only three elements(6,
2, 4) having GCD greater than 1 i.e., 2. So the
largest subsequence will be 3

Input: 10, 15, 7, 25, 9, 35
Output: 4
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Naive Approach(Method 1)

Simple approach is to generate all the subsequence one by one and then find the GCD of all such generated set. Problem of this approach is that it grows exponentially in 2N

Iterative Approach(Method 2)

If we observe then we will found that to make gcd greater than 1, all such elements must contain comman factor greater than 1 which evenly divides all these values. So in order to get that factor we will iterate from 2 to Maximum element of array and then check for divisibility.

```// Simple C++ program to find length of
// the largest subsequence with GCD greater
// than 1.
#include<bits/stdc++.h>
using namespace std;

// Returns length of the largest subsequence
// with GCD more than 1.
int largestGCDSubsequence(int arr[], int n)
{
int ans = 0;

// Finding the Maximum value in arr[]
int maxele = *max_element(arr, arr+n);

// Iterate from 2 to maximum possible
// divisor of all give values
for (int i=2; i<=maxele; ++i)
{
int count = 0;
for (int j=0; j<n; ++j)
{
// If we found divisor,
// increment count
if (arr[j]%i == 0)
++count;
}
ans = max(ans, count);
}

return ans;
}

// Driver code
int main()
{
int arr[] = {3, 6, 2, 5, 4};
int size = sizeof(arr) / sizeof(arr[0]);
cout << largestGCDSubsequence(arr, size);
return 0;
}
```
```Output: 3
```

Time Complexity: O(n * max(arr[i])) where n is size of array.
Auxiliary Space: O(1)

Best Approach(Method 3)

An efficient approach is to use prime factorization method with the help of Sieve of Eratosthenes. First of all we will find the smallest prime divisor of all elements by pre-computed sieve. After that we will mark all the prime divisor of every element of arr[] by factorizing it with the help of pre-computed prime[] array.
Now we have all the marked primes occurring in all the array elements. The last step is to find the maximum count of all such prime factors.

```// Efficient C++ program to find length of
// the largest subsequence with GCD greater
// than 1.
#include<bits/stdc++.h>
using namespace std;

#define MAX 100001

// prime[] for storing smallest prime divisor of element
// count[] for storing the number of times a particular
// divisor occurs in a subsequence
int prime[MAX], countdiv[MAX];

// Simple sieve to find smallest prime factors of numbers
// smaller than MAX
void SieveOfEratosthenes()
{
for (int i = 2; i * i < MAX; ++i)
{
if (!prime[i])
for (int j = i * i; j < MAX; j += i)
prime[j] = i;
}

// Prime number will have same divisor
for (int i = 1; i < MAX; ++i)
if (!prime[i])
prime[i] = i;
}

// Returns length of the largest subsequence
// with GCD more than 1.
int largestGCDSubsequence(int arr[], int n)
{
int ans = 0;
for (int i=0; i < n; ++i)
{
int element = arr[i];

// Fetch total unique prime divisor of element
while (element > 1)
{
int div = prime[element];

// Increment count[] of Every unique divisor
// we get till now
++countdiv[div];

// Find maximum frequency of divisor
ans = max(ans, countdiv[div]);

while (element % div==0)
element /= div;
}
}

return ans;
}

// Driver code
int main()
{
// Pre-compute smallest divisor of all numbers
SieveOfEratosthenes();

int arr[] = {10, 15, 7, 25, 9, 35};
int size = sizeof(arr) / sizeof(arr[0]);

cout << largestGCDSubsequence(arr, size);
return 0;
}
```
```Output: 4
```

Time complexity: O( n*log(max(arr[i])) ) + MAX*log(log(MAX))
Auxiliary space: O(MAX)

This article is contributed by Shubham Bansal. 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.

# GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.
2 Average Difficulty : 2/5.0
Based on 2 vote(s)