给定正整数N ,任务是构造最长排序的唯一元素的LCM等于N的序列。
例子:
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 }.
方法:可以通过以下观察来解决问题:
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.
请按照以下步骤解决此问题:
- 初始化一个数组,例如newArr [] ,以存储LCM等于N的所有唯一数组元素。
- 找到N的所有因子并将其存储在newArr []中。
- 对数组newArr []进行排序。
- 打印newArr []的所有数组元素。
下面是上述方法的实现:
C++
// C++ program to implement
// the above approach
#include
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 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
输出:
1 2 3 4 6 12
时间复杂度: O(√N)
辅助空间: O(1)