VOOZH about

URL: https://www.geeksforgeeks.org/dsa/array-gcd-subset-belongs-given-array/

⇱ Array with GCD of any of its subset belongs to the given array - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

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:


Output
No 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.

Comment
Article Tags: