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: 9
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 <bits/stdc++.h>using namespace std; // Function to calculate x ^ yint 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 arrayvoid maximumPossibleSum(int N, int X){ // Print the result using // the formula cout << (X * (power(2, N) - 1)) << endl;} // Driver codeint main(){ int N = 3, X = 5; // Function call maximumPossibleSum(N, X);}// This code is contributed by rutvik_56 |
Java
// Java program for the above approachimport 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 arraydef maximumPossibleSum(N, X): # Print the result using # the formula print(X * (power(2, N) - 1))# Driver CodeN = 3X = 5# Function callmaximumPossibleSum(N, X)# This code is contributed by Shivam Singh |
C#
// C# program for // the above approachusing System;class GFG{// Function to calculate x ^ ystatic 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 arraypublic static void maximumPossibleSum(int N, int X){ // Print the result using // the formula Console.WriteLine(X * (power(2, N) - 1));}// Driver Codepublic static void Main(String[] args){ int N = 3, X = 5; // Function Call maximumPossibleSum(N, X);}}// This code is contributed by shikhasingrajput |
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:
- Minimize array sum by replacing greater and smaller elements of pairs by half and double of their values respectively atmost K times
- 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
- Minimize array length by repeatedly replacing pairs of unequal adjacent array elements by their sum
- Maximize sum of squares of array elements possible by replacing pairs with their Bitwise AND and Bitwise OR
- Smallest array that can be obtained by replacing adjacent pairs with their products
- Reduce array to a single element by repeatedly replacing adjacent unequal pairs with their maximum
- 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 remaining array element by repeatedly replacing pairs by half of one more than their sum
- Maximize product of array by replacing array elements with its sum or product with element from another array
- 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
- 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
- Array value by repeatedly replacing max 2 elements with their absolute difference
- Possible values of Q such that, for any value of R, their product is equal to X times their sum
- Last element remaining by deleting two largest elements and replacing by their absolute difference if they are unequal
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.

