📜  数组的两个连续子数组之和的最大乘积

📅  最后修改于: 2022-05-13 01:57:51.561000             🧑  作者: Mango

数组的两个连续子数组之和的最大乘积

给定一个包含N个正整数的数组arr[] ,任务是将数组拆分为两个连续的子数组,使得两个连续子数组之和的乘积最大。

例子:

朴素的方法:一种简单的方法是逐一考虑子数组的所有可能分区,并计算子数组之和的最大乘积。
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to return the maximum
// product of sum for any partition
int maxProdSum(int arr[], int n)
{
    int leftArraySum = 0, maxProduct = 0;
 
    // Traversing the array
    for (int i = 0; i < n; i++) {
 
        // Compute left array sum
        leftArraySum += arr[i];
 
        // Compute right array sum
        int rightArraySum = 0;
        for (int j = i + 1; j < n; j++) {
            rightArraySum += arr[j];
        }
 
        // Multiplying left and right subarray sum
        int k = leftArraySum * rightArraySum;
 
        // Checking for the maximum product
        // of sum of left and right subarray
        if (k > maxProduct) {
            maxProduct = k;
        }
    }
 
    // Printing the maximum product
    return maxProduct;
}
 
// Driver code
int main()
{
    int arr[] = { 4, 10, 1, 7, 2, 9 };
    int n = sizeof(arr) / sizeof(int);
 
    cout << maxProdSum(arr, n);
 
    return 0;
}


Java
// Java implementation of the approach
class GFG
{
 
// Function to return the maximum
// product of sum for any partition
static int maxProdSum(int arr[], int n)
{
    int leftArraySum = 0, maxProduct = 0;
 
    // Traversing the array
    for (int i = 0; i < n; i++)
    {
 
        // Compute left array sum
        leftArraySum += arr[i];
 
        // Compute right array sum
        int rightArraySum = 0;
        for (int j = i + 1; j < n; j++)
        {
            rightArraySum += arr[j];
        }
 
        // Multiplying left and right subarray sum
        int k = leftArraySum * rightArraySum;
 
        // Checking for the maximum product
        // of sum of left and right subarray
        if (k > maxProduct)
        {
            maxProduct = k;
        }
    }
 
    // Printing the maximum product
    return maxProduct;
}
 
// Driver code
public static void main(String[] args)
{
    int arr[] = { 4, 10, 1, 7, 2, 9 };
    int n = arr.length;
 
    System.out.print(maxProdSum(arr, n));
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 implementation of the approach
 
# Function to return the maximum
# product of sum for any partition
def maxProdSum(arr, n):
    leftArraySum = 0;
    maxProduct = 0;
 
    # Traversing the array
    for i in range(n):
 
        # Compute left array sum
        leftArraySum += arr[i];
 
        # Compute right array sum
        rightArraySum = 0;
        for j in range(i + 1, n):
            rightArraySum += arr[j];
         
        # Multiplying left and right subarray sum
        k = leftArraySum * rightArraySum;
 
        # Checking for the maximum product
        # of sum of left and right subarray
        if (k > maxProduct):
            maxProduct = k;
         
    # Printing the maximum product
    return maxProduct;
 
# Driver code
if __name__ == '__main__':
    arr = [ 4, 10, 1, 7, 2, 9 ];
    n = len(arr);
 
    print(maxProdSum(arr, n));
 
# This code is contributed by Rajput-Ji


C#
// C# implementation of the approach
using System;
 
class GFG
{
 
// Function to return the maximum
// product of sum for any partition
static int maxProdSum(int []arr, int n)
{
    int leftArraySum = 0, maxProduct = 0;
 
    // Traversing the array
    for (int i = 0; i < n; i++)
    {
 
        // Compute left array sum
        leftArraySum += arr[i];
 
        // Compute right array sum
        int rightArraySum = 0;
        for (int j = i + 1; j < n; j++)
        {
            rightArraySum += arr[j];
        }
 
        // Multiplying left and right subarray sum
        int k = leftArraySum * rightArraySum;
 
        // Checking for the maximum product
        // of sum of left and right subarray
        if (k > maxProduct)
        {
            maxProduct = k;
        }
    }
 
    // Printing the maximum product
    return maxProduct;
}
 
// Driver code
public static void Main(String[] args)
{
    int []arr = { 4, 10, 1, 7, 2, 9 };
    int n = arr.Length;
 
    Console.Write(maxProdSum(arr, n));
}
}
 
// This code is contributed by PrinciRaj1992


Javascript


CPP
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to return the maximum
// product of sum for any partition
int maxProdSum(int arr[], int n)
{
    int prefixArraySum[n], maxProduct = 0;
 
    // Initialise prefixArraySum[0]
    // with arr[0] element
    prefixArraySum[0] = arr[0];
 
    // Traverse array elements
    // to compute prefix array sum
    for (int i = 1; i < n; i++) {
        prefixArraySum[i] = prefixArraySum[i - 1]
                            + arr[i];
    }
 
    for (int i = 0; i < n - 1; i++) {
        // Compute left and right array sum
        int leftArraySum = prefixArraySum[i];
        int rightArraySum = prefixArraySum[n - 1]
                            - prefixArraySum[i];
 
        // Multiplying left and right subarray sum
        int k = leftArraySum * rightArraySum;
 
        // Checking for maximum product of
        // the sum of left and right subarray
        if (k > maxProduct) {
            maxProduct = k;
        }
    }
 
    // Printing the maximum value
    return maxProduct;
}
 
// Driver code
int main()
{
    int arr[] = { 4, 10, 1, 7, 2, 9 };
    int n = sizeof(arr) / sizeof(int);
 
    cout << maxProdSum(arr, n);
 
    return 0;
}


Java
// Java implementation of the approach
class GFG
{
 
// Function to return the maximum
// product of sum for any partition
static int maxProdSum(int arr[], int n)
{
    int []prefixArraySum = new int[n];
    int maxProduct = 0;
 
    // Initialise prefixArraySum[0]
    // with arr[0] element
    prefixArraySum[0] = arr[0];
 
    // Traverse array elements
    // to compute prefix array sum
    for (int i = 1; i < n; i++)
    {
        prefixArraySum[i] = prefixArraySum[i - 1]
                            + arr[i];
    }
 
    for (int i = 0; i < n - 1; i++)
    {
        // Compute left and right array sum
        int leftArraySum = prefixArraySum[i];
        int rightArraySum = prefixArraySum[n - 1]
                            - prefixArraySum[i];
 
        // Multiplying left and right subarray sum
        int k = leftArraySum * rightArraySum;
 
        // Checking for maximum product of
        // the sum of left and right subarray
        if (k > maxProduct)
        {
            maxProduct = k;
        }
    }
 
    // Printing the maximum value
    return maxProduct;
}
 
// Driver code
public static void main(String[] args)
{
    int arr[] = { 4, 10, 1, 7, 2, 9 };
    int n = arr.length;
 
    System.out.print(maxProdSum(arr, n));
}
}
 
// This code is contributed by PrinciRaj1992


Python3
# Python implementation of the approach
 
# Function to return the maximum
# product of sum for any partition
def maxProdSum(arr, n):
     
    prefixArraySum = [0] * n
    maxProduct = 0
     
    # Initialise prefixArraySum[0]
    # with arr[0] element
    prefixArraySum[0] = arr[0]
     
    # Traverse array elements
    # to compute prefix array sum
    for i in range(1, n):
        prefixArraySum[i] = prefixArraySum[i - 1] + arr[i]
     
    for i in range(n - 1):
         
        # Compute left and right array sum
        leftArraySum = prefixArraySum[i]
        rightArraySum = prefixArraySum[n - 1] - \
                        prefixArraySum[i]
         
        # Multiplying left and right subarray sum
        k = leftArraySum * rightArraySum
         
        # Checking for maximum product of
        # the sum of left and right subarray
        if (k > maxProduct):
            maxProduct = k
     
    # Printing the maximum value
    return maxProduct
 
# Driver code
arr = [4, 10, 1, 7, 2, 9]
n = len(arr)
print(maxProdSum(arr, n))
 
# This code is contributed by SHUBHAMSINGH10


C#
// C# implementation of the approach
using System;
 
class GFG
{
 
// Function to return the maximum
// product of sum for any partition
static int maxProdSum(int []arr, int n)
{
    int []prefixArraySum = new int[n];
    int maxProduct = 0;
 
    // Initialise prefixArraySum[0]
    // with arr[0] element
    prefixArraySum[0] = arr[0];
 
    // Traverse array elements
    // to compute prefix array sum
    for (int i = 1; i < n; i++)
    {
        prefixArraySum[i] = prefixArraySum[i - 1]
                            + arr[i];
    }
 
    for (int i = 0; i < n - 1; i++)
    {
        // Compute left and right array sum
        int leftArraySum = prefixArraySum[i];
        int rightArraySum = prefixArraySum[n - 1]
                            - prefixArraySum[i];
 
        // Multiplying left and right subarray sum
        int k = leftArraySum * rightArraySum;
 
        // Checking for maximum product of
        // the sum of left and right subarray
        if (k > maxProduct)
        {
            maxProduct = k;
        }
    }
 
    // Printing the maximum value
    return maxProduct;
}
 
// Driver code
public static void Main(String[] args)
{
    int []arr = { 4, 10, 1, 7, 2, 9 };
    int n = arr.Length;
 
    Console.Write(maxProdSum(arr, n));
}
}
 
// This code is contributed by Rajput-Ji


Javascript


输出:
270

时间复杂度: O(N 2 )

辅助空间: O(1)

有效的方法:更好的方法是使用概念前缀数组和,这有助于计算两个连续子数组的总和。

  • 可以计算元素的前缀和。
  • 左边的数组总和是第 i位置的值。
  • 右边的数组总和是最后一个位置的值——第 i位置。
  • 现在计算k ,其中一些左右子数组的乘积。
  • 查找并打印这些数组乘积的最大值。

下面是上述方法的实现:

CPP

// C++ implementation of the approach
#include 
using namespace std;
 
// Function to return the maximum
// product of sum for any partition
int maxProdSum(int arr[], int n)
{
    int prefixArraySum[n], maxProduct = 0;
 
    // Initialise prefixArraySum[0]
    // with arr[0] element
    prefixArraySum[0] = arr[0];
 
    // Traverse array elements
    // to compute prefix array sum
    for (int i = 1; i < n; i++) {
        prefixArraySum[i] = prefixArraySum[i - 1]
                            + arr[i];
    }
 
    for (int i = 0; i < n - 1; i++) {
        // Compute left and right array sum
        int leftArraySum = prefixArraySum[i];
        int rightArraySum = prefixArraySum[n - 1]
                            - prefixArraySum[i];
 
        // Multiplying left and right subarray sum
        int k = leftArraySum * rightArraySum;
 
        // Checking for maximum product of
        // the sum of left and right subarray
        if (k > maxProduct) {
            maxProduct = k;
        }
    }
 
    // Printing the maximum value
    return maxProduct;
}
 
// Driver code
int main()
{
    int arr[] = { 4, 10, 1, 7, 2, 9 };
    int n = sizeof(arr) / sizeof(int);
 
    cout << maxProdSum(arr, n);
 
    return 0;
}

Java

// Java implementation of the approach
class GFG
{
 
// Function to return the maximum
// product of sum for any partition
static int maxProdSum(int arr[], int n)
{
    int []prefixArraySum = new int[n];
    int maxProduct = 0;
 
    // Initialise prefixArraySum[0]
    // with arr[0] element
    prefixArraySum[0] = arr[0];
 
    // Traverse array elements
    // to compute prefix array sum
    for (int i = 1; i < n; i++)
    {
        prefixArraySum[i] = prefixArraySum[i - 1]
                            + arr[i];
    }
 
    for (int i = 0; i < n - 1; i++)
    {
        // Compute left and right array sum
        int leftArraySum = prefixArraySum[i];
        int rightArraySum = prefixArraySum[n - 1]
                            - prefixArraySum[i];
 
        // Multiplying left and right subarray sum
        int k = leftArraySum * rightArraySum;
 
        // Checking for maximum product of
        // the sum of left and right subarray
        if (k > maxProduct)
        {
            maxProduct = k;
        }
    }
 
    // Printing the maximum value
    return maxProduct;
}
 
// Driver code
public static void main(String[] args)
{
    int arr[] = { 4, 10, 1, 7, 2, 9 };
    int n = arr.length;
 
    System.out.print(maxProdSum(arr, n));
}
}
 
// This code is contributed by PrinciRaj1992

Python3

# Python implementation of the approach
 
# Function to return the maximum
# product of sum for any partition
def maxProdSum(arr, n):
     
    prefixArraySum = [0] * n
    maxProduct = 0
     
    # Initialise prefixArraySum[0]
    # with arr[0] element
    prefixArraySum[0] = arr[0]
     
    # Traverse array elements
    # to compute prefix array sum
    for i in range(1, n):
        prefixArraySum[i] = prefixArraySum[i - 1] + arr[i]
     
    for i in range(n - 1):
         
        # Compute left and right array sum
        leftArraySum = prefixArraySum[i]
        rightArraySum = prefixArraySum[n - 1] - \
                        prefixArraySum[i]
         
        # Multiplying left and right subarray sum
        k = leftArraySum * rightArraySum
         
        # Checking for maximum product of
        # the sum of left and right subarray
        if (k > maxProduct):
            maxProduct = k
     
    # Printing the maximum value
    return maxProduct
 
# Driver code
arr = [4, 10, 1, 7, 2, 9]
n = len(arr)
print(maxProdSum(arr, n))
 
# This code is contributed by SHUBHAMSINGH10

C#

// C# implementation of the approach
using System;
 
class GFG
{
 
// Function to return the maximum
// product of sum for any partition
static int maxProdSum(int []arr, int n)
{
    int []prefixArraySum = new int[n];
    int maxProduct = 0;
 
    // Initialise prefixArraySum[0]
    // with arr[0] element
    prefixArraySum[0] = arr[0];
 
    // Traverse array elements
    // to compute prefix array sum
    for (int i = 1; i < n; i++)
    {
        prefixArraySum[i] = prefixArraySum[i - 1]
                            + arr[i];
    }
 
    for (int i = 0; i < n - 1; i++)
    {
        // Compute left and right array sum
        int leftArraySum = prefixArraySum[i];
        int rightArraySum = prefixArraySum[n - 1]
                            - prefixArraySum[i];
 
        // Multiplying left and right subarray sum
        int k = leftArraySum * rightArraySum;
 
        // Checking for maximum product of
        // the sum of left and right subarray
        if (k > maxProduct)
        {
            maxProduct = k;
        }
    }
 
    // Printing the maximum value
    return maxProduct;
}
 
// Driver code
public static void Main(String[] args)
{
    int []arr = { 4, 10, 1, 7, 2, 9 };
    int n = arr.Length;
 
    Console.Write(maxProdSum(arr, n));
}
}
 
// This code is contributed by Rajput-Ji

Javascript


输出:
270

时间复杂度: O(n)

辅助空间: O(n)