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

📅  最后修改于: 2021-05-17 23:53:12             🧑  作者: 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


输出:
35





时间复杂度: O(log N)
空间复杂度: O(1)