📌  相关文章
📜  数组分成三段后的最大和

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

数组分成三段后的最大和

给定一个大小为N的数组a 。任务是通过将数组分成三段来找到数组的最大总和,使得第一段中的每个元素乘以 -1,第二段中的每个元素乘以 1,第三段中的每个元素为乘以-1。线段可以相交,任何线段都可以包含零。

例子:

方法:
首先,我们需要为应该进行除法的每个第 i 个元素计算所有可能的情况。

  • 在第一次遍历中,查找第 i 个元素是否通过乘以 -1 或保持原样产生最大和。
  • 将所有值存储在数组b中。
  • 在第二次遍历中,通过减少a[i]并添加b[i]来找到最大和。

以下是上述方法的实现:

C++
// C++ program to find maximum sum of array
// after dividing it into three segments
#include 
using namespace std;
 
// Function to find maximum sum of array
// after dividing it into three segments
int Max_Sum(int a[], int n)
{
    // To store sum upto ith index
    int b[n];
    int S = 0;
    int res = 0;
     
    // Traverse through the array
    for (int i = 0; i < n; i++)
    {
        b[i] = res;
        res += a[i];
        S += a[i];
         
        // Get the maximum possible sum
        res = max(res, -S);
    }
     
    // Store in the required answer
    int ans = S;
     
    // Maximum sum starting from left segment
    // by choosing between keeping array element as
    // it is or subtracting it
    ans = max(ans, res);
 
 
    // Finding maximum sum by decreasing a[i] and
    // adding b[i] to it that means max(multiplying
    // it by -1 or using b[i] value)
    int g = 0;
     
    // For third segment
    for (int i = n - 1; i >= 0; --i) {
        g -= a[i];
        ans = max(ans, g + b[i]);
    }
     
    // return the required answer
    return ans;
}
 
// Driver code
int main()
{
    int a[] = { -6, 10, -3, 10, -2 };
 
    int n = sizeof(a) / sizeof(a[0]);
     
    // Function call
    cout << "Maximum sum is: " << Max_Sum(a, n);
 
    return 0;
}


Java
// Java program to find maximum sum of array
// after dividing it into three segments
import java.util.*;
 
class GFG
{
 
// Function to find maximum sum of array
// after dividing it into three segments
static int Max_Sum(int a[], int n)
{
    // To store sum upto ith index
    int []b = new int[n];
    int S = 0;
    int res = 0;
     
    // Traverse through the array
    for (int i = 0; i < n; i++)
    {
        b[i] = res;
        res += a[i];
        S += a[i];
         
        // Get the maximum possible sum
        res = Math.max(res, -S);
    }
     
    // Store in the required answer
    int ans = S;
     
    // Maximum sum starting from left segment
    // by choosing between keeping array element as
    // it is or subtracting it
    ans = Math.max(ans, res);
 
    // Finding maximum sum by decreasing a[i] and
    // adding b[i] to it that means max(multiplying
    // it by -1 or using b[i] value)
    int g = 0;
     
    // For third segment
    for (int i = n - 1; i >= 0; --i)
    {
        g -= a[i];
        ans = Math.max(ans, g + b[i]);
    }
     
    // return the required answer
    return ans;
}
 
// Driver code
public static void main(String[] args)
{
    int a[] = { -6, 10, -3, 10, -2 };
 
    int n = a.length;
     
    // Function call
    System.out.println("Maximum sum is: " +
                            Max_Sum(a, n));
}
}
 
// This code is contributed by Princi Singh


Python3
# Python3 program to find
# maximum sum of array after
# dividing it into three segments
 
# Function to find maximum sum of array
# after dividing it into three segments
def Max_Sum(a, n):
     
    # To store sum upto ith index
    b = [0 for i in range(n)]
    S = 0
    res = 0
 
    # Traverse through the array
    for i in range(n):
        b[i] = res
        res += a[i]
        S += a[i]
 
        # Get the maximum possible sum
        res = max(res, -S)
 
    # Store in the required answer
    ans = S
 
    # Maximum sum starting from
    # left segment by choosing between
    # keeping array element as it is
    # or subtracting it
    ans = max(ans, res)
 
    # Finding maximum sum by decreasing
    # a[i] and adding b[i] to it
    # that means max(multiplying it
    # by -1 or using b[i] value)
    g = 0
 
    # For third segment
    for i in range(n - 1, -1, -1):
        g -= a[i]
        ans = max(ans, g + b[i])
 
    # return the required answer
    return ans
 
# Driver code
a = [-6, 10, -3, 10, -2]
 
n = len(a)
 
# Function call
print("Maximum sum is:",
          Max_Sum(a, n))
            
# This code is contributed
# by Mohit Kumar


C#
// C#+ program to find maximum sum of array
// after dividing it into three segments
using System;
 
class GFG
{
 
// Function to find maximum sum of array
// after dividing it into three segments
static int Max_Sum(int []a, int n)
{
    // To store sum upto ith index
    int []b = new int[n];
    int S = 0;
    int res = 0;
     
    // Traverse through the array
    for (int i = 0; i < n; i++)
    {
        b[i] = res;
        res += a[i];
        S += a[i];
         
        // Get the maximum possible sum
        res = Math.Max(res, -S);
    }
     
    // Store in the required answer
    int ans = S;
     
    // Maximum sum starting from left segment
    // by choosing between keeping array element
    // as it is or subtracting it
    ans = Math.Max(ans, res);
 
    // Finding maximum sum by decreasing a[i] and
    // adding b[i] to it that means max(multiplying
    // it by -1 or using b[i] value)
    int g = 0;
     
    // For third segment
    for (int i = n - 1; i >= 0; --i)
    {
        g -= a[i];
        ans = Math.Max(ans, g + b[i]);
    }
     
    // return the required answer
    return ans;
}
 
// Driver code
public static void Main()
{
    int []a = { -6, 10, -3, 10, -2 };
 
    int n = a.Length;
     
    // Function call
    Console.WriteLine("Maximum sum is: " +
                           Max_Sum(a, n));
}
}
 
// This code is contributed by anuj_67..


PHP
= 0; --$i)
    {
        $g -= $a[$i];
        $ans = max($ans, $g + $b[$i]);
    }
     
    // return the required answer
    return $ans;
}
 
// Driver code
$a = array(-6, 10, -3, 10, -2 );
 
$n = count($a);
 
// Function call
echo ("Maximum sum is: ");
echo Max_Sum($a, $n);
 
// This code is contributed by Naman_garg.
?>


Javascript


输出:
Maximum sum is: 25

时间复杂度: O(N)