Maximize the Product of Sum by converting Array elements into given two types
Last Updated :
24 Apr, 2023
Given an array of positive integer arr[] of length N and an integer Z, (Z > arr[i] for all 0 ? i ? N – 1). Each integer of the array can be converted into the following two types:
- Keep it unchanged
- Change it to Z – arr[i].
The task is to maximize the product of the sum of these two types of elements.
Note: There should be present at least one element of each type.
Examples:
Input: N = 5, arr[] = {500, 100, 400, 560, 876}, Z = 1000
Output: 290400
Explanation: arr[] = {500, 100, 400, 560, 876}
Convert elements present at indices 0, 3 and 4 to first type = (500, 560, 876)
Convert elements present at indices 1 and 2 to second type
= (Z-arr[1], Z-arr[2]) = (1000 – 100, 1000 – 400) = (900, 600)
Sum of all first type elements = 500+560+876 = 1936
Sum of all second type elements = 900 + 600 = 1500
Product of each type sum = 1936*1500 = 290400.
Which is maximum possible for this case.
Input: N = 4, arr[] = {1, 4, 6, 3}, Z = 7
Output: 100
Explanation: Change the 1st and last element to 2nd type, i.e.,
{7-1, 7-3} = {6, 4}. The sum is (6 + 4) = 10.
Keep the 2nd and third element as it is. Their sum = (4 + 6) = 10 .
Product is 10*10 = 100. This is the maximum product possible.
Approach: The problem can be solved using Sorting based on the following idea:
The idea is to sort the arr[] in decreasing order, Calculate product of all possible combinations of the types. Obtain maximum product among all combinations.
Illustration:
Input: N = 4, arr[] = {1, 4, 6, 3}, Z = 7
After sorting arr[] in decreasing order = {6, 4, 3, 1}
Now we have 3 possible combinations for choosing all elements as first or second type:
- 1 of first type, 3 of second type
- 2 of first type, 2 of second type
- 3 of first type, 1 of second type
Let’s see the product and sum at each combination for decreasing ordered arr[]:
Choosing first element as first type and next 3 elements as second type:
- Sum of first type elements = 6
- Sum of second type elements = ((7 – 4)+(7 – 3)+(7 – 1))= 13
- Product of first and second = 6 * 13 = 78
Choosing first two elements as first type and last 2 elements as second type:
- Sum of first type elements = 6 + 4 = 10
- Sum of second type elements = (7 – 3)+(7 – 1))= 10
- Product of first and second types = 10 * 10 = 100
Choosing first three elements as first type and last element as second type:
- Sum of first type elements = 6 + 4 + 3 = 13
- Sum of second type elements = (7 – 1)) = 6
- Product of first and second types = 13 * 6 = 78
As we can clearly see that 2nd combination has maximum value of product.Therefore, output for this case is :
Maximum Product: 100
Follow the steps to solve the problem:
- Sort the input array arr[].
- Traverse from the end of the array to calculate the product for all possible combinations:
- Consider all the element till index i as first type, and the suffix elements as second type.
- Calculate the product of this combination.
- Update the maximum product accordingly.
- Print the maximum product obtained.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
long Max_Product( int n, vector< long > arr, long Z)
{
long product = INT_MIN;
sort(arr.begin(), arr.end());
long sum1 = 0;
long X = INT_MIN;
long Y = INT_MAX;
for ( int i = n - 1; i > 0; i--)
{
sum1 += arr[i];
long sum2 = 0;
for ( int j = i - 1; j >= 0; j--)
{
sum2 = sum2 + (Z - arr[j]);
}
if (sum1 * sum2 > product)
{
product = sum1 * sum2;
X = sum1;
Y = sum2;
}
}
return (product);
}
int main()
{
vector< long > arr = {500, 100, 400, 560, 876};
int N = arr.size();
long Z = 1000;
cout << (Max_Product(N, arr, Z));
}
|
Java
import java.util.*;
class GFG {
public static void main(String[] args)
{
long [] arr = { 500 , 100 , 400 , 560 , 876 };
int N = arr.length;
long Z = 1000 ;
System.out.println(Max_Product(N, arr, Z));
}
static long Max_Product( int n, long [] arr, long Z)
{
long product = Long.MIN_VALUE;
Arrays.sort(arr);
long sum1 = 0 ;
long X = Integer.MIN_VALUE;
long Y = Integer.MAX_VALUE;
for ( int i = n - 1 ; i > 0 ; i--) {
sum1 += arr[i];
long sum2 = 0 ;
for ( int j = i - 1 ; j >= 0 ; j--) {
sum2 = sum2 + (Z - arr[j]);
}
if (sum1 * sum2 > product) {
product = sum1 * sum2;
X = sum1;
Y = sum2;
}
}
return (product);
}
}
|
Python3
def Max_Product(n, arr, Z):
product = - 10000000000000000000000
arr.sort()
sum1 = 0
X = - 10000000000000000000000
Y = 100000000000000000000000
for i in range (n - 1 , 0 , - 1 ):
sum1 + = arr[i]
sum2 = 0
for j in range (i - 1 , - 1 , - 1 ):
sum2 = sum2 + (Z - arr[j])
if (sum1 * sum2 > product):
product = sum1 * sum2
X = sum1
Y = sum2
return product
if __name__ = = "__main__" :
arr = [ 500 , 100 , 400 , 560 , 876 ]
N = len (arr)
Z = 1000
print (Max_Product(N, arr, Z))
|
C#
using System;
public class GFG{
static public void Main (){
long [] arr = { 500, 100, 400, 560, 876 };
int N = arr.Length;
long Z = 1000;
Console.WriteLine(Max_Product(N, arr, Z));
}
static long Max_Product( int n, long [] arr, long Z)
{
#pragma warning disable 219
long product = Int64.MinValue;
Array.Sort(arr);
long sum1 = 0;
long X = Int32.MinValue;
long Y = Int32.MaxValue;
for ( int i = n - 1; i > 0; i--) {
sum1 += arr[i];
long sum2 = 0;
for ( int j = i - 1; j >= 0; j--) {
sum2 = sum2 + (Z - arr[j]);
}
if (sum1 * sum2 > product) {
product = sum1 * sum2;
X = sum1;
Y = sum2;
}
}
return product;
}
}
|
Javascript
<script>
function Max_Product(n, arr, Z)
{
let product = Number.MIN_VALUE;
arr.sort();
let sum1 = 0;
let X =Number.MIN_VALUE;
let Y = Number.MAX_VALUE;
for (let i = n - 1; i > 0; i--) {
sum1 += arr[i];
let sum2 = 0;
for (let j = i - 1; j >= 0; j--) {
sum2 = sum2 + (Z - arr[j]);
}
if (sum1 * sum2 > product) {
product = sum1 * sum2;
X = sum1;
Y = sum2;
}
}
return (product);
}
let arr = [ 500, 100, 400, 560, 876 ];
let N = arr.length;
let Z = 1000;
document.write(Max_Product(N, arr, Z));
</script>
|
Time Complexity: O(N*N+NlogN)
Auxiliary Space: O(1)
Similar Reads
Reduce given Array to 0 by maximising sum of chosen elements
Given an array arr[] containing N positive integers, the task is to maximize the array sum when after every sum operation all the remaining array elements decrease by 1. Note: The value of an array element does not go below 0. Examples: Input: arr[] = {6, 2, 4, 5} Output: 12 Explanation: Add 6 initi
9 min read
Remove array end element to maximize the sum of product
Given an array of N positive integers. We are allowed to remove element from either of the two ends i.e from the left side or right side of the array. Each time we remove an element, score is increased by value of element * (number of element already removed + 1). The task is to find the maximum sco
9 min read
Maximize count of array elements required to obtain given sum
Given an integer V and an array arr[] consisting of N integers, the task is to find the maximum number of array elements that can be selected from array arr[] to obtain the sum V. Each array element can be chosen any number of times. If the sum cannot be obtained, print -1. Examples: Input: arr[] =
8 min read
Maximize array sum by concatenating corresponding elements of given two arrays
Given two array A[] and B[] of the same length, the task is to find the maximum array sum that can be formed by joining the corresponding elements of the array in any order. Input: A[] = {1, 2, 3, 4, 5}, B[] = {3, 2, 1, 4, 5} Output: 183 Explanation: Numbers formed by joining the digits of the eleme
8 min read
Maximize sum by selecting X different-indexed elements from three given arrays
Given three arrays A[], B[] and C[] of size N and three positive integers X, Y, and Z, the task is to find the maximum sum possible by selecting at most N array elements such that at most X elements are from the array A[], at most Y elements from the array B[], at most Z elements are from the array
15+ min read
Maximise product of each array element with their indices by rearrangement
Given an Array of N integers, the task is to maximize the value of the product of each array element with their corresponding indices by rearranging the array. Examples: Input: N = 4, arr[] = { 3, 5, 6, 1 }Output: 31Explanation: If we arrange arr[] as { 1, 3, 5, 6 }. Sum of arr[i]*i is 1*0 + 3*1 + 5
4 min read
Maximize the product of sum and least power by choosing at most K elements from given value and power Arrays
Given two arrays arr[] and powr[] of size N and an integer K. Every element arr[i] has its respective power powr[i]. The task is to maximize the value of the given function by choosing at most K elements from the array. The function is defined as : f(N) = (arr[i1] + arr[i2] + arr[i3]+.....arr[in]) *
10 min read
Maximize sum of pairwise products generated from the given Arrays
Given three arrays arr1[], arr2[] and arr3[] of length N1, N2, and N3 respectively, the task is to find the maximum sum possible by adding the products of pairs taken from different arrays. Note: Each array element can be a part of a single pair. Examples: Input: arr1[] = {3, 5}, arr2[] = {2, 1}, ar
11 min read
Maximize product of two closest numbers of other array for every element in given array
Given arrays arr1[] of size M and arr2[] of size N having length at least 2, the task is for every element in arr1[], maximize the product of two elements in arr2[] which are closest to the element in arr1[]. The closest elements must be present on distinct indices. Example: Input: arr1 = [5, 10, 17
15 min read
Minimize the sum after choosing elements from the given three arrays
Given three arrays A[], B[] and C[] of same size N. The task is to minimize the sum after choosing N elements from these array such that at every index i an element from any one of the array A[i], B[i] or C[i] can be chosen and no two consecutive elements can be chosen from the same array.Examples:
15+ min read