📌  相关文章
📜  通过分别用它们的总和和 X 替换相等的相邻对来最大化数组总和

📅  最后修改于: 2021-09-03 13:43:19             🧑  作者: Mango

给定两个整数NX ,分别表示数组arr[]的大小和所有数组元素的初始值,任务是在执行以下操作任意次数后从给定数组中找到可能的最大和。

例子:

朴素的方法:这个想法是对初始数组中的每个有效索引执行给定的操作,并从所有可能的数组重新排列中找到最大可能的总和。

时间复杂度: O(2 N )
辅助空间: O(1)

高效的方法:可以通过使用以下观察来优化上述方法:

  • 从上述例子可以看出,最终数组中索引i处元素的值由下式给出:
  • 因此,对于每个有效索引i ,最终数组的总和将等于系列X * 2 (N – i – 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)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live