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++
// C++ code to implement the above approach
#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]);
// Function call
cout << maxValue(arr, N) << endl;
return 0;
}
// This code is contributed by lokeshmvs21.
Java
// Java code to implement the approach
import java.io.*;
import java.util.*;
class GFG {
// Function to find maximum value of expression
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);
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 5, 2, 3 };
int N = arr.length;
// Function call
System.out.println(maxValue(arr, N));
}
}
Python3
# Python code to implement the approach
# Function to find maximum value of expression
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)
# Driver Code
if __name__ == '__main__':
arr = [5, 2, 3]
N = len(arr)
# Function call
print(maxValue(arr, N))
C#
// C# code to implement the approach
using System;
class GFG {
// Function to find maximum value of expression
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);
}
// Driver Code
public static void Main()
{
int[] arr = { 5, 2, 3 };
int N = arr.Length;
// Function call
Console.Write(maxValue(arr, N));
}
}
// This code is contributed by code_hunt.
JavaScript
// Javascript code to implement the approach
<script>
// Function to find maximum value of expression
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);
}
// Driver code
let arr = [ 5, 2, 3 ];
let N = arr.length;
// Function call
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
Similar Reads
Maximize value of a pair from two given arrays based on given conditions Given two arrays A[] and B[] consisting of N integers and an integer K, the task is to find the maximum value of B[i] + B[j] + abs(A[i] - A[j]) by choosing any pair (i, j) such that abs(A[i] - A[j]) ? K. Examples: Input: A[] = {5, 6, 9, 10}, B[] = {3, 0, 10, -10}, K = 1Output: 4Explanation:Only two
15+ min read
Maximum the value of a given expression for any pair of coordinates on a 2D plane Given a sorted 2D array arr[][2] of size N such that (arr[i][0], arr[i][1]) represents the coordinates of ith point in the cartesian plane and an integer K, the task is to find the maximum value of the expression (|arr[i][0] - arr[j][0]| + arr[i][1] + arr[j][1]) such that |arr[i][0] - arr[j][0]| ? K
7 min read
Maximize the value of expression [i.j - K.(Ai | Aj)] over all pairs (i, j) in given Array Given an array A[] of length N and an integer K, the task is to maximize the value of expression [i.j - K.(Ai | Aj)] over all pairs (i, j) in given Array, where (1 ⤠i < j ⤠N) and | denotes Bitwise OR operator. Examples: Input: A[] = {5, 20, 1, 0, 8, 11}, K = 10Output: 2Explanation: The maximum
6 min read
Maximise sum of product of pairs by choosing subsequence of same length from given Arrays Given two integer arrays A[] and B[] of different length N and M respectively, the task is to choose any subsequence of same length from each array such that sum of product of pairs at corresponding indices in the subsequence is maximised. Example: Input: A = {4, -1, -3, 3}, B = {-4, 0, 5}Output: 27
15+ min read
Find a pair from the given array with maximum nCr value Given an array arr[] of n positive integers. The task is to find elements arr[i] and arr[j] from the array such that arr[i]Carr[j] is maximum possible. In case of more than 1 valid pairs, print any one of them.Examples: Input: arr[] = {3, 1, 2} Output: 3 2 3C1 = 3 3C2 = 3 2C1 = 2 (3, 1) and (3, 2) a
14 min read