Maximum value of ( arr [ i ] * arr [ j ] ) + ( arr [ j ] | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int calc ( int a , int b ) { return a * b + ( b - a ) ; } int findMaximum ( vector < int > arr , int N ) { sort ( arr . begin ( ) , arr . end ( ) ) ; int ans = -1e9 ; ans = max ( ans , calc ( arr [ 0 ] , arr [ 1 ] ) ) ; ans = max ( ans , calc ( arr [ N - 2 ] , arr [ N - 1 ] ) ) ; return ans ; } int main ( ) { vector < int > arr = { 0 , -47 , 12 } ; int N = ( int ) arr . size ( ) ; cout << findMaximum ( arr , N ) ; return 0 ; }