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++
#include <bits/stdc++.h>
using namespace std;
int canPossibleReplacement( int N, int arr[])
{
int S = 0;
int i;
for (i = 0; i < N; i++)
S += arr[i];
int P = 1;
for (i = 0; i < N; i++)
P *= i;
for ( int i = 0; i < N; i++) {
int x = arr[i];
int y = (S - x) / (P / x - 1);
if ((S - x + y) == (P * y) / x)
return 1;
}
return 0;
}
int main()
{
int N = 3;
int arr[] = { 1, 3, 4 };
if (canPossibleReplacement(N, arr) == 1)
printf ( "Yes" );
else
printf ( "No" );
return 0;
}
|
C
#include <stdio.h>
int canPossibleReplacement( int N, int arr[])
{
int S = 0;
int i;
for (i = 0; i < N; i++)
S += arr[i];
int P = 1;
for (i = 0; i < N; i++)
P *= i;
for ( int i = 0; i < N; i++) {
int x = arr[i];
int y = (S - x) / (P / x - 1);
if ((S - x + y) == (P * y) / x)
return 1;
}
return 0;
}
int main()
{
int N = 3;
int arr[] = { 1, 3, 4 };
if (canPossibleReplacement(N, arr) == 1)
printf ( "Yes" );
else
printf ( "No" );
return 0;
}
|
Java
import java.util.*;
class GFG{
static int canPossibleReplacement( int N, int [] arr)
{
int S = 0 ;
int i;
for (i = 0 ; i < arr.length; i++)
S += arr[i];
int P = 1 ;
for (i = 0 ; i < arr.length; i++)
{
P *= i;
}
for ( int x : arr)
{
int y = (S - x)/(P / x - 1 );
if ((S - x + y) == (P * y) / x)
return 1 ;
}
return 0 ;
}
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" );
}
}
|
Python3
def canPossibleReplacement(N, arr):
S = sum (arr)
P = 1
for i in arr:
P * = i
for x in arr:
y = (S - x) / / (P / x - 1 )
if (S - x + y) = = (P * y) / x:
return 'Yes'
return 'No'
N, arr = 3 , [ 1 , 3 , 4 ]
print (canPossibleReplacement(N, arr))
|
C#
using System;
class GFG{
static int canPossibleReplacement( int N, int [] arr)
{
int S = 0;
int i;
for (i = 0; i < arr.Length; i++)
S += arr[i];
int P = 1;
for (i = 0; i < arr.Length; i++)
{
P *= i;
}
foreach ( int x in arr)
{
int y = (S - x)/(P / x - 1);
if ((S - x + y) == (P * y) / x)
return 1;
}
return 0;
}
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" );
}
}
|
Javascript
<script>
function canPossibleReplacement(N, arr)
{
var S = 0;
var i;
for (i = 0; i < N; i++)
S += arr[i];
var P = 1;
for (i = 0; i < N; i++)
{
P *= i;
}
for (i = 0; i < N; i++)
{
var x = arr[i];
var y = (S - x) / (P / x - 1);
if ((S - x + y) == (P * y) / x)
return 1;
}
return 0;
}
var N = 3;
var arr = [1, 3, 4]
if (canPossibleReplacement(N, arr) == 1)
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...