Maximum value of ( arr [ i ] * arr [ j ] ) + ( arr [ j ] | Java program for the above approach ; 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 ) ; Sort the 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 ; Driver Code ; Given inputs
import java . util . * ; class GFG { static int calc ( int a , int b ) { return a * b + ( b - a ) ; } static int findMaximum ( int [ ] arr , int N ) { Arrays . sort ( arr ) ; int ans = ( int ) - 1e9 ; ans = Math . max ( ans , calc ( arr [ 0 ] , arr [ 1 ] ) ) ; ans = Math . max ( ans , calc ( arr [ N - 2 ] , arr [ N - 1 ] ) ) ; return ans ; } public static void main ( String [ ] args ) { int [ ] arr = { 0 , - 47 , 12 } ; int N = arr . length ; System . out . println ( findMaximum ( arr , N ) ) ; } }