Maximize array sum by replacing equal adjacent pairs by their sum and X respectively | C ++ program for the above approach ; Function to calculate x ^ y ; Base Case ; Find the value in temp ; If y is even ; Function that find the maximum possible sum of the array ; Print the result using the formula ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int power ( int x , int y ) { int temp ; if ( y == 0 ) return 1 ; temp = power ( x , y / 2 ) ; if ( y % 2 == 0 ) return temp * temp ; else return x * temp * temp ; } void maximumPossibleSum ( int N , int X ) { cout << ( X * ( power ( 2 , N ) - 1 ) ) << endl ; } int main ( ) { int N = 3 , X = 5 ; maximumPossibleSum ( N , X ) ; }