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++
#include <bits/stdc++.h>
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);
}
|
Java
import java.io.*;
class GFG {
static 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;
}
public static void
maximumPossibleSum( int N, int X)
{
System.out.println(
X * (power( 2 , N) - 1 ));
}
public static void
main(String[] args)
{
int N = 3 , X = 5 ;
maximumPossibleSum(N, X);
}
}
|
Python3
def power(x, y):
if (y = = 0 ):
return 1
temp = power(x, y / / 2 )
if (y % 2 = = 0 ):
return temp * temp
else :
return x * temp * temp
def maximumPossibleSum(N, X):
print (X * (power( 2 , N) - 1 ))
N = 3
X = 5
maximumPossibleSum(N, X)
|
C#
using System;
class GFG{
static 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;
}
public static void maximumPossibleSum( int N,
int X)
{
Console.WriteLine(X * (power(2, N) - 1));
}
public static void Main(String[] args)
{
int N = 3, X = 5;
maximumPossibleSum(N, X);
}
}
|
Javascript
<script>
function power(x , y) {
var temp;
if (y == 0)
return 1;
temp = power(x, parseInt(y / 2));
if (y % 2 == 0)
return temp * temp;
else
return x * temp * temp;
}
function maximumPossibleSum(N , X)
{
document.write(X * (power(2, N) - 1));
}
var N = 3, X = 5;
maximumPossibleSum(N, X);
</script>
|
Time Complexity: O(log N), The time complexity of this code is O(log N) because the power function is called recursively with a value of y being halved at each recursive call. Therefore, the number of recursive calls required to reach the base case of y=0 is log base 2 of N.
Space Complexity: O(1)