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.
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
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”.
No array can be build
Time Complexity : O(nlog(n)), where n is the size of array given.
- Find maximum subset sum formed by partitioning any subset of array into 2 partitions with equal sum
- Find whether an array is subset of another array | Added Method 3
- Find whether an array is subset of another array using Map
- Find if there is any subset of size K with 0 sum in an array of -1 and +1
- Largest divisible subset in array
- Maximum product subset of an array
- Minimum value among AND of elements of every subset of an array
- Subset array sum by generating all the subsets
- Minimum product subset of an array
- Find the sum of maximum difference possible from all subset of a given array.
- Check whether bitwise AND of a number with any subset of an array is zero or not
- Find the Largest divisor Subset in the Array
- Size of the largest divisible subset in an Array
- Find the smallest positive integer value that cannot be represented as sum of any subset of a given array
- Maximum number of elements greater than X after equally distributing subset of array
- Find original array from encrypted array (An array of sums of other elements)
- Find an element in array such that sum of left array is equal to sum of right array
- Find Array formed by adding each element of given array with largest element in new array to its left
- Minimum cost to reach end of array array when a maximum jump of K index is allowed
- Minimum number of operations to convert array A to array B by adding an integer into a subarray
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.