📌  相关文章
📜  检查是否可以通过恰好替换一个元素使 Array sum 等于 Array product

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

检查是否可以通过恰好替换一个元素使 Array sum 等于 Array product

给定一个由N个非负整数组成的数组arr[] ,任务是检查是否有可能通过将一个数组元素替换为任何非负整数来使数组的总和等于数组元素的乘积.

例子:

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

从上面的观察,想法是找到数组元素的和和乘积作为SP然后迭代数组元素(比如X )并使用上面的等式找到Y的值,如果存在任何数组元素将Y的值作为一个完整的非负整数,然后打印Yes 。否则,打印No

下面是上述方法的实现:

C++
// C++ Program to implement
// the above approach
#include 
using namespace std;
// Function to check if it is possible
// to form an array whose sum and the
// product is the same or not
int canPossibleReplacement(int N, int arr[])
{
 
    // Find the sum of the array
    // initialize sum
    int S = 0;
    int i;
 
    // Iterate through all elements and
    // add them to sum
    for (i = 0; i < N; i++)
        S += arr[i];
 
    // Find the product of the array
    int P = 1;
 
    for (i = 0; i < N; i++)
    {
        P *= i;
    }
 
    // Check a complete integer y
    // for every x
    for (int i = 0; i < N; i++)
    {
        int x = arr[i];
        int y = (S - x) / (P / x - 1);
 
        // If got such y
        if ((S - x + y) == (P * y) / x)
            return 1;
    }
 
    // If no such y exist
    return 0;
}
 
// Driver Code
int main()
{
    int N = 3;
    int arr[] = {1, 3, 4};
 
    if (canPossibleReplacement(N, arr) == 1)
        cout << ("Yes");
    else
        cout << ("No");
    return 0;
}
 
// This code is contributed by Potta Lokesh


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
     
// Function to check if it is possible
// to form an array whose sum and the
// product is the same or not
static int canPossibleReplacement(int N, int[] arr)
{
     
    // Find the sum of the array
    // initialize sum
    int S = 0;
    int i;
        
    // Iterate through all elements and
    // add them to sum
    for(i = 0; i < arr.length; i++)
        S +=  arr[i];
 
    // Find the product of the array
    int P = 1;
 
    for(i = 0; i < arr.length; i++)
    {
        P *= i;
    }
 
    // Check a complete integer y
    // for every x
    for(int x : arr)
    {
        int y = (S - x)/(P / x - 1);
 
        // If got such y
        if ((S - x + y) == (P * y) / x)
            return 1;
    }
 
    // If no such y exist
    return 0;
}
   
// Driver Code
public static void main(String[] args)
{
    int N = 3;
    int arr[] = { 1, 3, 4 };
     
    if (canPossibleReplacement(N, arr) == 1)
        System.out.print("Yes");
    else
        System.out.print("No");
}
}
 
// This code is contributed by sanjoy_62


Python3
# Python program for the above approach
 
# Function to check if it is possible
# to form an array whose sum and the
# product is the same or not
def canPossibleReplacement(N, arr):
 
    # Find the sum of the array
    S = sum(arr)
 
    # Find the product of the array
    P = 1
 
    for i in arr:
        P *= i
 
    # Check a complete integer y
    # for every x
    for x in arr:
 
        y = (S-x)//(P / x-1)
 
        # If got such y
        if (S-x + y) == (P * y)/x:
 
            return 'Yes'
 
    # If no such y exist
    return 'No'
 
 
# Driver Code
N, arr = 3, [1, 3, 4]
print(canPossibleReplacement(N, arr))


C#
// C# program for the above approach
 
using System;
 
class GFG{
     
// Function to check if it is possible
// to form an array whose sum and the
// product is the same or not
static int canPossibleReplacement(int N, int[] arr)
{
     
    // Find the sum of the array
    // initialize sum
    int S = 0;
    int i;
        
    // Iterate through all elements and
    // add them to sum
    for(i = 0; i < arr.Length; i++)
        S +=  arr[i];
 
    // Find the product of the array
    int P = 1;
 
    for(i = 0; i < arr.Length; i++)
    {
        P *= i;
    }
 
    // Check a complete integer y
    // for every x
    foreach(int x in arr)
    {
        int y = (S - x)/(P / x - 1);
 
        // If got such y
        if ((S - x + y) == (P * y) / x)
            return 1;
    }
 
    // If no such y exist
    return 0;
}
   
// Driver Code
public static void Main(string[] args)
{
    int N = 3;
    int []arr = { 1, 3, 4 };
     
    if (canPossibleReplacement(N, arr) == 1)
        Console.Write("Yes");
    else
         Console.Write("No");
}
}
 
// This code is contributed by AnkThon


Javascript


输出:
Yes

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