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\in [1, 1000] , 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++
// C++ implementation to generate the
// required array
#include <bits/stdc++.h>
using namespace std;
// Function to return gcd of a and b
int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
// Function to find gcd of
// array of numbers
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;
}
// Function to generate the array
// with required constraints.
void compute(vector<int> arr, int n)
{
vector<int> answer;
// computing GCD of the given set
int GCD_of_array = findGCD(arr, n);
// Solution exists if GCD of array is equal
// to the minimum element of the array
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]);
}
// Printing the built array
for (int i = 0; i < answer.size(); i++)
cout << answer[i] << " ";
}
else
cout << "No array can be build";
}
// Driver function
int main()
{
// Taking in the input and initializing
// the set STL set in cpp has a property
// that it maintains the elements in
// sorted order, thus we do not need
// to sort them externally
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);
// Calling the computing function.
compute(arr,n);
return 0;
}
Java
// Java implementation
// to generate the
// required array
import java.io.*;
import java.util.*;
class GFG
{
// Function to return
// gcd of a and b
static int gcd(int a,
int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
// Function to find gcd
// of array of numbers
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;
}
// Function to generate
// the array with required
// constraints.
public static void compute(ArrayList<Integer>
arr, int n)
{
ArrayList<Integer> answer =
new ArrayList<Integer>();
// computing GCD of
// the given set
int GCD_of_array = findGCD(arr, n);
// Solution exists if GCD
// of array is equal to the
// minimum element of the array
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));
}
// Printing the
// built array
for (int i = 0;
i < answer.size(); i++)
System.out.print(answer.get(i) + " ");
}
else
System.out.print("No array " +
"can be build");
}
// Driver Code
public static void main(String args[])
{
// Taking in the input and
// initializing the set STL
// set in cpp has a property
// that it maintains the
// elements in sorted order,
// thus we do not need to
// sort them externally
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);
// Calling the
// computing function.
compute(arr, n);
}
}
// This code is contributed by
// Manish Shaw(manishshaw1)
Python3
from math import gcd
# Python 3 implementation to generate the
# required array
# Function to find gcd of
# array of numbers
def findGCD(arr, n):
result = arr[0]
for i in range(1,n):
result = gcd(arr[i], result)
return result
# Function to generate the array
# with required constraints.
def compute(arr, n):
answer = []
# computing GCD of the given set
GCD_of_array = findGCD(arr, n)
# Solution exists if GCD of array is equal
# to the minimum element of the array
if(GCD_of_array == arr[0]):
answer.append(arr[0])
for i in range(1,n):
answer.append(arr[0])
answer.append(arr[i])
# Printing the built array
for i in range(len(answer)):
print(answer[i],end = " ")
else:
print("No array can be build")
# Driver function
if __name__ == '__main__':
# Taking in the input and initializing
# the set STL set in cpp has a property
# that it maintains the elements in
# sorted order, thus we do not need
# to sort them externally
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)
# Calling the computing function.
compute(arr,n)
# This code is contributed by
# Surendra_Gangwar
C#
// C# implementation
// to generate the
// required array
using System;
using System.Collections.Generic;
class GFG
{
// Function to return
// gcd of a and b
static int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
// Function to find gcd
// of array of numbers
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;
}
// Function to generate
// the array with required
// constraints.
static void compute(List<int> arr,
int n)
{
List<int> answer = new List<int>();
// computing GCD of
// the given set
int GCD_of_array = findGCD(arr, n);
// Solution exists if GCD
// of array is equal to the
// minimum element of the array
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]);
}
// Printing the
// built array
for (int i = 0; i < answer.Count; i++)
Console.Write(answer[i] + " ");
}
else
Console.Write("No array " +
"can be build");
}
// Driver Code
static void Main()
{
// Taking in the input and
// initializing the set STL
// set in cpp has a property
// that it maintains the
// elements in sorted order,
// thus we do not need to
// sort them externally
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);
// Calling the
// computing function.
compute(arr, n);
}
}
// This code is contributed by
// Manish Shaw(manishshaw1)
JavaScript
<script>
// javascript implementation
// to generate the
// required array
// Function to return
// gcd of a and b
function gcd(a , b) {
if (a == 0)
return b;
return gcd(b % a, a);
}
// Function to find gcd
// of array of numbers
function findGCD( arr , n) {
var result = arr[0];
for (i = 1; i < n; i++)
result = gcd(arr[i], result);
return result;
}
// Function to generate
// the array with required
// constraints.
function compute(arr , n) {
var answer = new Array();
// computing GCD of
// the given set
var GCD_of_array = findGCD(arr, n);
// Solution exists if GCD
// of array is equal to the
// minimum element of the array
if (GCD_of_array == arr[0]) {
answer.add(arr[0]);
for (i = 1; i < n; i++) {
answer.add(arr[0]);
answer.add(arr[i]);
}
// Printing the
// built array
for (var i = 0; i < answer.length; i++)
document.write(answer[i] + " ");
} else
document.write("No array " + "can be build");
}
// Driver Code
// Taking in the input and
// initializing the set STL
// set in cpp has a property
// that it maintains the
// elements in sorted order,
// thus we do not need to
// sort them externally
var n = 3;
var input = [ 2, 5, 6, 7, 11 ];
// Calling the
// computing function.
compute(input, n);
// This code is contributed by umadevi9616
</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.
Similar Reads
Find the subset of Array with given LCM Given an array arr[] consisting of N positive integers and a positive integer X, the task is to find the subset of the given array whose Lowest Common Multiple(LCM) is X. If there doesn't exists any subset then print "-1". Examples: Input: arr[ ] = {2, 4, 3, 5}, X = 20Output: {4, 5}Explanation:Consi
8 min read
Convert the array such that the GCD of the array becomes 1 Given an array of positive elements and a positive integer k, the task is to convert the GCD of the array to 1. To make it possible, only one operation is allowed any number of times i.e. choose any element of the array & divide with a number d where d <= k. Examples: Input : arr = {10, 15, 3
9 min read
Remove an element to maximize the GCD of the given array Given an array arr[] of length N ⥠2. The task is to remove an element from the given array such that the GCD of the array after removing it is maximized. Examples: Input: arr[] = {12, 15, 18} Output: 6 Remove 12: GCD(15, 18) = 3 Remove 15: GCD(12, 18) = 6 Remove 18: GCD(12, 15) = 3 Input: arr[] = {
13 min read
Count number of subsets of a set with GCD equal to a given number Given a set of positive integer elements, find the count of subsets with GCDs equal to given numbers.Examples: Input: arr[] = {2, 3, 4}, gcd[] = {2, 3} Output: Number of subsets with gcd 2 is 2 Number of subsets with gcd 3 is 1 The two subsets with GCD equal to 2 are {2} and {2, 4}. The one subset w
11 min read
Maximize sum of GCD of two subsets of given Array Given an array arr[] of positive integers of size N, the task is to divide the array into two non-empty subsets X and Y in such a way that the sum of their GCD turns out to be the maximum possible Examples: Input: N = 3, arr[] = {1, 2, 9}Output: 10Explanation: We can divide the array asX = (1, 2) wi
10 min read