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

ifor whicharr[i] = arr[i + 1]and updatearr[i] = arr[i] + arr[i + 1]andarr[i + 1] = X.

**Examples:**

Input:N = 3, X = 5Output:35Explanation:

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 = 3Output:9Explanation:

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(2^{N}) **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**for every valid index^{(N – i – 1)}**i**. - The sum of the above series is given by:

Sum of the series = X * (2

^{N}– 1)

Therefore, simply print **X * (2 ^{N} – 1)** as the required answer.

Below is the implementation of the above approach:

## C++

`// C++ program for the above approach` `#include <bits/stdc++.h>` `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` |

*chevron_right*

*filter_none*

## 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);` ` ` `}` `}` |

*chevron_right*

*filter_none*

## 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` |

*chevron_right*

*filter_none*

## 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` |

*chevron_right*

*filter_none*

**Output:**

35

**Time Complexity:** O(log N) **Space Complexity:** 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.

## Recommended Posts:

- Maximize modulus by replacing adjacent pairs with their modulus for any permutation of given Array
- Maximize count of equal numbers in Array of numbers upto N by replacing pairs with their sum
- Maximize sum of squares of array elements possible by replacing pairs with their Bitwise AND and Bitwise OR
- Minimize array length by repeatedly replacing pairs of unequal adjacent array elements by their sum
- Smallest array that can be obtained by replacing adjacent pairs with their products
- Count ways to generate pairs having Bitwise XOR and Bitwise AND equal to X and Y respectively
- Count of pairs in a given range with sum of their product and sum equal to their concatenated number
- Minimize length of string by replacing K pairs of distinct adjacent characters
- Highest and Smallest power of K less than and greater than equal to N respectively
- Maximize removal of adjacent array elements based on their absolute value
- Maximum sum of Array formed by replacing each element with sum of adjacent elements
- Array value by repeatedly replacing max 2 elements with their absolute difference
- Generate an Array in which count of even and odd sum sub-arrays are E and O respectively
- Count pairs from 1 to N such that their Sum is divisible by their XOR
- Last element remaining by deleting two largest elements and replacing by their absolute difference if they are unequal
- Minimize swaps required to maximize the count of elements replacing a greater element in an Array
- Maximize length of increasing subsequence possible by replacing array element by nearest primes
- Possible values of Q such that, for any value of R, their product is equal to X times their sum
- Subsequence pair from given Array having all unique and all same elements respectively
- Count of pairs from Array with sum equal to twice their bitwise AND

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.