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[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 = 130Input: 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++
// 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; } |
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
<script> // javascript program for the above approach // Function to find the maximum product // possible after repeatedly replacing // pairs of adjacent array elements // with their sum function maxProduct(arr , N) { // Store the maximum product var max_product = Number.MIN_VALUE; // Store the prefix sum var prefix_sum = 0; // Store the total sum of array var sum = 0; // Traverse the array to find // the total sum for (i = 0; i < N; i++) { sum += arr[i]; } // Iterate in the range [0, N-2] for (i = 0; i < N - 1; i++) { // Add arr[i] to prefix_sum prefix_sum += arr[i]; // Store the value of prefix_sum var X = prefix_sum; // Store the value of // (total sum - prefix sum) var Y = sum - prefix_sum; // Update the maximum product max_product = Math.max(max_product, X * Y); } // Print the answer document.write(max_product); } // Driver Code var arr = [ 2, 3, 5, 6, 7 ]; var N = arr.length; maxProduct(arr, N); // This code contributed by umadevi9616 </script> |
130
Time Complexity: O(N), The time complexity of the given program is O(N) because the program has two loops, one for calculating the total sum of the array, which takes O(N) time and the other one for iterating over the array and finding the maximum product by calculating the prefix sum and suffix sum, which also takes O(N) time. Therefore, the total time complexity of the program is O(N + N) = O(N).
Auxiliary Space: O(1), The space complexity of the given program is O(1) because the program uses only a constant amount of extra space to store a few variables like max_product, prefix_sum, sum, X, and Y. The amount of extra space used by the program remains constant irrespective of the input size, so it is O(1).
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!