📌  相关文章
📜  用相邻的数组元素对和重复替换相邻数组元素对后,剩余对的最大乘积

📅  最后修改于: 2021-05-17 05:36:13             🧑  作者: Mango

给定大小为N的数组arr [] ,任务是在将相邻的数组元素对用它们的和重复替换后,找到剩余对的最大乘积。
注意:将数组缩小为2。

例子:

方法:给定的问题可以通过观察解决。可以观察到,对于索引iX必须等于前i个元素的总和,即arr [1] + arr [2] + arr [3] +…+ arr [i]并且Y必须等于到其余元素的总和,即arr [i + 1] + arr [i + 2] +…+ arr [N]。现在,可以通过使用前缀总和并找到它与每个索引中其余元素之和的乘积来解决该问题。请按照以下步骤解决问题:

  • ans初始化为INT_MIN以存储所需的答案,将prefixSum初始化0以存储数组的前缀和。
  • 将数组元素的总和存储在一个变量中,例如S。
  • 使用变量i在索引[0,N – 2]的范围内遍历数组,并执行以下操作:
    • arr [i]的值添加到prefixSum
    • prefixSum的值存储在变量X中,并将(sum – prefixSum)存储在变量Y中
    • 如果(X * Y)的值大于ans ,则将ans更新为(X * Y)
  • 完成上述步骤后,输出ans的值作为结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the maximum product
// possible after repeatedly replacing
// pairs of adjacent array elements
// with their sum
void maxProduct(int arr[], int N)
{
    // Store the maximum product
    int max_product = INT_MIN;
 
    // Store the prefix sum
    int prefix_sum = 0;
 
    // Store the total sum of array
    int sum = 0;
 
    // Traverse the array to find
    // the total sum
    for (int i = 0; i < N; i++) {
        sum += arr[i];
    }
 
    // Iterate in the range [0, N-2]
    for (int i = 0; i < N - 1; i++) {
 
        // Add arr[i] to prefix_sum
        prefix_sum += arr[i];
 
        // Store the value of prefix_sum
        int X = prefix_sum;
 
        // Store the value of
        // (total sum - prefix sum)
        int Y = sum - prefix_sum;
 
        // Update the maximum product
        max_product = max(max_product,
                          X * Y);
    }
 
    // Print the answer
    cout << max_product;
}
 
// Driver Code
int main()
{
    int arr[] = { 2, 3, 5, 6, 7 };
    int N = sizeof(arr) / sizeof(arr[0]);
    maxProduct(arr, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.util.*;
 
class GFG
{
  // Function to find the maximum product
  // possible after repeatedly replacing
  // pairs of adjacent array elements
  // with their sum
  static void maxProduct(int[] arr, int N)
  {
    // Store the maximum product
    int max_product = Integer.MIN_VALUE;
 
    // Store the prefix sum
    int prefix_sum = 0;
 
    // Store the total sum of array
    int sum = 0;
 
    // Traverse the array to find
    // the total sum
    for (int i = 0; i < N; i++)
    {
      sum += arr[i];
    }
 
    // Iterate in the range [0, N-2]
    for (int i = 0; i < N - 1; i++)
    {
 
      // Add arr[i] to prefix_sum
      prefix_sum += arr[i];
 
      // Store the value of prefix_sum
      int X = prefix_sum;
 
      // Store the value of
      // (total sum - prefix sum)
      int Y = sum - prefix_sum;
 
      // Update the maximum product
      max_product = Math.max(max_product, X * Y);
    }
 
    // Print the answer
    System.out.print(max_product);
  }
 
// Driver Code
public static void main(String[] args)
{
    int[] arr = { 2, 3, 5, 6, 7 };
    int N = arr.length;
    maxProduct(arr, N);
}
}
 
// This code is contributed by sanjoy_62.


Python3
# Python program for the above approach
import sys
 
# Function to find the maximum product
# possible after repeatedly replacing
# pairs of adjacent array elements
# with their sum
def maxProduct(arr, N):
   
    # Store the maximum product
    max_product = -sys.maxsize;
 
    # Store the prefix sum
    prefix_sum = 0;
 
    # Store the total sum of array
    sum = 0;
 
    # Traverse the array to find
    # the total sum
    for i in range(N):
        sum += arr[i];
 
    # Iterate in the range [0, N-2]
    for i in range(N - 1):
       
        # Add arr[i] to prefix_sum
        prefix_sum += arr[i];
 
        # Store the value of prefix_sum
        X = prefix_sum;
 
        # Store the value of
        # (total sum - prefix sum)
        Y = sum - prefix_sum;
 
        # Update the maximum product
        max_product = max(max_product, X * Y);
 
    # Prthe answer
    print(max_product);
 
# Driver Code
if __name__ == '__main__':
    arr = [2, 3, 5, 6, 7];
    N = len(arr);
    maxProduct(arr, N);
 
# This code is contributed by shikhasingrajput


C#
// C# program for the above approach
using System;
class GFG
{
 
  // Function to find the maximum product
  // possible after repeatedly replacing
  // pairs of adjacent array elements
  // with their sum
  static void maxProduct(int[] arr, int N)
  {
    // Store the maximum product
    int max_product = Int32.MinValue;
 
    // Store the prefix sum
    int prefix_sum = 0;
 
    // Store the total sum of array
    int sum = 0;
 
    // Traverse the array to find
    // the total sum
    for (int i = 0; i < N; i++)
    {
      sum += arr[i];
    }
 
    // Iterate in the range [0, N-2]
    for (int i = 0; i < N - 1; i++)
    {
 
      // Add arr[i] to prefix_sum
      prefix_sum += arr[i];
 
      // Store the value of prefix_sum
      int X = prefix_sum;
 
      // Store the value of
      // (total sum - prefix sum)
      int Y = sum - prefix_sum;
 
      // Update the maximum product
      max_product = Math.Max(max_product, X * Y);
    }
 
    // Print the answer
    Console.WriteLine(max_product);
  } 
 
  // Driver code
  static void Main()
  {
    int[] arr = { 2, 3, 5, 6, 7 };
    int N = arr.Length;
    maxProduct(arr, N);
  }
}
 
// This code is contributed by divyeshrabadiya07.


输出:
130

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