The Wayback Machine - https://web.archive.org/web/20210211080228/https://www.geeksforgeeks.org/maximum-product-of-the-remaining-pair-after-repeatedly-replacing-pairs-of-adjacent-array-elements-with-their-sum/
Skip to content
Related Articles

Related Articles

Maximum product of the remaining pair after repeatedly replacing pairs of adjacent array elements with their sum
  • Last Updated : 10 Feb, 2021

Given an array arr[] of size N, the task is to find the maximum product of remaining pairs possible after repeatedly replacing 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[1] and arr[2] with their sum (i.e. 3 + 5 = 8) modifies arr[] to {2, 8, 6, 7}
Replacing arr[2] and arr[3] with their sum (i.e. 6 + 7 = 13) modifies arr[] to {2, 8, 13}
Replacing arr[0] and arr[1] 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[1] + arr[2] + arr[3] + … + 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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program for the above approach
  
#include <bits/stdc++.h>
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[0]);
    maxProduct(arr, N);
  
    return 0;
}

chevron_right


Output:

130

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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up
Recommended Articles
Page :