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 <bits/stdc++.h>
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 <stdio.h>
// 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
<script>
// javascript Program to implement
// the above approach
// Function to check if it is possible
// to form an array whose sum and the
// product is the same or not
function canPossibleReplacement(N, arr)
{
// Find the sum of the array
// initialize sum
var S = 0;
var 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
var P = 1;
for (i = 0; i < N; i++)
{
P *= i;
}
// Check a complete integer y
// for every x
for (i = 0; i < N; i++)
{
var x = arr[i];
var 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
var N = 3;
var arr = [1, 3, 4]
if (canPossibleReplacement(N, arr) == 1)
document.write("Yes");
else
document.write("No");
// This code is contributed by ipg2016107.
</script>
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
Check whether it is possible to make both arrays equal by modifying a single element Given two sequences of integers 'A' and 'B', and an integer 'k'. The task is to check if we can make both sequences equal by modifying any one element from the sequence A in the following way: We can add any number from the range [-k, k] to any element of A. This operation must only be performed onc
11 min read
Check if sum of array can be made equal to X by removing either the first or last digits of every array element Given an array arr[] consisting of N positive integers and a positive integer X, the task is to check if the sum of the given array can be made equal to X by removing either the first or last digit of every array element. If it is possible to make the sum of array elements equal to X, then print "Ye
7 min read
Make all array elements even by replacing adjacent pair of array elements with their sum Given an array arr[] of size N, the task is to make all array elements even by replacing a pair of adjacent elements with their sum. Examples: Input: arr[] = { 2, 4, 5, 11, 6 }Output: 1Explanation:Replacing a pair (arr[2], arr[3]) with their sum ( = 5 + 11 = 16) modifies arr[] to { 2, 4, 16, 16, 6 }
8 min read
Maximize product of array by replacing array elements with its sum or product with element from another array Given two arrays A[] and B[] consisting of N integers, the task is to update array A[] by assigning every array element A[i] to a single element B[j] and update A[i] to A[i] + B[j] or A[i] * B[j], such that the product of the array A[] is maximized. Note: Every array element in both the arrays can b
7 min read
Make all array elements equal by replacing adjacent pairs by their sum Given an array arr[] consisting of N integers, the task is to replace a minimum number of pairs of adjacent elements by their sum to make all array elements equal. Print the minimum number of such operations required. Examples: Input: arr[] = {1, 2, 3}Output: 1Explanation: Replace arr[0] and arr[1]
8 min read
Count ways to split array into two equal sum subarrays by replacing each array element to 0 once Given an array arr[] consisting of N integers, the task is to count the number of ways to split the array into two subarrays of equal sum after changing a single array element to 0. Examples: Input: arr[] = {1, 2, -1, 3}Output: 4Explanation: Replacing arr[0] by 0, arr[] is modified to {0, 2, -1, 3}.
11 min read