📌  相关文章
📜  最大化子数组和与其最大元素的乘积

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

最大化子数组和与其最大元素的乘积

给定一个由N个正整数组成的数组arr[] ,任务是找到子数组和与该子数组的最大元素的最大乘积。

例子:

朴素方法:解决问题的最简单方法是生成给定数组的所有子数组,并为每个子数组计算子数组的总和,并将其与子数组中的最大元素相乘。通过将其与计算的产品进行比较来更新最大产品。检查所有子数组后,打印处理所有子数组后得到的最大乘积。

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

高效方法:上述方法也可以通过修改 Kadane 的算法得到优化,得到结果最大值。请按照以下步骤解决问题:

  • 按照以下步骤执行 Kadane 算法:
    • 初始化三个变量,比如 maximumSum 、 currSum currMax0
    • 遍历给定的数组arr[]并执行以下步骤;
      • currSum更新为currSum + arr[i]并将currMax 更新max(currMax, arr[i])
      • 将 maximumSum 的值更新为 max( largestSum , currMax * currSum)
      • 如果currSum的值小于0 ,则将currMaxcurrSum的值更新为0
    • 完成上述步骤后,返回maximumSum的值作为子数组与其最大元素之和的最大乘积。
  • 初始化一个变量,比如maximumSum0 ,它存储子数组之和与其最大元素的最大乘积。
  • 使用给定数组执行更新后的 Kadane 算法,并将返回值存储在maximumSum中。
  • 现在,通过将每个元素乘以(-1)来更新每个数组元素,并再次对更新后的数组执行更新后的 Kadane 算法,这样如果任何子数组的最大元素为负数,则必须考虑该组合。
  • maximumSum的值更新为maximumSum的最大值和上述步骤返回的值。
  • 完成上述步骤后,打印maximumSum的值作为结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the maximum product
// of the sum of the subarray with its
// maximum element
int Kadane(int arr[], int n)
{
 
    int largestSum = 0, currMax = 0;
    int currSum = 0;
 
    // Traverse the array arr[]
    for (int i = 0; i < n; i++) {
 
        // Increment currSum by a[i]
        currSum += arr[i];
 
        // Maximize the value of currMax
        currMax = max(currMax, arr[i]);
 
        // Maximize the value of
        // largestSum
        largestSum = max(largestSum,
                         currMax * currSum);
 
        // If currSum goes less than 0
        // then update currSum = 0
        if (currSum < 0) {
            currMax = 0;
            currSum = 0;
        }
    }
 
    // Return the resultant value
    return largestSum;
}
 
// Function to maximize the product of
// the sum of the subarray with its
// maximum element
int maximumWeight(int arr[], int n)
{
    // Find the largest sum of the
    // subarray
    int largestSum = Kadane(arr, n);
 
    // Multiply each array element
    // with -1
    for (int i = 0; i < n; i++) {
        arr[i] = -arr[i];
    }
 
    // Find the largest sum of the
    // subarray with negation of all
    // array element
    largestSum = max(largestSum,
                     Kadane(arr, n));
 
    // Return the resultant maximum
    // value
    return largestSum;
}
 
// Driver Code
int main()
{
    int arr[] = { 2, -3, 8, -2, 5 };
    int N = sizeof(arr) / sizeof(arr[0]);
    cout << maximumWeight(arr, N);
 
    return 0;
}


Java
// Java program for the above approach
 
import java.io.*;
 
class GFG {
    // Function to find the maximum product
    // of the sum of the subarray with its
    // maximum element
    static int Kadane(int arr[], int n)
    {
 
        int largestSum = 0, currMax = 0;
        int currSum = 0;
 
        // Traverse the array arr[]
        for (int i = 0; i < n; i++) {
 
            // Increment currSum by a[i]
            currSum += arr[i];
 
            // Maximize the value of currMax
            currMax = Math.max(currMax, arr[i]);
 
            // Maximize the value of
            // largestSum
            largestSum
                = Math.max(largestSum, currMax * currSum);
 
            // If currSum goes less than 0
            // then update currSum = 0
            if (currSum < 0) {
                currMax = 0;
                currSum = 0;
            }
        }
 
        // Return the resultant value
        return largestSum;
    }
 
    // Function to maximize the product of
    // the sum of the subarray with its
    // maximum element
    static int maximumWeight(int arr[], int n)
    {
        // Find the largest sum of the
        // subarray
        int largestSum = Kadane(arr, n);
 
        // Multiply each array element
        // with -1
        for (int i = 0; i < n; i++) {
            arr[i] = -arr[i];
        }
 
        // Find the largest sum of the
        // subarray with negation of all
        // array element
        largestSum = Math.max(largestSum, Kadane(arr, n));
 
        // Return the resultant maximum
        // value
        return largestSum;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int arr[] = { 2, -3, 8, -2, 5 };
        int N = arr.length;
        System.out.println(maximumWeight(arr, N));
        // This code is contributed by Potta Lokesh
    }
}


Python3
# Python3 program for the above approach
 
# Function to find the maximum product
# of the sum of the subarray with its
# maximum element
def Kadane(arr, n):
     
    largestSum = 0
    currMax = 0
    currSum = 0
 
    # Traverse the array arr[]
    for i in range(n):
         
        # Increment currSum by a[i]
        currSum += arr[i]
 
        # Maximize the value of currMax
        currMax = max(currMax, arr[i])
 
        # Maximize the value of
        # largestSum
        largestSum = max(largestSum,
                         currMax * currSum)
 
        # If currSum goes less than 0
        # then update currSum = 0
        if (currSum < 0):
            currMax = 0
            currSum = 0
 
    # Return the resultant value
    return largestSum
 
# Function to maximize the product of
# the sum of the subarray with its
# maximum element
def maximumWeight(arr, n):
     
    # Find the largest sum of the
    # subarray
    largestSum = Kadane(arr, n)
 
    # Multiply each array element
    # with -1
    for i in range(n):
        arr[i] = -arr[i]
 
    # Find the largest sum of the
    # subarray with negation of all
    # array element
    largestSum = max(largestSum,
                     Kadane(arr, n))
 
    # Return the resultant maximum
    # value
    return largestSum
 
# Driver Code
if __name__ == '__main__':
     
    arr = [ 2, -3, 8, -2, 5 ]
    N = len(arr)
     
    print(maximumWeight(arr, N))
 
# This code is contributed by mohit kumar 29


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to find the maximum product
// of the sum of the subarray with its
// maximum element
static int Kadane(int []arr, int n)
{
    int largestSum = 0, currMax = 0;
    int currSum = 0;
 
    // Traverse the array arr[]
    for(int i = 0; i < n; i++)
    {
         
        // Increment currSum by a[i]
        currSum += arr[i];
 
        // Maximize the value of currMax
        currMax = Math.Max(currMax, arr[i]);
 
        // Maximize the value of
        // largestSum
        largestSum = Math.Max(largestSum,
                              currMax * currSum);
 
        // If currSum goes less than 0
        // then update currSum = 0
        if (currSum < 0)
        {
            currMax = 0;
            currSum = 0;
        }
    }
 
    // Return the resultant value
    return largestSum;
}
 
// Function to maximize the product of
// the sum of the subarray with its
// maximum element
static int maximumWeight(int []arr, int n)
{
     
    // Find the largest sum of the
    // subarray
    int largestSum = Kadane(arr, n);
 
    // Multiply each array element
    // with -1
    for(int i = 0; i < n; i++)
    {
        arr[i] = -arr[i];
    }
 
    // Find the largest sum of the
    // subarray with negation of all
    // array element
    largestSum = Math.Max(largestSum,
                          Kadane(arr, n));
 
    // Return the resultant maximum
    // value
    return largestSum;
}
 
// Driver Code
public static void Main()
{
    int []arr = { 2, -3, 8, -2, 5 };
    int N = arr.Length;
     
    Console.Write(maximumWeight(arr, N));
}
}
  
// This code is contributed by ipg2016107


Javascript


输出:
88

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