给定两个整数N和X ,分别表示数组arr[]的大小和所有数组元素的初始值,任务是在执行以下操作任意次数后从给定数组中找到可能的最大和。
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.
例子:
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.
朴素的方法:这个想法是对初始数组中的每个有效索引执行给定的操作,并从所有可能的数组重新排列中找到最大可能的总和。
时间复杂度: O(2 N )
辅助空间: O(1)
高效的方法:可以通过使用以下观察来优化上述方法:
- 从上述例子可以看出,最终数组中索引i处元素的值由下式给出:
X * 2(N – i – 1)
- 因此,对于每个有效索引i ,最终数组的总和将等于系列X * 2 (N – i – 1)的总和。
- 上述系列的总和由下式给出:
Sum of the series = X * (2N – 1)
因此,只需打印X * (2 N – 1)作为所需答案。
下面是上述方法的实现:
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
35
时间复杂度: O(log N)
空间复杂度: O(1)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。