Maximum product of the remaining pair after repeatedly replacing pairs of adjacent array elements with their sum
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 = 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++
#include <bits/stdc++.h>
using namespace std;
void maxProduct( int arr[], int N)
{
int max_product = INT_MIN;
int prefix_sum = 0;
int sum = 0;
for ( int i = 0; i < N; i++) {
sum += arr[i];
}
for ( int i = 0; i < N - 1; i++) {
prefix_sum += arr[i];
int X = prefix_sum;
int Y = sum - prefix_sum;
max_product = max(max_product,
X * Y);
}
cout << max_product;
}
int main()
{
int arr[] = { 2, 3, 5, 6, 7 };
int N = sizeof (arr) / sizeof (arr[0]);
maxProduct(arr, N);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG
{
static void maxProduct( int [] arr, int N)
{
int max_product = Integer.MIN_VALUE;
int prefix_sum = 0 ;
int sum = 0 ;
for ( int i = 0 ; i < N; i++)
{
sum += arr[i];
}
for ( int i = 0 ; i < N - 1 ; i++)
{
prefix_sum += arr[i];
int X = prefix_sum;
int Y = sum - prefix_sum;
max_product = Math.max(max_product, X * Y);
}
System.out.print(max_product);
}
public static void main(String[] args)
{
int [] arr = { 2 , 3 , 5 , 6 , 7 };
int N = arr.length;
maxProduct(arr, N);
}
}
|
Python3
import sys
def maxProduct(arr, N):
max_product = - sys.maxsize;
prefix_sum = 0 ;
sum = 0 ;
for i in range (N):
sum + = arr[i];
for i in range (N - 1 ):
prefix_sum + = arr[i];
X = prefix_sum;
Y = sum - prefix_sum;
max_product = max (max_product, X * Y);
print (max_product);
if __name__ = = '__main__' :
arr = [ 2 , 3 , 5 , 6 , 7 ];
N = len (arr);
maxProduct(arr, N);
|
C#
using System;
class GFG
{
static void maxProduct( int [] arr, int N)
{
int max_product = Int32.MinValue;
int prefix_sum = 0;
int sum = 0;
for ( int i = 0; i < N; i++)
{
sum += arr[i];
}
for ( int i = 0; i < N - 1; i++)
{
prefix_sum += arr[i];
int X = prefix_sum;
int Y = sum - prefix_sum;
max_product = Math.Max(max_product, X * Y);
}
Console.WriteLine(max_product);
}
static void Main()
{
int [] arr = { 2, 3, 5, 6, 7 };
int N = arr.Length;
maxProduct(arr, N);
}
}
|
Javascript
<script>
function maxProduct(arr , N) {
var max_product = Number.MIN_VALUE;
var prefix_sum = 0;
var sum = 0;
for (i = 0; i < N; i++) {
sum += arr[i];
}
for (i = 0; i < N - 1; i++) {
prefix_sum += arr[i];
var X = prefix_sum;
var Y = sum - prefix_sum;
max_product = Math.max(max_product, X * Y);
}
document.write(max_product);
}
var arr = [ 2, 3, 5, 6, 7 ];
var N = arr.length;
maxProduct(arr, N);
</script>
|
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).
Last Updated :
23 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...