# Check if possible to make Array sum equal to Array product by replacing exactly one element

Last Updated : 18 Jul, 2022

Given an array arr[] consisting of N non-negative integers, the task is to check if it is possible to make the sum of the array equal to the product of the array element by replacing exactly one array element with any non-negative integer.

Examples:

Input: arr[] = {1, 3, 4}
Output: Yes
Explanation:
Replacing the last element of the array with 2 modifies the array to {1, 3, 2}. The sum of array element  = 6 and  The product of array element is 1*2*3 = 6. Therefore, print Yes.

Input: arr[] = {1, 2, 3}
Output: No

Approach: The given problem can be solved by using the following mathematical observations:

Consider the sum of array element as S and product of array element as P and after replacing any array element X with Y the sum and the product of array element must be the same, the equation becomes:

=> S – X + Y = P * Y / X
=> Y = (S – X) / (P / X – 1)

From the above observations, the idea is to find the sum and the product of array elements as S and P and then iterate over the array element(say X) and find the value of Y using the above equation and if there exist any array element having the value of Y as a complete non-negative integer, then print Yes. Otherwise, print No.

Below is the implementation of the above approach:

## 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)` `        ``printf``(``"Yes"``);` `    ``else` `        ``printf``(``"No"``);` `    ``return` `0;` `}`   `// This code is contributed by Sania Kumari Gupta`

## C

 `// C Program to implement the above approach` `#include `   `// 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)` `        ``printf``(``"Yes"``);` `    ``else` `        ``printf``(``"No"``);` `    ``return` `0;` `}`   `// This code is contributed by Sania Kumari Gupta`

## 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

 ``

Output:

`Yes`

Time Complexity: O(N)
Auxiliary Space: O(1)

Previous
Next