给定一个整数K ,任务是构造一个最大长度的数组,其中所有数组元素的乘积等于K ,使得除了第一个数组元素之外的每个数组元素都可以被其前一个相邻元素整除。
注意:生成的数组中的每个数组元素都必须大于 1。
例子:
Input: K = 4
Output: {2, 2}
Explanation:
The second element, i.e. arr[1] (= 2) is divisible by the first element, i.e. arr[0] (= 2).
Product of the array elements = 2 * 2 = 4(= K).
Therefore, the array satisfies the required condition.
Input: K = 23
Output: {23}
方法:解决这个问题的想法是找到K 的所有质因数和它们各自的幂,使得:
prime_factor[1]x * prime_factor[2]y … * primefactor[i]z = K
请按照以下步骤解决此问题:
- 找出具有最大幂的素因数,例如X ,例如Y。
- 然后, Y将是所需数组的长度。
- 因此,构造一个长度为Y的数组,其中的所有元素都等于X。
- 要使数组的乘积等于K ,请将最后一个元素乘以K / X y 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to construct longest array
// with product K such that each element
// is divisible by its previous element
void findLongestArray(int K)
{
// Stores the prime factors of K
vector > primefactors;
int K_temp = K;
for (int i = 2; i * i <= K; i++) {
// Stores the power to which
// primefactor i is raised
int count = 0;
while (K_temp % i == 0) {
K_temp /= i;
count++;
}
if (count > 0)
primefactors.push_back({ count, i });
}
if (K_temp != 1)
primefactors.push_back(
{ 1, K_temp });
// Sort prime factors in descending order
sort(primefactors.rbegin(),
primefactors.rend());
// Stores the final array
vector answer(
primefactors[0].first,
primefactors[0].second);
// Multiply the last element by K
answer.back() *= K;
for (int i = 0;
i < primefactors[0].first; i++) {
answer.back() /= primefactors[0].second;
}
// Print the constructed array
cout << "{";
for (int i = 0; i < (int)answer.size(); i++) {
if (i == answer.size() - 1)
cout << answer[i] << "}";
else
cout << answer[i] << ", ";
}
}
// Driver Code
int main()
{
int K = 4;
findLongestArray(K);
}
Java
// java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
// Function to construct longest array
// with product K such that each element
// is divisible by its previous element
static void findLongestArray(int K)
{
// Stores the prime factors of K
ArrayList primefactors = new ArrayList<>();
int K_temp = K;
for (int i = 2; i * i <= K; i++) {
// Stores the power to which
// primefactor i is raised
int count = 0;
while (K_temp % i == 0) {
K_temp /= i;
count++;
}
if (count > 0)
primefactors.add(new int[] { count, i });
}
if (K_temp != 1)
primefactors.add(new int[] { 1, K_temp });
// Sort prime factors in descending order
Collections.sort(primefactors, (x, y) -> {
if (x[0] != y[0])
return y[0] - x[0];
return y[1] - x[1];
});
// Stores the final array
int n = primefactors.get(0)[0];
int val = primefactors.get(0)[1];
int answer[] = new int[n];
Arrays.fill(answer, val);
// Multiply the last element by K
answer[n - 1] *= K;
for (int i = 0; i < n; i++) {
answer[n - 1] /= val;
}
// Print the constructed array
System.out.print("{");
for (int i = 0; i < answer.length; i++) {
if (i == answer.length - 1)
System.out.print(answer[i] + "}");
else
System.out.print(answer[i] + ", ");
}
}
// Driver Code
public static void main(String[] args)
{
int K = 4;
findLongestArray(K);
}
}
// This code is contributed by Kingash.
Python3
# Python 3 program for the above approach
# Function to construct longest array
# with product K such that each element
# is divisible by its previous element
def findLongestArray(K):
# Stores the prime factors of K
primefactors = []
K_temp = K
i = 2
while i * i <= K:
# Stores the power to which
# primefactor i is raised
count = 0
while (K_temp % i == 0):
K_temp //= i
count += 1
if (count > 0):
primefactors.append([count, i])
i += 1
if (K_temp != 1):
primefactors.append(
[1, K_temp])
# Sort prime factors in descending order
primefactors.sort()
# Stores the final array
answer = [primefactors[0][0],
primefactors[0][1]]
# Multiply the last element by K
answer[-1] *= K
for i in range(primefactors[0][0]):
answer[-1] //= primefactors[0][1]
# Print the constructed array
print("{", end = "")
for i in range(len(answer)):
if (i == len(answer) - 1):
print(answer[i], end = "}")
else:
print(answer[i], end = ", ")
# Driver Code
if __name__ == "__main__":
K = 4
findLongestArray(K)
# This code is contributed by ukasp.
输出:
{2, 2}
时间复杂度: O(√(K) * log(√(K)))
辅助空间: O(√(K))
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live