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
= (Zarr[1], Zarr[2]) = (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.,
{71, 73} = {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

Time Complexity: O(N^{2})
Auxiliary House: O(1)