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

📅  最后修改于: 2021-05-14 01:58:56             🧑  作者: 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)

如果您希望与行业专家一起参加实时课程,请参阅Geeks在线课程