Check if possible to make Array sum equal to Array product by replacing exactly one element | 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
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 ( " " ) ; else document . write ( " " ) ;