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
- Smallest subsequence having GCD equal to GCD of given array
- Pair of integers having least GCD among all given pairs having GCD exceeding K
- Sum of maximum and minimum of Kth subset ordered by increasing subset sum
- Largest Subset with GCD 1
- Find the smallest positive integer value that cannot be represented as sum of any subset of a given array
- std::gcd | C++ inbuilt function for finding GCD
- Count of lists which are not a subset of any other given lists
- Find any pair with given GCD and LCM
- Check if there is any pair in a given range with GCD is divisible by k
- Check whether bitwise AND of a number with any subset of an array is zero or not
- Find if there is any subset of size K with 0 sum in an array of -1 and +1
- Split array into minimum number of subarrays having GCD of its first and last element exceeding 1
- Check if bitwise AND of any subset is power of two
- Largest subset where absolute difference of any two element is a power of 2
- Largest subset of rectangles such that no rectangle fit in any other rectangle
- Print any pair of integers with sum of GCD and LCM equals to N
- Given a number N in decimal base, find number of its digits in any base (base b)
- Count of Array elements greater than all elements on its left and at least K elements on its right
- Count of Array elements greater than all elements on its left and next K elements on its right
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.