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++
#include <iostream>
using namespace std;
int gcd( int a, int b)
{
if (a == 0)
return b;
return gcd(b%a, a);
}
int largestGCD1Subset( int A[], int n)
{
int currentGCD = A[0];
for ( int i=1; i<n; i++)
{
currentGCD = gcd(currentGCD, A[i]);
if (currentGCD == 1)
return n;
}
return 0;
}
int main()
{
int A[] = {2, 18, 6, 3};
int n = sizeof (A)/ sizeof (A[0]);
cout << largestGCD1Subset(A, n);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int gcd( int a, int b)
{
if (a == 0 )
return b;
return gcd(b % a, a);
}
static int largestGCD1Subset( int A[],
int n)
{
int currentGCD = A[ 0 ];
for ( int i= 1 ; i<n; i++)
{
currentGCD =
gcd(currentGCD, A[i]);
if (currentGCD == 1 )
return n;
}
return 0 ;
}
public static void main (String[] args)
{
int A[] = { 2 , 18 , 6 , 3 };
int n =A.length;
System.out.println(
largestGCD1Subset(A, n) );
}
}
|
Python3
def gcd( a, b):
if (a = = 0 ):
return b
return gcd(b % a, a)
def largestGCD1Subset(A, n):
currentGCD = A[ 0 ];
for i in range ( 1 , n):
currentGCD = gcd(currentGCD, A[i])
if (currentGCD = = 1 ):
return n
return 0
A = [ 2 , 18 , 6 , 3 ]
n = len (A)
print (largestGCD1Subset(A, n))
|
C#
using System;
public class GFG {
static int gcd( int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
static int largestGCD1Subset( int []A,
int n)
{
int currentGCD = A[0];
for ( int i = 1; i < n; i++)
{
currentGCD =
gcd(currentGCD, A[i]);
if (currentGCD == 1)
return n;
}
return 0;
}
public static void Main()
{
int []A = {2, 18, 6, 3};
int n = A.Length;
Console.Write(
largestGCD1Subset(A, n));
}
}
|
PHP
<?php
function gcd( $a , $b )
{
if ( $a == 0)
return $b ;
return gcd( $b % $a , $a );
}
function largestGCD1Subset( $A , $n )
{
$currentGCD = $A [0];
for ( $i = 1; $i < $n ; $i ++)
{
$currentGCD =
gcd( $currentGCD , $A [ $i ]);
if ( $currentGCD == 1)
return $n ;
}
return 0;
}
$A = array (2, 18, 6, 3);
$n = sizeof( $A );
echo largestGCD1Subset( $A , $n );
?>
|
Javascript
<script>
function gcd(a, b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
function largestGCD1Subset(A, n)
{
let currentGCD = A[0];
for ( let i = 1; i < n; i++)
{
currentGCD =
gcd(currentGCD, A[i]);
if (currentGCD == 1)
return n;
}
return 0;
}
let A = [2, 18, 6, 3];
let n = A.length;
document.write(largestGCD1Subset(A, n));
</script>
|
Time Complexity : O(n* log(n))
Space Complexity : O(1)
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.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
16 Feb, 2023
Like Article
Save Article