 Open in App
Not now

# Maximize array sum by replacing equal adjacent pairs by their sum and X respectively

• Difficulty Level : Hard
• Last Updated : 07 May, 2021

Given two integers N and X which denotes the size of an array arr[] and the initial value of all the array elements respectively, the task is to find the maximum sum possible from the given array after performing the following operation any number of times.

Choose any valid index i for which arr[i] = arr[i + 1] and update arr[i] = arr[i] + arr[i + 1] and arr[i + 1] = X.

Examples:

Input: N = 3, X = 5
Output: 35
Explanation:
Initially arr[] = [5, 5, 5]
Performing the given operation on i = 1, arr[] = [10, 5, 5]
Performing the given operation on i = 2, arr[] = [10, 10, 5]
Performing the given operation on i = 1, arr[] = [20, 5, 5]
Performing the given operation on i = 2, arr[] = [20, 10, 5]
No adjacent equal elements are present in the array.
Therefore, the maximum possible sum from the array is 35.

Input: N = 2, X = 3
Output:
Explanation:
Initially arr[] = [3, 3]
Performing the given operation on i = 1, arr[] = [6, 3]
No adjacent equal elements are present in the array.
Therefore, the maximum possible sum from the array is 9.

Naive Approach: The idea is to perform the given operation on every valid index in the initial array and find the maximum possible sum form all possible array rearrangements.

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

Efficient Approach: The above approach can be optimized by using the following observation:

• From the aforementioned examples, it can be observed that the value of the element at index i in the final array is given by:

X * 2(N – i – 1)

• Therefore, the sum of the final array will be equal to the sum of the series X * 2(N – i – 1) for every valid index i.
• The sum of the above series is given by:

Sum of the series = X * (2N – 1)

Therefore, simply print X * (2N – 1) as the required answer.
Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach``#include ``using` `namespace` `std;`` ` `// Function to calculate x ^ y``int` `power(``int` `x, ``int` `y)``{``    ``int` `temp;`` ` `    ``// Base Case``    ``if` `(y == 0)``        ``return` `1;`` ` `    ``// Find the value in temp``    ``temp = power(x, y / 2);`` ` `    ``// If y is even``    ``if` `(y % 2 == 0)``        ``return` `temp * temp;``    ``else``        ``return` `x * temp * temp;``}`` ` `// Function that find the maximum``// possible sum of the array``void` `maximumPossibleSum(``int` `N, ``int` `X)``{``    ` `    ``// Print the result using``    ``// the formula``    ``cout << (X * (power(2, N) - 1)) << endl;``}`` ` `// Driver code``int` `main()``{``    ``int` `N = 3, X = 5;`` ` `    ``// Function call``    ``maximumPossibleSum(N, X);``}` `// This code is contributed by rutvik_56`

## Java

 `// Java program for the above approach` `import` `java.io.*;` `class` `GFG {` `    ``// Function to calculate x ^ y``    ``static` `int` `power(``int` `x, ``int` `y)``    ``{``        ``int` `temp;` `        ``// Base Case``        ``if` `(y == ``0``)``            ``return` `1``;` `        ``// Find the value in temp``        ``temp = power(x, y / ``2``);` `        ``// If y is even``        ``if` `(y % ``2` `== ``0``)``            ``return` `temp * temp;``        ``else``            ``return` `x * temp * temp;``    ``}` `    ``// Function that find the maximum``    ``// possible sum of the array``    ``public` `static` `void``    ``maximumPossibleSum(``int` `N, ``int` `X)``    ``{``        ``// Print the result using``        ``// the formula``        ``System.out.println(``            ``X * (power(``2``, N) - ``1``));``    ``}` `    ``// Driver Code``    ``public` `static` `void``        ``main(String[] args)``    ``{``        ``int` `N = ``3``, X = ``5``;` `        ``// Function Call``        ``maximumPossibleSum(N, X);``    ``}``}`

## Python3

 `# Python3 program for the above approach` `# Function to calculate x ^ y``def` `power(x, y):` `    ``# Base Case``    ``if``(y ``=``=` `0``):``        ``return` `1` `    ``# Find the value in temp``    ``temp ``=` `power(x, y ``/``/` `2``)` `    ``# If y is even``    ``if``(y ``%` `2` `=``=` `0``):``        ``return` `temp ``*` `temp``    ``else``:``        ``return` `x ``*` `temp ``*` `temp` `# Function that find the maximum``# possible sum of the array``def` `maximumPossibleSum(N, X):` `    ``# Print the result using``    ``# the formula``    ``print``(X ``*` `(power(``2``, N) ``-` `1``))` `# Driver Code``N ``=` `3``X ``=` `5` `# Function call``maximumPossibleSum(N, X)` `# This code is contributed by Shivam Singh`

## C#

 `// C# program for``// the above approach``using` `System;``class` `GFG{` `// Function to calculate x ^ y``static` `int` `power(``int` `x, ``int` `y)``{``  ``int` `temp;` `  ``// Base Case``  ``if` `(y == 0)``    ``return` `1;` `  ``// Find the value in temp``  ``temp = power(x, y / 2);` `  ``// If y is even``  ``if` `(y % 2 == 0)``    ``return` `temp * temp;``  ``else``    ``return` `x * temp * temp;``}` `// Function that find the maximum``// possible sum of the array``public` `static` `void` `maximumPossibleSum(``int` `N,``                                      ``int` `X)``{``  ``// Print the result using``  ``// the formula``  ``Console.WriteLine(X * (power(2, N) - 1));``}` `// Driver Code``public` `static` `void` `Main(String[] args)``{``  ``int` `N = 3, X = 5;` `  ``// Function Call``  ``maximumPossibleSum(N, X);``}``}` `// This code is contributed by shikhasingrajput`

## Javascript

 ``

Output:

`35`

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

My Personal Notes arrow_drop_up