Maximum value of ( arr [ i ] * arr [ j ] ) + ( arr [ j ] | Function to find the value of the expression a * b + ( b - a ) ; Function to find the maximum value of the expression a * b + ( b - a ) possible for any pair ( a , b ) ; Sohe vector in ascending order ; Stores the maximum value ; Update ans by choosing the pair of the minimum and 2 nd minimum ; Update ans by choosing the pair of maximum and 2 nd maximum ; Return the value of ans ; Given inputs
function calc ( a , b ) { return ( a * b ) + ( b - a ) ; } function findMaximum ( arr , N ) { arr . sort ( function ( a , b ) { return a - b } ) let ans = Number . MIN_VALUE ; ans = Math . max ( ans , calc ( arr [ 0 ] , arr [ 1 ] ) ) ; ans = Math . max ( ans , calc ( arr [ N - 2 ] , arr [ N - 1 ] ) ) ; return ans ; } let arr = [ - 47 , 0 , 12 ] let N = arr . length ; document . write ( findMaximum ( arr , N ) ) ;