📌  相关文章
📜  将前缀和后缀乘以-1后最大化数组的总和

📅  最后修改于: 2021-10-26 02:40:38             🧑  作者: Mango

给定一个长度为N的数组arr[] ,任务是通过最多执行一次以下操作来最大化数组所有元素的总和。

  • 选择数组的前缀并将所有元素乘以 -1。
  • 选择数组的后缀并将所有元素乘以 -1。

例子:

处理方法:问题中的关键观察是如果前缀和后缀的选择范围相交,则相交部分的元素具有相同的符号。因此,选择前缀和后缀数组的不相交范围总是更好。下面是步骤的图示:

  • 很容易观察到,数组中会有一个部分/子数组,其总和与原始元素的总和相同,而其他元素的总和是相反的。所以数组的新总和将是:
// X - Sum of subarray which is not in
//     the range of the prefix and suffix
// S - Sum of the original array

New Sum = X + -1*(S - X) = 2*X - S 
  • 因此,我们的想法是最大化 X 的值以获得最大和,因为 S 是无法更改的常数值。这可以在 Kadane 算法的帮助下实现。

下面是上述方法的实现:

C++
// C++ implementation to find the
// maximum sum of the array by
// multiplying the prefix and suffix
// of the array by -1
 
#include 
 
using namespace std;
 
// Kadane's algorithm to find
// the maximum subarray sum
int maxSubArraySum(int a[], int size)
{
    int max_so_far = INT_MIN,
         max_ending_here = 0;
     
    // Loop to find the maximum subarray
    // array sum in the given array
    for (int i = 0; i < size; i++) {
        max_ending_here =
             max_ending_here + a[i];
        if (max_ending_here < 0)
            max_ending_here = 0;
        if (max_so_far < max_ending_here)
            max_so_far = max_ending_here;
    }
    return max_so_far;
}
 
// Function to find the maximum
// sum of the array by multiplying
// the prefix and suffix by -1
int maxSum(int a[], int n)
{
     
    // Total intital sum
    int S = 0;
     
    // Loop to find the maximum
    // sum of the array
    for (int i = 0; i < n; i++)
        S += a[i];
    int X = maxSubArraySum(a, n);
     
    // Maximum value
    return 2 * X - S;
}
 
// Driver Code
int main()
{
    int a[] = { -1, -2, -3 };
    int n = sizeof(a) / sizeof(a[0]);
    int max_sum = maxSum(a, n);
    cout << max_sum;
    return 0;
}


Java
// Java implementation to find the
// maximum sum of the array by
// multiplying the prefix and suffix
// of the array by -1
class GFG
{
      
    // Kadane's algorithm to find
    // the maximum subarray sum
    static int maxSubArraySum(int a[], int size)
    {
        int max_so_far = Integer.MIN_VALUE,
             max_ending_here = 0;
          
        // Loop to find the maximum subarray
        // array sum in the given array
        for (int i = 0; i < size; i++) {
            max_ending_here =
                 max_ending_here + a[i];
            if (max_ending_here < 0)
                max_ending_here = 0;
            if (max_so_far < max_ending_here)
                max_so_far = max_ending_here;
        }
        return max_so_far;
    }
      
    // Function to find the maximum
    // sum of the array by multiplying
    // the prefix and suffix by -1
    static int maxSum(int a[], int n)
    {
          
        // Total intital sum
        int S = 0;
        int i;
 
        // Loop to find the maximum
        // sum of the array
        for (i = 0; i < n; i++)
            S += a[i];
        int X = maxSubArraySum(a, n);
          
        // Maximum value
        return 2 * X - S;
    }
      
    // Driver Code
    public static void main(String []args)
    {
        int a[] = { -1, -2, -3 };
        int n = a.length;
        int max_sum = maxSum(a, n);
        System.out.print(max_sum);
    }
}
 
// This code is contributed by chitranayal


Python3
# Python3 implementation to find the
# maximum sum of the array by
# multiplying the prefix and suffix
# of the array by -1
 
# Kadane's algorithm to find
# the maximum subarray sum
def maxSubArraySum(a, size):
    max_so_far = -10**9
    max_ending_here = 0
 
    # Loop to find the maximum subarray
    # array sum in the given array
    for i in range(size):
        max_ending_here = max_ending_here + a[i]
        if (max_ending_here < 0):
            max_ending_here = 0
        if (max_so_far < max_ending_here):
            max_so_far = max_ending_here
    return max_so_far
 
# Function to find the maximum
# sum of the array by multiplying
# the prefix and suffix by -1
def maxSum(a, n):
 
    # Total intital sum
    S = 0
 
    # Loop to find the maximum
    # sum of the array
    for i in range(n):
        S += a[i]
    X = maxSubArraySum(a, n)
 
    # Maximum value
    return 2 * X - S
 
# Driver Code
if __name__ == '__main__':
    a=[-1, -2, -3]
    n= len(a)
    max_sum = maxSum(a, n)
    print(max_sum)
 
# This code is contributed by mohit kumar 29


C#
// C# implementation to find the
// maximum sum of the array by
// multiplying the prefix and suffix
// of the array by -1
using System;
 
class GFG
{
       
    // Kadane's algorithm to find
    // the maximum subarray sum
    static int maxSubArraySum(int []a, int size)
    {
        int max_so_far = int.MinValue,
             max_ending_here = 0;
           
        // Loop to find the maximum subarray
        // array sum in the given array
        for (int i = 0; i < size; i++) {
            max_ending_here =
                 max_ending_here + a[i];
            if (max_ending_here < 0)
                max_ending_here = 0;
            if (max_so_far < max_ending_here)
                max_so_far = max_ending_here;
        }
        return max_so_far;
    }
       
    // Function to find the maximum
    // sum of the array by multiplying
    // the prefix and suffix by -1
    static int maxSum(int []a, int n)
    {
           
        // Total intital sum
        int S = 0;
        int i;
  
        // Loop to find the maximum
        // sum of the array
        for (i = 0; i < n; i++)
            S += a[i];
        int X = maxSubArraySum(a, n);
           
        // Maximum value
        return 2 * X - S;
    }
       
    // Driver Code
    public static void Main(String []args)
    {
        int []a = { -1, -2, -3 };
        int n = a.Length;
        int max_sum = maxSum(a, n);
        Console.Write(max_sum);
    }
}
 
// This code is contributed by sapnasingh4991


Javascript


输出:
6

时间复杂度: O(N)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程