Construct longest possible sequence of unique elements with given LCM

• Last Updated : 11 Jun, 2021

Given a positive integer N, the task is to construct the longest sorted sequence of unique elements whose LCM of the is equal to N.

Examples:

Input: N = 12
Output: 1 2 3 4 6 12
Explanation:
LCM of {1, 2, 3, 4, 6, 12 } is N( = 12).
Therefore, the longest possible sequence is {1, 2, 3, 4, 6, 12 }.

Input: N = 9
Output: 1 3 9
Explanation:
LCM of { 1, 2, 9 } is N( = 9).
Therefore, the longest possible sequence is {1, 3, 9 }.

Approach: The problem can be solved based on the following observation:

If an array element is not a factor of N then the LCM of the array elements never be N. Therefore, the array elements must be the factor of N.

Follow the below steps to solve this problem:

Below is the implementation of the above approach:

C++

 // C++ program to implement// the above approach #include using namespace std; // Function to construct an array of// unique elements whose LCM is Nvoid constructArrayWithGivenLCM(int N){    // Stores array elements    // whose LCM is N    vector newArr;     // Iterate over the range    // [1, sqrt(N)]    for (int i = 1; i * i <= N;         i++) {         // If N is divisible        // by i        if (N % i == 0) {             // Insert i into newArr[]            newArr.push_back(i);             // If N is not perfect square            if (N / i != i) {                newArr.push_back(N / i);            }        }    }     // Sort the array newArr[]    sort(newArr.begin(), newArr.end());     // Print array elements    for (auto i : newArr) {         cout << i << " ";    }} // Driver Codeint main(){    // Given N    int N = 12;     // Function Call    constructArrayWithGivenLCM(N);     return 0;}

Java

 // Java program to implement// the above approachimport java.util.Arrays; class GFG{ // Function to construct an array of// unique elements whose LCM is Nstatic void constructArrayWithGivenLCM(int N){         // Stores array elements    // whose LCM is N    int newArr[] = new int[N];     int j = 0;         // Iterate over the range    // [1, sqrt(N)]    for(int i = 1; i * i <= N; i++)    {                 // If N is divisible        // by i        if (N % i == 0)        {                         // Insert i into newArr[]            newArr[j] = i;            j++;             // If N is not perfect square            if (N / i != i)            {                newArr[j] = N / i;                j++;            }        }    }     // Sort the array newArr[]    Arrays.sort(newArr);     // Print array elements    for(int i = j; i < N; i++)     {        System.out.print(newArr[i] + " ");    }} // Driver Codepublic static void main (String[] args){         // Given N    int N = 12;         // Function Call    constructArrayWithGivenLCM(N);}} // This code is contributed by AnkThon

Python3

 # Python3 program to implement# the above approachfrom math import sqrt,ceil,floor # Function to construct an array of# unique elements whose LCM is Ndef constructArrayWithGivenLCM(N):       # Stores array elements    # whose LCM is N    newArr = []     # Iterate over the range    # [1, sqrt(N)]    for i in range(1, ceil(sqrt(N + 1))):         # If N is divisible        # by i        if (N % i == 0):             # Insert i into newArr[]            newArr.append(i)             # If N is not perfect square            if (N // i != i):                newArr.append(N // i)     # Sort the array newArr[]    newArr = sorted(newArr)     # Print array elements    for i in newArr:        print(i, end = " ") # Driver Codeif __name__ == '__main__':   # Given N    N = 12     # Function Call    constructArrayWithGivenLCM(N) # This code is contributed by mohit kumar 29

C#

 // C# program to implement// the above approachusing System;class GFG{   // Function to construct an array of  // unique elements whose LCM is N  static void constructArrayWithGivenLCM(int N)  {     // Stores array elements    // whose LCM is N    int []newArr = new int[N];     int j = 0;     // Iterate over the range    // [1, sqrt(N)]    for(int i = 1; i * i <= N; i++)    {       // If N is divisible      // by i      if (N % i == 0)      {         // Insert i into newArr[]        newArr[j] = i;        j++;         // If N is not perfect square        if (N / i != i)        {          newArr[j] = N / i;          j++;        }      }    }     // Sort the array newArr[]    Array.Sort(newArr);     // Print array elements    for(int i = j; i < N; i++)     {      Console.Write(newArr[i] + " ");    }  }   // Driver Code  public static void Main(String[] args)  {     // Given N    int N = 12;     // Function Call    constructArrayWithGivenLCM(N);  }} // This code is contributed by Princi Singh

Javascript


Output:
1 2 3 4 6 12

Time Complexity: O(√ N)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up