Maximize sum of product and difference between any pair of array elements possible
Last Updated :
13 Apr, 2023
Given an array arr[] of size N, the task is to find the maximum value of arr[i] ∗ arr[j] + arr[i] − arr[j] for any pair (arr[i], arr[j]) from the given array, where i != j and 0 < i, j < N – 1.
Examples:
Input: arr[] = {1, 2, 3, 4, 5}
Output: 21
Explanation:
Among all the pairs of the array, the maximum value is obtained for the pair (arr[4], arr[3]), which is equal to
=> arr[4] * arr[3] + arr[4] – arr[3] = 5 * 4 + 5 – 4 = 20 + 1 = 21.
Input: {-4, -5, 0, 1, 3}
Output: 21
Explanation:
Among all the pairs of the array, the maximum value is obtained for the pair (arr[0], arr[1]), which is equal to
=> arr[0] * arr[1] + arr[0] – arr[1] = (-4) * (-5) + (-4) – (-5) = 20 + 1 = 21.
Naive Approach: The simplest approach to solve the problem is to traverse the array and generate all possible pairs (arr[i], arr[j]) (i != j) from the array and evaluate the expression for all the pairs. Finally, print the maximum of all the pairs.
Below is the implementation of the approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maxProductSum( int arr[], int N) {
int maxSum = INT_MIN;
for ( int i = 0; i < N - 1; i++) {
for ( int j = i + 1; j < N; j++) {
int sum1 = (arr[i] * arr[j]) + (arr[i] - arr[j]);
int sum2 = (arr[j] * arr[i]) + (arr[j] - arr[i]);
int sum = max(sum1, sum2);
maxSum = max(maxSum, sum);
}
}
return maxSum;
}
int main() {
int arr[] = { -4, -5, 0, 1, 3 };
int N = sizeof (arr) / sizeof (arr[0]);
int maxSum = maxProductSum(arr, N);
cout << maxSum << endl;
return 0;
}
|
Java
import java.util.*;
public class GFG {
static int maxProductSum( int [] arr, int N)
{
int maxSum = Integer.MIN_VALUE;
for ( int i = 0 ; i < N - 1 ; i++) {
for ( int j = i + 1 ; j < N; j++) {
int sum1
= (arr[i] * arr[j]) + (arr[i] - arr[j]);
int sum2
= (arr[j] * arr[i]) + (arr[j] - arr[i]);
int sum = Math.max(sum1, sum2);
maxSum = Math.max(maxSum, sum);
}
}
return maxSum;
}
public static void main(String[] args)
{
int [] arr = { - 4 , - 5 , 0 , 1 , 3 };
int N = arr.length;
int maxSum = maxProductSum(arr, N);
System.out.println(maxSum);
}
}
|
Python3
import sys
def maxProductSum(arr, N):
maxSum = - sys.maxsize
for i in range (N - 1 ):
for j in range (i + 1 , N):
sum1 = (arr[i] * arr[j]) + (arr[i] - arr[j])
sum2 = (arr[j] * arr[i]) + (arr[j] - arr[i])
sum = max (sum1, sum2)
maxSum = max (maxSum, sum )
return maxSum
if __name__ = = '__main__' :
arr = [ - 4 , - 5 , 0 , 1 , 3 ]
N = len (arr)
maxSum = maxProductSum(arr, N)
print (maxSum)
|
C#
using System;
public class GFG
{
static int MaxProductSum( int [] arr, int N)
{
int maxSum = int .MinValue;
for ( int i = 0; i < N - 1; i++)
{
for ( int j = i + 1; j < N; j++)
{
int sum1 = (arr[i] * arr[j]) + (arr[i] - arr[j]);
int sum2 = (arr[j] * arr[i]) + (arr[j] - arr[i]);
int sum = Math.Max(sum1, sum2);
maxSum = Math.Max(maxSum, sum);
}
}
return maxSum;
}
static void Main( string [] args)
{
int [] arr = { -4, -5, 0, 1, 3 };
int N = arr.Length;
int maxSum = MaxProductSum(arr, N);
Console.WriteLine(maxSum);
}
}
|
Javascript
function maxProductSum(arr, N) {
let maxSum = -Infinity;
for (let i = 0; i < N - 1; i++) {
for (let j = i + 1; j < N; j++) {
const sum1 = arr[i] * arr[j] + arr[i] - arr[j];
const sum2 = arr[j] * arr[i] + arr[j] - arr[i];
const sum = Math.max(sum1, sum2);
maxSum = Math.max(maxSum, sum);
}
}
return maxSum;
}
const arr = [-4, -5, 0, 1, 3];
const N = arr.length;
const maxSum = maxProductSum(arr, N);
console.log(maxSum);
|
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach: The optimal idea is based on the observation that the value of the expression can be maximum for the following 2 cases:
Follow the steps below to solve the problem:
- Sort the array arr[] in ascending order.
- Evaluate the expression for the pair arr[N – 1] and arr[N – 2] and store it in a variable, say max1.
- Similarly, evaluate the expression for the pair arr[1] and arr[0] and store it in a variable, say max2.
- Store the maximum of max1 and max2 in a variable, say ans.
- Print the value of ans as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int compute( int a, int b)
{
int ans = a * b + a - b;
return ans;
}
void findMaxValue( int arr[], int N)
{
sort(arr, arr + N);
int maxm = compute(arr[N - 1], arr[N - 2]);
maxm = max(maxm, compute(arr[1], arr[0]));
cout << maxm;
}
int main()
{
int arr[] = { -4, -5, 0, 1, 3 };
int N = sizeof (arr) / sizeof (arr[0]);
findMaxValue(arr, N);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG
{
static int compute( int a, int b)
{
int ans = a * b + a - b;
return ans;
}
static void findMaxValue( int arr[], int N)
{
Arrays.sort(arr);
int maxm = compute(arr[N - 1 ], arr[N - 2 ]);
maxm = Math.max(maxm, compute(arr[ 1 ], arr[ 0 ]));
System.out.print(maxm);
}
public static void main(String[] args)
{
int arr[] = { - 4 , - 5 , 0 , 1 , 3 };
int N = arr.length;
findMaxValue(arr, N);
}
}
|
Python3
def compute(a, b):
res = (a * b) + (a - b)
return res
def findMaxValue(arr, N):
arr.sort()
maxm = compute(arr[N - 1 ], arr[N - 2 ])
maxm = max (maxm, compute(arr[ 1 ], arr[ 0 ]));
print (maxm)
arr = [ - 4 , - 5 , 0 , 1 , 3 ]
N = len (arr)
findMaxValue(arr, N)
|
C#
using System;
public class GFG
{
static int compute( int a, int b)
{
int ans = a * b + a - b;
return ans;
}
static void findMaxValue( int [] arr, int N)
{
Array.Sort(arr);
int maxm = compute(arr[N - 1], arr[N - 2]);
maxm = Math.Max(maxm, compute(arr[1], arr[0]));
Console.WriteLine(maxm);
}
public static void Main(String[] args)
{
int [] arr = { -4, -5, 0, 1, 3 };
int N = arr.Length;
findMaxValue(arr, N);
}
}
|
Javascript
<script>
function compute(a, b)
{
var ans = a * b + a - b;
return ans;
}
function findMaxValue(arr, N)
{
arr.sort( function (a,b){ return a - b});
var maxm = compute(arr[N - 1], arr[N - 2]);
maxm = Math.max(maxm, compute(arr[1], arr[0]));
document.write(maxm);
}
var arr = [-4, -5, 0, 1, 3];
var N = arr.length;
findMaxValue(arr, N);
</script>
|
Time Complexity: O(N*log(N))
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...