Maximize value of given expression by choosing pair from Array
Last Updated :
19 Dec, 2022
Given an array A[] of length N, choose any two elements x and y from the array, the task is to find the maximum value of the expression (x * y + x – y).
Examples:
Input: A[] = {5, 2, 3}
Output: 17
Explanation: There are six possible pairs:
For pairs {2, 3} and {3, 2}, answer = 2 ? 3 + max(2?3, 3?2) = 7
For pairs {3, 5} and {5, 3} answer = 5 ? 3 + max(3?5, 5?3) = 17 and
For pairs {2, 5} and {5, 2}, answer = 2 ? 5 + max(2?5, 5?2) = 13.
So final answer is maximum of {7, 17, 13} = 17.
Input: A[] = {3, 7, 4, 9}
Output: 65
Approach: The problem can be solved based on the following observation:
Rewriting x?y + x?y = (x?1)?(y+1) + 1
To maximize the value of the expression we have to maximize the value of x*y.
So there can be two choices:
- If the minimum and second minimum both are negative, their product will be positive and that can be a valid answer.
- If the maximum and second maximum both are positive, their product will be positive and that can be the required pair.
So find the minimum and second minimum and maximum and second maximum and check for the above cases. The maximum will be the answer.
Follow the below steps to implement the above idea:
- First sort the array.
- Select minimum, second minimum, maximum, and second maximum element of the array.
- After that find the value of expression for x = minimum and y = second minimum & x = maximum and y = second maximum and store them in sum1 and sum2 respectively.
- Find the maximum between sum1 and sum2 and print that value
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
int maxValue( int array[], int n)
{
sort(array, array + n);
int a1 = array[0];
int b1 = array[1];
int b2 = array[n - 2];
int a2 = array[n - 1];
int sum1;
int sum2;
sum1 = a1 * b1 + max(a1 - b1, b1 - a1);
sum2 = a2 * b2 + max(b2 - a2, a2 - b2);
return max(sum1, sum2);
}
int main()
{
int arr[] = { 5, 2, 3 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << maxValue(arr, N) << endl;
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
public static int maxValue( int array[], int n)
{
Arrays.sort(array);
int a1 = array[ 0 ];
int b1 = array[ 1 ];
int b2 = array[n - 2 ];
int a2 = array[n - 1 ];
int sum1;
int sum2;
sum1 = a1 * b1 + Math.max(a1 - b1, b1 - a1);
sum2 = a2 * b2 + Math.max(b2 - a2, a2 - b2);
return Math.max(sum1, sum2);
}
public static void main(String[] args)
{
int arr[] = { 5 , 2 , 3 };
int N = arr.length;
System.out.println(maxValue(arr, N));
}
}
|
Python3
def maxValue(array, n):
array.sort()
a1 = array[ 0 ]
b1 = array[ 1 ]
b2 = array[n - 2 ]
a2 = array[n - 1 ]
sum1 = a1 * b1 + max (a1 - b1, b1 - a1)
sum2 = a2 * b2 + max (b2 - a2, a2 - b2)
return max (sum1, sum2)
if __name__ = = '__main__' :
arr = [ 5 , 2 , 3 ]
N = len (arr)
print (maxValue(arr, N))
|
C#
using System;
class GFG {
public static int maxValue( int [] array, int n)
{
Array.Sort(array);
int a1 = array[0];
int b1 = array[1];
int b2 = array[n - 2];
int a2 = array[n - 1];
int sum1;
int sum2;
sum1 = a1 * b1 + Math.Max(a1 - b1, b1 - a1);
sum2 = a2 * b2 + Math.Max(b2 - a2, a2 - b2);
return Math.Max(sum1, sum2);
}
public static void Main()
{
int [] arr = { 5, 2, 3 };
int N = arr.Length;
Console.Write(maxValue(arr, N));
}
}
|
Javascript
<script>
function maxValue(array, n){
array.sort();
let a1 = array[0];
let b1 = array[1];
let b2 = array[n - 2];
let a2 = array[n - 1];
let sum1;
let sum2;
sum1 = a1 * b1 + Math.max(a1 - b1, b1 - a1);
sum2 = a2 * b2 + Math.max(b2 - a2, a2 - b2);
return Math.max(sum1, sum2);
}
let arr = [ 5, 2, 3 ];
let N = arr.length;
console.log(maxValue(arr, N));
</script>
|
Time Complexity: O(N*log(N)) //the inbuilt sort function takes N log N time to complete all operations, hence the overall time taken by the algorithm is N log N
Auxiliary Space: O(1) // since no extra array is used so the space taken by the algorithm is constant
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...