给定两个正整数N和K ,任务是生成一个长度为N的数组,使得每个长度大于1 的子数组的乘积必须能被K整除,并且数组的最大元素必须小于K 。如果没有这样的数组,则打印-1 。
例子:
Input: N = 3, K = 20
Output: {15, 12, 5}
Explanation: All subarrays of length greater than 1 are {15, 12}, {12, 5}, {15, 12, 5} and their corresponding products are 180, 60 and 900, which are all divisible by 20.
Input: N = 4, K = 100
Output: {90, 90, 90, 90}
方法:给定的问题可以通过以下观察来解决:
It can be observed that by making the product of every subarray of length 2 divisible by K, the product of every subarray of length greater than 2 will automatically be divisible by K.
因此,我们的想法是取K 的两个除数,比如d1和d2 ,使得d1 * d2 = K并将它们交替放置在数组中。请按照以下步骤解决问题:
- 初始化两个整数变量d1和d2 。
- 检查 K 是否为素数。如果发现为真,则打印-1 。
- 否则,计算K的因数并将两个因数存储在d1和d2 中。
- 之后,从i = 0遍历到 N – 1 。
- 如果i是偶数,则打印d1 。否则,打印d2 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to check if the required
// array can be generated or not
void array_divisbleby_k(int N, int K)
{
// To check if divisor exists
bool flag = false;
// To store divisiors of K
int d1, d2;
// Check if K is prime or not
for (int i = 2; i * i <= K; i++) {
if (K % i == 0) {
flag = true;
d1 = i;
d2 = K / i;
break;
}
}
// If array can be generated
if (flag) {
// Print d1 and d2 alternatively
for (int i = 0; i < N; i++) {
if (i % 2 == 1) {
cout << d2 << " ";
}
else {
cout << d1 << " ";
}
}
}
else {
// No such array can be generated
cout << -1;
}
}
// Driver Code
int main()
{
// Given N and K
int N = 5, K = 21;
// Function Call
array_divisbleby_k(N, K);
return 0;
}
Java
// Java program for the above approach
class GFG{
// Function to check if the required
// array can be generated or not
public static void array_divisbleby_k(int N,
int K)
{
// To check if divisor exists
boolean flag = false;
// To store divisiors of K
int d1 = 0, d2 = 0;
// Check if K is prime or not
for(int i = 2; i * i <= K; i++)
{
if (K % i == 0)
{
flag = true;
d1 = i;
d2 = K / i;
break;
}
}
// If array can be generated
if (flag)
{
// Print d1 and d2 alternatively
for(int i = 0; i < N; i++)
{
if (i % 2 == 1)
{
System.out.print(d2 + " ");
}
else
{
System.out.print(d1 + " ");
}
}
}
else
{
// No such array can be generated
System.out.print(-1);
}
}
// Driver Code
public static void main(String[] args)
{
// Given N and K
int N = 5, K = 21;
// Function Call
array_divisbleby_k(N, K);
}
}
// This code is contributed by divyesh072019
Python3
# Python3 program for the above approach
# Function to check if the required
# array can be generated or not
def array_divisbleby_k(N, K):
# To check if divisor exists
flag = False
# To store divisiors of K
d1, d2 = 0, 0
# Check if K is prime or not
for i in range(2, int(K ** (1 / 2)) + 1):
if (K % i == 0):
flag = True
d1 = i
d2 = K // i
break
# If array can be generated
if (flag):
# Print d1 and d2 alternatively
for i in range(N):
if (i % 2 == 1):
print(d2, end = " ")
else:
print(d1, end = " ")
else:
# No such array can be generated
print(-1)
# Driver Code
if __name__ == "__main__":
# Given N and K
N = 5
K = 21
# Function Call
array_divisbleby_k(N, K)
# This code is contributed by AnkThon
C#
// C# program for the above approach
using System;
class GFG{
// Function to check if the required
// array can be generated or not
public static void array_divisbleby_k(int N,
int K)
{
// To check if divisor exists
bool flag = false;
// To store divisiors of K
int d1 = 0, d2 = 0;
// Check if K is prime or not
for(int i = 2; i * i <= K; i++)
{
if (K % i == 0)
{
flag = true;
d1 = i;
d2 = K / i;
break;
}
}
// If array can be generated
if (flag)
{
// Print d1 and d2 alternatively
for(int i = 0; i < N; i++)
{
if (i % 2 == 1)
{
Console.Write(d2 + " ");
}
else
{
Console.Write(d1 + " ");
}
}
}
else
{
// No such array can be generated
Console.Write(-1);
}
}
// Driver Code
public static void Main(string[] args)
{
// Given N and K
int N = 5, K = 21;
// Function Call
array_divisbleby_k(N, K);
}
}
// This code is contributed by AnkThon
Javascript
输出:
3 7 3 7 3
时间复杂度: O(N + √K)
辅助空间: O(1)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。