Find all possible GCDs of every subsequences of given Array
Last Updated :
09 Aug, 2021
Given an array arr[] consisting of N positive integers, the task is to find all the possible distinct Greatest Common Divisors(GCDs) among all the non-empty subsequences of the array arr[].
Examples:
Input: arr[] = {3, 4, 8}
Output: 1 3 4 8
Explanation:
The non-empty subsequences possible are {3}, {4}, {8}, {3, 4}, {4, 8}, {3, 8}, {3, 4, 8} and their corresponding GCDs are 3, 4, 8, 1, 4, 1, 1.
Therefore, print all the GCDs as {1, 3, 4, 8}.
Input: arr[] = {3, 8, 9, 4, 13, 45, 6}
Output: 1 2 3 4 6 8 9 13 45
Naive Approach: The simplest approach to solve the given problem is to generate all possible subsequences of the given array and store all the GCDs of the subsequence in a set. After checking for all the subsequences, print the element stores in the set as all possible GCDs that can be formed.
Time Complexity: O(log M*2N), where M is the maximum element of the array.
Auxiliary Space: O(1)
Efficient Approach: The above approach can also be optimized using Greedy Approach by observing the fact that the GCD of any subsequence lies over the range [1, M] where M is the maximum element of the array. Therefore, the idea is to iterate over then range [1, M] and if any element in the range is a factor of an array element then print the current element as one of the resultant GCD. Follow the steps below to solve the problem:
- Store all the array elements in the HashSet, say s.
- Iterate over the range [1, M] using the variable i and perform the following steps:
- Iterate through all the multiples of i, if there exists any multiple which is present in the HashSet, then print the current element i as one of the possible GCD.
Below is an implementation of the above approach:
// C++ program for the above approach
C++
#include <bits/stdc++.h>
using namespace std;
void findGCDsSubsequence(vector< int > arr)
{
vector< int > ans;
set< int > s;
for ( int i : arr)
s.insert(i);
int M = *max_element(arr.begin(), arr.end());
for ( int i = 1; i <= M; i++)
{
int gcd = 0;
for ( int j = i; j < M + 1; j += i)
{
if (s.find(j) != s.end())
gcd = __gcd(gcd, j);
}
if (gcd == i)
ans.push_back(i);
}
for ( int i = 0; i < ans.size(); i++)
cout << ans[i] << " " ;
}
int main()
{
int N = 7;
vector< int > arr = { 3, 4, 8 };
findGCDsSubsequence(arr);
return 0;
}
|
Java
import java.util.*;
public class GFG {
static int gcd1( int a, int b)
{
return b == 0 ? a : gcd1(b, a % b);
}
static void findGCDsSubsequence(ArrayList<Integer> arr)
{
ArrayList<Integer> ans = new ArrayList<Integer>();
HashSet<Integer> s = new HashSet<Integer>();
for ( int i : arr)
s.add(i);
int M = Integer.MIN_VALUE;
for ( int i : arr)
{
if (i > M)
M = i;
}
for ( int i = 1 ; i <= M; i++)
{
int gcd = 0 ;
for ( int j = i; j < M + 1 ; j += i)
{
if (s.contains(j))
gcd = gcd1(gcd, j);
}
if (gcd == i)
ans.add(i);
}
for ( int i = 0 ; i < ans.size(); i++)
System.out.print(ans.get(i) + " " );
}
public static void main(String args[])
{
ArrayList<Integer> arr = new ArrayList<Integer>();
arr.add( 3 );
arr.add( 4 );
arr.add( 8 );
findGCDsSubsequence(arr);
}
}
|
Python3
import math
def findGCDsSubsequence(nums):
Ans = []
s = set (nums)
M = max (nums)
for i in range ( 1 , M + 1 ):
gcd = 0
for j in range (i, M + 1 , i):
if j in s:
gcd = math.gcd(gcd, j)
if gcd = = i:
Ans + = [i]
print ( * Ans)
N = 7
arr = [ 3 , 4 , 8 ]
findGCDsSubsequence(arr)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int gcd1( int a, int b)
{
return b == 0 ? a : gcd1(b, a % b);
}
static void findGCDsSubsequence(List< int > arr)
{
List< int > ans = new List< int >();
HashSet< int > s = new HashSet< int >();
foreach ( int i in arr)
s.Add(i);
int M = Int32.MinValue;
foreach ( int i in arr)
{
if (i > M)
M = i;
}
for ( int i = 1; i <= M; i++)
{
int gcd = 0;
for ( int j = i; j < M + 1; j += i)
{
if (s.Contains(j))
gcd = gcd1(gcd, j);
}
if (gcd == i)
ans.Add(i);
}
for ( int i = 0; i < ans.Count; i++)
Console.Write(ans[i] + " " );
}
public static void Main()
{
List< int > arr = new List< int >(){ 3, 4, 8 };
findGCDsSubsequence(arr);
}
}
|
Javascript
<script>
function __gcd(a, b) {
if (b == 0)
return a;
return __gcd(b, a % b);
}
function findGCDsSubsequence(arr) {
let ans = [];
let s = new Set();
for (let i of arr)
s.add(i);
let M = [...arr].sort((a, b) => b - a)[0]
for (let i = 1; i <= M; i++) {
let gcd = 0;
for (let j = i; j < M + 1; j += i) {
if (s.has(j))
gcd = __gcd(gcd, j);
}
if (gcd == i)
ans.push(i);
}
for (let i = 0; i < ans.length; i++)
document.write(ans[i] + " " );
}
let N = 7;
let arr = [3, 4, 8];
findGCDsSubsequence(arr);
</script>
|
Time Complexity: O(M*log M), where M is the maximum element of the array.
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...