Maximum value of (arr[i] * arr[j]) + (arr[j] – arr[i])) possible for any pair in an array
Last Updated :
13 May, 2021
Given an array arr[] consisting of N integers, the task is to find the maximum possible value of the expression (arr[i] * arr[j]) + (arr[j] – arr[i])) for any pair (i, j), such that i ? j and 0 ? (i, j) < N.
Examples:
Input: arr[] = {-2, -8, 0, 1, 2, 3, 4}
Output: 22
Explanation:
For the pair (-8, -2) the value of the expression (arr[i] * arr[j]) + (arr[j] – arr[i])) = ((-2)*(-8) + (-2 – (-8))) = 22, which is maximum among all possible pairs.
Input: arr[] = {-47, 0, 12}
Output: 47
Naive Approach: The simplest approach to solve the given problem is to generate all possible pairs of the array and find the value of the given expression (arr[i] * arr[j]) + (arr[j] – arr[i])) for each pair and print the maximum value obtained for any pair.
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach: The above approach can also be optimized, which is based on the observation that the maximum value of the expression will be obtained by selecting the pairs of maximum and second maximum or a minimum and second minimum of the array. Follow the steps below to solve the problem:
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
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;
}
|
Java
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));
}
}
|
Python3
def calc(a, b):
return a * b + (b - a)
def findMaximum(arr, N):
arr = sorted (arr)
ans = - 10 * * 9
ans = max (ans, calc(arr[ 0 ], arr[ 1 ]))
ans = max (ans, calc(arr[N - 2 ],arr[N - 1 ]))
return ans
if __name__ = = '__main__' :
arr = [ 0 , - 47 , 12 ]
N = len (arr)
print (findMaximum(arr, N))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int calc( int a, int b)
{
return a * b + (b - a);
}
static int findMaximum(List< int > arr, int N)
{
arr.Sort();
int ans = -1000000000;
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()
{
List< int > arr = new List< int >{ 0, -47, 12 };
int N = ( int )arr.Count;
Console.Write(findMaximum(arr, N));
}
}
|
Javascript
<script>
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));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...