📌  相关文章
📜  生成一个数组,其中所有子数组的乘积的长度超过一个可被K整除的长度

📅  最后修改于: 2021-05-04 18:34:06             🧑  作者: Mango

给定两个正整数NK ,任务是生成一个长度为N的数组,以使每个长度大于1的子数组的乘积必须被K整除,并且数组的最大元素必须小于K。如果没有这样的数组,则打印-1

例子:

方法:可以通过以下观察来解决给定的问题:

因此,这个想法是取K的两个除数,即d1d2 ,使得d1 * d2 = K并将它们交替放置在数组中。请按照以下步骤解决问题:

  1. 初始化两个整数变量d1d2
  2. 检查K是否为质数。如果发现是真的,则打印-1
  3. 否则,计算K的除数,并将两个除数存储在d1d2中
  4. 之后,从i = 0遍历到N – 1
  5. 如果是偶数,请打印d1 。否则,打印d2

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to check if the required
// array can be genrated 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 genrated 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 genrated 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 genrated 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


输出:

3 7 3 7 3

时间复杂度: O(N +√K)
辅助空间: O(1)