Check if possible to make Array sum equal to Array product by replacing exactly one element | Java 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 ; Find the sum of the array initialize sum ; Iterate through all elements and add them to sum ; Find the product of the array ; Check a complete integer y for every x ; If got such y ; If no such y exist ; Driver Code
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 " ) ; } }