Saturday, November 16, 2024
Google search engine
HomeData Modelling & AIConstruct longest possible sequence of unique elements with given LCM

Construct longest possible sequence of unique elements with given LCM

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 <bits/stdc++.h>
using namespace std;
 
// Function to construct an array of
// unique elements whose LCM is N
void constructArrayWithGivenLCM(int N)
{
    // Stores array elements
    // whose LCM is N
    vector<int> 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 Code
int main()
{
    // Given N
    int N = 12;
 
    // Function Call
    constructArrayWithGivenLCM(N);
 
    return 0;
}


Java




// Java program to implement
// the above approach
import java.util.Arrays;
 
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[]
    Arrays.sort(newArr);
 
    // Print array elements
    for(int i = j; i < N; i++) 
    {
        System.out.print(newArr[i] + " ");
    }
}
 
// Driver Code
public 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 approach
from math import sqrt,ceil,floor
 
# Function to construct an array of
# unique elements whose LCM is N
def 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 Code
if __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 approach
using 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




<script>
 
    // JavaScript program to implement the above approach
     
    // Function to construct an array of
    // unique elements whose LCM is N
    function constructArrayWithGivenLCM(N)
    {
 
      // Stores array elements
      // whose LCM is N
      let newArr = new Array(N);
      newArr.fill(0);
 
      let j = 0;
 
      // Iterate over the range
      // [1, sqrt(N)]
      for(let 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 (parseInt(N / i, 10) != i)
          {
            newArr[j] = parseInt(N / i, 10);
            j++;
          }
        }
      }
 
      // Sort the array newArr[]
      newArr.sort(function(a, b){return a - b});
 
      // Print array elements
      for(let i = j; i < N; i++)
      {
        document.write(newArr[i] + " ");
      }
    }
     
    // Given N
    let N = 12;
  
    // Function Call
    constructArrayWithGivenLCM(N);
 
</script>


Output: 

1 2 3 4 6 12

 

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

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Last Updated :
11 Jun, 2021
Like Article
Save Article


Previous

<!–

8 Min Read | Java

–>


Next


<!–

8 Min Read | Java

–>

RELATED ARTICLES

Most Popular

Recent Comments