 Open in App
Not now

# Maximum product of the remaining pair after repeatedly replacing pairs of adjacent array elements with their sum

• Last Updated : 08 Oct, 2021

Given an array arr[] of size N, the task is to find the maximum product of remaining pairs possible after repeatedly replacing a pair of adjacent array elements with their sum.

Note: Reduce the array to a size of 2.

Examples:

Input: arr[] = {2, 3, 5, 6, 7}
Output: 130
Explanation:
Replacing arr and arr with their sum (i.e. 3 + 5 = 8) modifies arr[] to {2, 8, 6, 7}
Replacing arr and arr with their sum (i.e. 6 + 7 = 13) modifies arr[] to {2, 8, 13}
Replacing arr and arr with their sum (2 + 8 = 10) modifies arr[] to {10, 13}
Maximum Product of the remaining pair = 10 * 13 = 130

Input: arr[] = {5, 6}
Output: 30

Approach: The given problem can be solved by observation. It can be observed that for an index i, X must be equal to the sum of first i elements, i.e., arr + arr + arr + … + arr[i] and Y must be equal to the sum of rest of the elements, i.e., arr[i + 1] + arr[i + 2] +…+ arr[N]. Now, the problem can be solved by using the prefix sum and finding the product of it with the sum of the rest of the elements at each index. Follow the steps below to solve the problem:

• Initialize ans as INT_MIN to store the required answer and prefixSum as 0 to store the prefix sum of the array.
• Store the total sum of the array elements in a variable, say S.
• Traverse the array over the range of indices [0, N – 2] using the variable i and perform the following operations:
• Add the value of arr[i] to prefixSum.
• Store the value of prefixSum in a variable X and store (sum – prefixSum) in a variable Y.
• If the value of (X * Y) is greater than ans, then update ans as (X * Y).
• After completing the above steps, print the value of ans as the result.

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach` `#include ``using` `namespace` `std;` `// Function to find the maximum product``// possible after repeatedly replacing``// pairs of adjacent array elements``// with their sum``void` `maxProduct(``int` `arr[], ``int` `N)``{``    ``// Store the maximum product``    ``int` `max_product = INT_MIN;` `    ``// Store the prefix sum``    ``int` `prefix_sum = 0;` `    ``// Store the total sum of array``    ``int` `sum = 0;` `    ``// Traverse the array to find``    ``// the total sum``    ``for` `(``int` `i = 0; i < N; i++) {``        ``sum += arr[i];``    ``}` `    ``// Iterate in the range [0, N-2]``    ``for` `(``int` `i = 0; i < N - 1; i++) {` `        ``// Add arr[i] to prefix_sum``        ``prefix_sum += arr[i];` `        ``// Store the value of prefix_sum``        ``int` `X = prefix_sum;` `        ``// Store the value of``        ``// (total sum - prefix sum)``        ``int` `Y = sum - prefix_sum;` `        ``// Update the maximum product``        ``max_product = max(max_product,``                          ``X * Y);``    ``}` `    ``// Print the answer``    ``cout << max_product;``}` `// Driver Code``int` `main()``{``    ``int` `arr[] = { 2, 3, 5, 6, 7 };``    ``int` `N = ``sizeof``(arr) / ``sizeof``(arr);``    ``maxProduct(arr, N);` `    ``return` `0;``}`

## Java

 `// Java program for the above approach``import` `java.io.*;``import` `java.util.*;` `class` `GFG``{``  ``// Function to find the maximum product``  ``// possible after repeatedly replacing``  ``// pairs of adjacent array elements``  ``// with their sum``  ``static` `void` `maxProduct(``int``[] arr, ``int` `N)``  ``{``    ``// Store the maximum product``    ``int` `max_product = Integer.MIN_VALUE;` `    ``// Store the prefix sum``    ``int` `prefix_sum = ``0``;` `    ``// Store the total sum of array``    ``int` `sum = ``0``;` `    ``// Traverse the array to find``    ``// the total sum``    ``for` `(``int` `i = ``0``; i < N; i++)``    ``{``      ``sum += arr[i];``    ``}` `    ``// Iterate in the range [0, N-2]``    ``for` `(``int` `i = ``0``; i < N - ``1``; i++)``    ``{` `      ``// Add arr[i] to prefix_sum``      ``prefix_sum += arr[i];` `      ``// Store the value of prefix_sum``      ``int` `X = prefix_sum;` `      ``// Store the value of``      ``// (total sum - prefix sum)``      ``int` `Y = sum - prefix_sum;` `      ``// Update the maximum product``      ``max_product = Math.max(max_product, X * Y);``    ``}` `    ``// Print the answer``    ``System.out.print(max_product);``  ``}` `// Driver Code``public` `static` `void` `main(String[] args)``{``    ``int``[] arr = { ``2``, ``3``, ``5``, ``6``, ``7` `};``    ``int` `N = arr.length;``    ``maxProduct(arr, N);``}``}` `// This code is contributed by sanjoy_62.`

## Python3

 `# Python program for the above approach``import` `sys` `# Function to find the maximum product``# possible after repeatedly replacing``# pairs of adjacent array elements``# with their sum``def` `maxProduct(arr, N):``  ` `    ``# Store the maximum product``    ``max_product ``=` `-``sys.maxsize;` `    ``# Store the prefix sum``    ``prefix_sum ``=` `0``;` `    ``# Store the total sum of array``    ``sum` `=` `0``;` `    ``# Traverse the array to find``    ``# the total sum``    ``for` `i ``in` `range``(N):``        ``sum` `+``=` `arr[i];` `    ``# Iterate in the range [0, N-2]``    ``for` `i ``in` `range``(N ``-` `1``):``      ` `        ``# Add arr[i] to prefix_sum``        ``prefix_sum ``+``=` `arr[i];` `        ``# Store the value of prefix_sum``        ``X ``=` `prefix_sum;` `        ``# Store the value of``        ``# (total sum - prefix sum)``        ``Y ``=` `sum` `-` `prefix_sum;` `        ``# Update the maximum product``        ``max_product ``=` `max``(max_product, X ``*` `Y);` `    ``# Print the answer``    ``print``(max_product);` `# Driver Code``if` `__name__ ``=``=` `'__main__'``:``    ``arr ``=` `[``2``, ``3``, ``5``, ``6``, ``7``];``    ``N ``=` `len``(arr);``    ``maxProduct(arr, N);` `# This code is contributed by shikhasingrajput`

## C#

 `// C# program for the above approach``using` `System;``class` `GFG``{` `  ``// Function to find the maximum product``  ``// possible after repeatedly replacing``  ``// pairs of adjacent array elements``  ``// with their sum``  ``static` `void` `maxProduct(``int``[] arr, ``int` `N)``  ``{``    ``// Store the maximum product``    ``int` `max_product = Int32.MinValue;` `    ``// Store the prefix sum``    ``int` `prefix_sum = 0;` `    ``// Store the total sum of array``    ``int` `sum = 0;` `    ``// Traverse the array to find``    ``// the total sum``    ``for` `(``int` `i = 0; i < N; i++)``    ``{``      ``sum += arr[i];``    ``}` `    ``// Iterate in the range [0, N-2]``    ``for` `(``int` `i = 0; i < N - 1; i++)``    ``{` `      ``// Add arr[i] to prefix_sum``      ``prefix_sum += arr[i];` `      ``// Store the value of prefix_sum``      ``int` `X = prefix_sum;` `      ``// Store the value of``      ``// (total sum - prefix sum)``      ``int` `Y = sum - prefix_sum;` `      ``// Update the maximum product``      ``max_product = Math.Max(max_product, X * Y);``    ``}` `    ``// Print the answer``    ``Console.WriteLine(max_product);``  ``} ` `  ``// Driver code``  ``static` `void` `Main()``  ``{``    ``int``[] arr = { 2, 3, 5, 6, 7 };``    ``int` `N = arr.Length;``    ``maxProduct(arr, N);``  ``}``}` `// This code is contributed by divyeshrabadiya07.`

## Javascript

 ``

Output:

`130`

Time Complexity: O(N)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up