📜  给定 sum 和 product 值可能的最小数组大小

📅  最后修改于: 2022-05-13 01:56:07.347000             🧑  作者: Mango

给定 sum 和 product 值可能的最小数组大小

给定两个正整数SP ,任务是找到数组的最小可能大小,使得元素之和为S并且元素的乘积为P 。如果不存在任何这样的数组,则打印“-1”

例子:

方法:可以根据以下观察解决给定的问题:

  • 使用N个数字,可以组成一个数组 大小N总和S
  • P的值介于[0, (S/N) N ]之间时,可以实现任何乘积值。

请按照以下步骤解决给定的问题:

  • 首先检查SP的值是否相同,然后返回 1,因为S值本身用于制作最小大小的数组。
  • 使用变量i迭代范围[2, S] ,如果(S/i) >= pow(P, 1/i)的值,则打印i的值作为形成的数组的最小大小。
  • 完成上述步骤后,如果没有任何可能的值i满足上述条件,则打印“-1”

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the minimum size
// of array  with sum S and product P
int minimumSizeArray(int S, int P)
{
    // Base Case
    if (S == P) {
 
        return 1;
    }
 
    // Iterate through all values of S
    // and check the mentioned condition
    for (int i = 2; i <= S; i++) {
 
        double d = i;
        if ((S / d) >= pow(P, 1.0 / d)) {
            return i;
        }
    }
 
    // Otherwise, print "-1"
    return -1;
}
 
// Driver Code
int main()
{
    int S = 5, P = 6;
    cout << minimumSizeArray(S, P);
 
    return 0;
}


Java
// Java program for the above approach
class GFG{
 
// Function to find the minimum size
// of array  with sum S and product P
static int minimumSizeArray(int S, int P)
{
    // Base Case
    if (S == P) {
 
        return 1;
    }
 
    // Iterate through all values of S
    // and check the mentioned condition
    for (int i = 2; i <= S; i++) {
 
        double d = i;
        if ((S / d) >= Math.pow(P, 1.0 / d)) {
            return i;
        }
    }
 
    // Otherwise, print "-1"
    return -1;
}
 
// Driver Code
public static void main(String args[])
{
    int S = 5, P = 6;
       System.out.println(minimumSizeArray(S, P));
}
}
 
// This code is contributed by AnkThon


Python3
# python program for the above approach
 
# Function to find the minimum size
# of array with sum S and product P
def minimumSizeArray(S, P):
 
    # Base Case
    if (S == P):
        return 1
 
    # Iterate through all values of S
    # and check the mentioned condition
    for i in range(2, S+1):
 
        d = i
        if ((S / d) >= pow(P, 1.0 / d)):
            return i
 
    # Otherwise, print "-1"
    return -1
 
# Driver Code
if __name__ == "__main__":
    S = 5
    P = 6
    print(minimumSizeArray(S, P))
 
    # This code is contributed by rakeshsahni


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to find the minimum size
// of array  with sum S and product P
static int minimumSizeArray(int S, int P)
{
    // Base Case
    if (S == P) {
 
        return 1;
    }
 
    // Iterate through all values of S
    // and check the mentioned condition
    for (int i = 2; i <= S; i++) {
 
        double d = i;
        if ((S / d) >= Math.Pow(P, 1.0 / d)) {
            return i;
        }
    }
 
    // Otherwise, print "-1"
    return -1;
}
 
// Driver Code
public static void Main()
{
    int S = 5, P = 6;
    Console.Write(minimumSizeArray(S, P));
}
}
 
// This code is contributed by SURENDRA_GANGWAR.


Javascript



输出:
2

时间复杂度: O(log P)
辅助空间: O(1)