Sunday, September 25, 2022
HomeSoftware DevelopmentMaximize the Product of Sum by changing Array components into given two...

# Maximize the Product of Sum by changing Array components into given two sorts

Given an array of constructive integer arr[] of size N and an integer Z, (Z > arr[i] for all 0 ≤ i ≤ N – 1). Every integer of the array could be transformed into the next two sorts:

• Hold it unchanged
• Change it to Z – arr[i].

The duty is to maximise the product of the sum of those two sorts of components.

Word: There must be current at the very least one factor of every sort.

Examples:

Enter: N = 5, arr[] = {500, 100, 400, 560, 876}, Z = 1000
Output: 290400
Rationalization: arr[] = {500, 100, 400, 560, 876}
Convert components current at indices 0, 3 and 4 to first sort =  (500, 560, 876)
Convert components current at indices 1 and a pair of to second sort
= (Z-arr,  Z-arr) = (1000 – 100, 1000 – 400) = (900, 600)
Sum of all first sort components = 500+560+876 = 1936
Sum of all second sort components = 900 + 600 = 1500
Product of every sort sum = 1936*1500 = 290400
Which is most attainable for this case.

Enter: N = 4, arr[] = {1, 4, 6, 3}, Z = 7
Output: 100
Rationalization: Change the first and final factor to 2nd sort, i.e.,
{7-1, 7-3} = {6, 4}. The sum is (6 + 4) = 10.
Hold the 2nd and third factor as it’s. Their sum = (4 + 6) = 10 .
Product is 10*10 = 100. That is the utmost product attainable.

Strategy: The issue could be solved utilizing Sorting primarily based on the next thought:

The thought is to kind the arr[] in reducing order, Calculate product of all attainable combos of the categories. Acquire most product amongst all combos.

Illustration:

Enter: N = 4, arr[] = {1, 4, 6, 3}, Z = 7

After sorting arr[] in reducing order = {6, 4, 3, 1}

Now we have now 3 attainable combos for selecting all components as first or second sort:

• 1 of first sort, 3 of second sort
• 2 of first sort, 2 of second sort
• 3 of first sort, 1 of second sort

Let’s see the product and sum at every mixture for reducing ordered arr[]:

Selecting first factor as first sort and subsequent 3 components as second sort:

• Sum of first sort components = 6
• Sum of second sort components = ((7 – 4)+(7 – 3)+(7 – 1))= 13
• Product of first and second = 6 * 13 = 78

Selecting first two components as first sort and final 2 components as second sort:

• Sum of first sort components = 6 + 4 = 10
• Sum of second sort components = (7 – 3)+(7 – 1))= 10
• Product of first and second sorts = 10 * 10 = 100

Selecting first three components as first sort and final factor as second sort:

• Sum of first sort components = 6 + 4 + 3 = 13
• Sum of second sort components = (7 – 1)) = 6
• Product of first and second sorts = 13 * 6 = 78

As we will clearly see that 2nd mixture has most worth of product.Due to this fact, output for this case is :

Most Product: 100

Observe the steps to resolve the issue:

• Type the enter array arr[].
• Traverse from the tip of the array to calculate the product for all attainable combos:
• Take into account all of the factor until index i as first sort, and the suffix components as second sort.
• Calculate the product of this mixture.
• Replace the utmost product accordingly.
• Print the utmost product obtained.

Beneath is the implementation of the above method.

## Java

 ` `  `import` `java.util.*;` ` `  `class` `GFG {` ` `  `    ` `    ``public` `static` `void` `important(String[] args)` `    ``{` `        ``lengthy``[] arr = { ``500``, ``100``, ``400``, ``560``, ``876` `};` `        ``int` `N = arr.size;` `        ``lengthy` `Z = ``1000``;` ` `  `        ` `        ``System.out.println(Max_Product(N, arr, Z));` `    ``}` ` `  `    ` `    ` `    ``static` `lengthy` `Max_Product(``int` `n, ``lengthy``[] arr, ``lengthy` `Z)` `    ``{` `        ` `        ``lengthy` `product = Lengthy.MIN_VALUE;` ` `  `        ` `        ``Arrays.kind(arr);` ` `  `        ` `        ``lengthy` `sum1 = ``0``;` ` `  `        ` `        ` `        ``lengthy` `X = Integer.MIN_VALUE;` ` `  `        ` `        ` `        ``lengthy` `Y = Integer.MAX_VALUE;` ` `  `        ` `        ` `        ``for` `(``int` `i = n - ``1``; i > ``0``; i--) {` `            ``sum1 += arr[i];` `            ``lengthy` `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);` `    ``}` `}`

Time Complexity: O(N2)
Auxiliary House: O(1)

RELATED ARTICLES