Largest subsequence having GCD greater than 1
Given an array, arr[], find the largest subsequence such that GCD of all those subsequences are greater than 1.
Examples:
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
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 common 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.
C++
#include<bits/stdc++.h>
using namespace std;
int largestGCDSubsequence( int arr[], int n)
{
int ans = 0;
int maxele = *max_element(arr, arr+n);
for ( int i=2; i<=maxele; ++i)
{
int count = 0;
for ( int j=0; j<n; ++j)
{
if (arr[j]%i == 0)
++count;
}
ans = max(ans, count);
}
return ans;
}
int main()
{
int arr[] = {3, 6, 2, 5, 4};
int size = sizeof (arr) / sizeof (arr[0]);
cout << largestGCDSubsequence(arr, size);
return 0;
}
|
Java
import java.util.Arrays;
class GFG {
static int largestGCDSubsequence( int arr[], int n)
{
int ans = 0 ;
int maxele = Arrays.stream(arr).max().getAsInt();;
for ( int i= 2 ; i<=maxele; ++i)
{
int count = 0 ;
for ( int j= 0 ; j<n; ++j)
{
if (arr[j]%i == 0 )
++count;
}
ans = Math.max(ans, count);
}
return ans;
}
public static void main(String[] args) {
int arr[] = { 3 , 6 , 2 , 5 , 4 };
int size = arr.length;
System.out.println(largestGCDSubsequence(arr, size));
}
}
|
Python3
def largestGCDSubsequence(arr, n):
ans = 0
maxele = max (arr)
for i in range ( 2 , maxele + 1 ):
count = 0
for j in range (n):
if (arr[j] % i = = 0 ):
count + = 1
ans = max (ans, count)
return ans
if __name__ = = '__main__' :
arr = [ 3 , 6 , 2 , 5 , 4 ]
size = len (arr)
print (largestGCDSubsequence(arr, size))
|
C#
using System;
using System.Linq;
public class GFG {
static int largestGCDSubsequence( int []arr, int n)
{
int ans = 0;
int maxele = arr.Max();
for ( int i=2; i<=maxele; ++i)
{
int count = 0;
for ( int j=0; j<n; ++j)
{
if (arr[j]%i == 0)
++count;
}
ans = Math.Max(ans, count);
}
return ans;
}
public static void Main() {
int []arr = {3, 6, 2, 5, 4};
int size = arr.Length;
Console.Write(largestGCDSubsequence(arr, size));
}
}
|
PHP
<?php
function largestGCDSubsequence( $arr , $n )
{
$ans = 0;
$maxele = max( $arr );
for ( $i = 2; $i <= $maxele ; ++ $i )
{
$count = 0;
for ( $j = 0; $j < $n ; ++ $j )
{
if ( $arr [ $j ] % $i == 0)
++ $count ;
}
$ans = max( $ans , $count );
}
return $ans ;
}
$arr = array (3, 6, 2, 5, 4);
$size = count ( $arr );
echo largestGCDSubsequence( $arr , $size );
?>
|
Javascript
<script>
function largestGCDSubsequence(arr , n)
{
var ans = 0;
var maxele =Math.max(...arr);
for ( var i = 2; i <= maxele; ++i)
{
var count = 0;
for (j = 0; j < n; ++j)
{
if (arr[j] % i == 0)
++count;
}
ans = Math.max(ans, count);
}
return ans;
}
var arr = [ 3, 6, 2, 5, 4 ];
var size = arr.length;
document.write(largestGCDSubsequence(arr, size));
</script>
|
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.
C++
#include<bits/stdc++.h>
using namespace std;
#define MAX 100001
int prime[MAX], countdiv[MAX];
void SieveOfEratosthenes()
{
for ( int i = 2; i * i <= MAX; ++i)
{
if (!prime[i])
for ( int j = i * 2; j <= MAX; j += i)
prime[j] = i;
}
for ( int i = 1; i < MAX; ++i)
if (!prime[i])
prime[i] = i;
}
int largestGCDSubsequence( int arr[], int n)
{
int ans = 0;
for ( int i=0; i < n; ++i)
{
int element = arr[i];
while (element > 1)
{
int div = prime[element];
++countdiv[ div ];
ans = max(ans, countdiv[ div ]);
while (element % div ==0)
element /= div ;
}
}
return ans;
}
int main()
{
SieveOfEratosthenes();
int arr[] = {10, 15, 7, 25, 9, 35};
int size = sizeof (arr) / sizeof (arr[0]);
cout << largestGCDSubsequence(arr, size);
return 0;
}
|
Java
class GFG
{
static int MAX = 100001 ;
static int [] prime = new int [MAX + 1 ];
static int [] countdiv = new int [MAX + 1 ];
static void SieveOfEratosthenes()
{
for ( int i = 2 ; i * i <= MAX; ++i)
{
if (prime[i] == 0 )
for ( int j = i * 2 ; j <= MAX; j += i)
prime[j] = i;
}
for ( int i = 1 ; i < MAX; ++i)
if (prime[i] == 0 )
prime[i] = i;
}
static int largestGCDSubsequence( int arr[], int n)
{
int ans = 0 ;
for ( int i = 0 ; i < n; ++i)
{
int element = arr[i];
while (element > 1 )
{
int div = prime[element];
++countdiv[div];
ans = Math.max(ans, countdiv[div]);
while (element % div == 0 )
element /= div;
}
}
return ans;
}
public static void main (String[] args)
{
SieveOfEratosthenes();
int arr[] = { 10 , 15 , 7 , 25 , 9 , 35 };
int size = arr.length;
System.out.println(largestGCDSubsequence(arr, size));
}
}
|
Python3
import math as mt
MAX = 100001
prime = [ 0 for i in range ( MAX + 1 )]
countdiv = [ 0 for i in range ( MAX + 1 )]
def SieveOfEratosthenes():
for i in range ( 2 , mt.ceil(mt.sqrt( MAX + 1 ))):
if (prime[i] = = 0 ):
for j in range (i * 2 , MAX + 1 , i):
prime[j] = i
for i in range ( 1 , MAX ):
if (prime[i] = = 0 ):
prime[i] = i
def largestGCDSubsequence(arr, n):
ans = 0
for i in range (n):
element = arr[i]
while (element > 1 ):
div = prime[element]
countdiv[div] + = 1
ans = max (ans, countdiv[div])
while (element % div = = 0 ):
element = element / / div
return ans
SieveOfEratosthenes()
arr = [ 10 , 15 , 7 , 25 , 9 , 35 ]
size = len (arr)
print (largestGCDSubsequence(arr, size))
|
C#
using System;
class GFG
{
static int MAX=100001;
static int [] prime = new int [MAX + 1];
static int [] countdiv = new int [MAX + 1];
static void SieveOfEratosthenes()
{
for ( int i = 2; i * i <= MAX; ++i)
{
if (prime[i] == 0)
for ( int j = i * 2; j <= MAX; j += i)
prime[j] = i;
}
for ( int i = 1; i < MAX; ++i)
if (prime[i] == 0)
prime[i] = i;
}
static int largestGCDSubsequence( int []arr, int n)
{
int ans = 0;
for ( int i = 0; i < n; ++i)
{
int element = arr[i];
while (element > 1)
{
int div = prime[element];
++countdiv[div];
ans = Math.Max(ans, countdiv[div]);
while (element % div==0)
element /= div;
}
}
return ans;
}
public static void Main()
{
SieveOfEratosthenes();
int []arr = {10, 15, 7, 25, 9, 35};
int size = arr.Length;
Console.WriteLine(largestGCDSubsequence(arr, size));
}
}
|
PHP
<?php
$MAX = 10001;
$prime = array_fill (0, $MAX , 0);
$countdiv = array_fill (0, $MAX , 0);
function SieveOfEratosthenes()
{
global $MAX , $prime ;
for ( $i = 2; $i * $i <= $MAX ; ++ $i )
{
if ( $prime [ $i ] == 0)
for ( $j = $i * 2; $j <= $MAX ; $j += $i )
$prime [ $j ] = $i ;
}
for ( $i = 1; $i < $MAX ; ++ $i )
if ( $prime [ $i ] == 0)
$prime [ $i ] = $i ;
}
function largestGCDSubsequence( $arr , $n )
{
global $countdiv , $prime ;
$ans = 0;
for ( $i = 0; $i < $n ; ++ $i )
{
$element = $arr [ $i ];
while ( $element > 1)
{
$div = $prime [ $element ];
++ $countdiv [ $div ];
$ans = max( $ans , $countdiv [ $div ]);
while ( $element % $div == 0)
$element = (int)( $element / $div );
}
}
return $ans ;
}
SieveOfEratosthenes();
$arr = array (10, 15, 7, 25, 9, 35);
$size = count ( $arr );
echo largestGCDSubsequence( $arr , $size );
?>
|
Javascript
<script>
let MAX = 100001;
let prime = new Array(MAX + 1);
let countdiv = new Array(MAX + 1);
for (let i=0;i<MAX+1;i++)
{
prime[i]=0;
countdiv[i]=0;
}
function SieveOfEratosthenes()
{
for (let i = 2; i * i <= MAX; ++i)
{
if (prime[i] == 0)
for (let j = i * 2; j <= MAX; j += i)
prime[j] = i;
}
for (let i = 1; i < MAX; ++i)
if (prime[i] == 0)
prime[i] = i;
}
function largestGCDSubsequence(arr,n)
{
let ans = 0;
for (let i = 0; i < n; ++i)
{
let element = arr[i];
while (element > 1)
{
let div = prime[element];
++countdiv[div];
ans = Math.max(ans, countdiv[div]);
while (element % div == 0)
element /= div;
}
}
return ans;
}
SieveOfEratosthenes();
let arr=[10, 15, 7, 25, 9, 35];
let size = arr.length;
document.write(largestGCDSubsequence(arr, size));
</script>
|
Output:
4
Time complexity: O( n*log(max(arr[i])) ) + MAX*log(log(MAX))
Auxiliary space: O(MAX)
Last Updated :
13 Sep, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...