Array with GCD of any of its subset belongs to the given array
Last Updated :
17 Aug, 2022
Given a set of N elements such that N, task is to generate an array such that the GCD of any subset of the generated array lies in the given set of elements. The generated array should not be more than thrice the length of the set of the GCD.
Prerequisite : GCD of an Array | Subset of Array
Examples :
Input : 3
1 2 7
Output : 1 1 2 1 7
Input : 4
2 4 6 12
Output : 2 2 4 2 6 2 12
Input : 5
2 5 6 7 11
Output : No array can be build
Explanation: Calculate the GCD of an array or in this case a set. Now, first sort the given set of GCD. If the GCD of this set is equal to the minimum number of the given set, then just by putting this GCD between each number. But, if this GCD is not the minimum element of the given set, then unfortunately “no array can be build”.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
int gcd( int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
int findGCD(vector< int > arr, int n)
{
int result = arr[0];
for ( int i = 1; i < n; i++)
result = gcd(arr[i], result);
return result;
}
void compute(vector< int > arr, int n)
{
vector< int > answer;
int GCD_of_array = findGCD(arr, n);
if (GCD_of_array == arr[0])
{
answer.push_back(arr[0]);
for ( int i = 1; i < n; i++)
{
answer.push_back(arr[0]);
answer.push_back(arr[i]);
}
for ( int i = 0; i < answer.size(); i++)
cout << answer[i] << " " ;
}
else
cout << "No array can be build" ;
}
int main()
{
int n = 3;
int input[]= {2, 5, 6, 7, 11};
set< int > GCD(input, input + n);
vector< int > arr;
set< int >::iterator it;
for (it = GCD.begin(); it!= GCD.end(); ++it)
arr.push_back(*it);
compute(arr,n);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG
{
static int gcd( int a,
int b)
{
if (a == 0 )
return b;
return gcd(b % a, a);
}
public static int findGCD(ArrayList<Integer>
arr, int n)
{
int result = arr.get( 0 );
for ( int i = 1 ; i < n; i++)
result = gcd(arr.get(i),
result);
return result;
}
public static void compute(ArrayList<Integer>
arr, int n)
{
ArrayList<Integer> answer =
new ArrayList<Integer>();
int GCD_of_array = findGCD(arr, n);
if (GCD_of_array == arr.get( 0 ))
{
answer.add(arr.get( 0 ));
for ( int i = 1 ; i < n; i++)
{
answer.add(arr.get( 0 ));
answer.add(arr.get(i));
}
for ( int i = 0 ;
i < answer.size(); i++)
System.out.print(answer.get(i) + " " );
}
else
System.out.print( "No array " +
"can be build" );
}
public static void main(String args[])
{
int n = 3 ;
Integer input[]= { 2 , 5 , 6 , 7 , 11 };
HashSet<Integer> GCD = new HashSet<Integer>
(Arrays.asList(input));
ArrayList<Integer> arr =
new ArrayList<Integer>();
for ( int v : GCD)
arr.add(v);
compute(arr, n);
}
}
|
Python3
from math import gcd
def findGCD(arr, n):
result = arr[ 0 ]
for i in range ( 1 ,n):
result = gcd(arr[i], result)
return result
def compute(arr, n):
answer = []
GCD_of_array = findGCD(arr, n)
if (GCD_of_array = = arr[ 0 ]):
answer.append(arr[ 0 ])
for i in range ( 1 ,n):
answer.append(arr[ 0 ])
answer.append(arr[i])
for i in range ( len (answer)):
print (answer[i],end = " " )
else :
print ( "No array can be build" )
if __name__ = = '__main__' :
n = 3
input = [ 2 , 5 , 6 , 7 , 11 ]
GCD = set ()
for i in range ( len ( input )):
GCD.add( input [i])
arr = []
for i in GCD:
arr.append(i)
compute(arr,n)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int gcd( int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
static int findGCD(List< int > arr,
int n)
{
int result = arr[0];
for ( int i = 1; i < n; i++)
result = gcd(arr[i],
result);
return result;
}
static void compute(List< int > arr,
int n)
{
List< int > answer = new List< int >();
int GCD_of_array = findGCD(arr, n);
if (GCD_of_array == arr[0])
{
answer.Add(arr[0]);
for ( int i = 1; i < n; i++)
{
answer.Add(arr[0]);
answer.Add(arr[i]);
}
for ( int i = 0; i < answer.Count; i++)
Console.Write(answer[i] + " " );
}
else
Console.Write( "No array " +
"can be build" );
}
static void Main()
{
int n = 3;
int []input= new int []{2, 5, 6, 7, 11};
HashSet< int > GCD = new HashSet< int >(input);
List< int > arr = new List< int >();
foreach ( int b in GCD)
arr.Add(b);
compute(arr, n);
}
}
|
Javascript
<script>
function gcd(a , b) {
if (a == 0)
return b;
return gcd(b % a, a);
}
function findGCD( arr , n) {
var result = arr[0];
for (i = 1; i < n; i++)
result = gcd(arr[i], result);
return result;
}
function compute(arr , n) {
var answer = new Array();
var GCD_of_array = findGCD(arr, n);
if (GCD_of_array == arr[0]) {
answer.add(arr[0]);
for (i = 1; i < n; i++) {
answer.add(arr[0]);
answer.add(arr[i]);
}
for ( var i = 0; i < answer.length; i++)
document.write(answer[i] + " " );
} else
document.write( "No array " + "can be build" );
}
var n = 3;
var input = [ 2, 5, 6, 7, 11 ];
compute(input, n);
</script>
|
OutputNo array can be build
Complexity Analysis:
- Time Complexity : O(nlog(n)), where n is the size of array given.
- Auxiliary Space: O(n)
Please suggest if someone has a better solution which is more efficient in terms of space and time.
Share your thoughts in the comments
Please Login to comment...