📜  删除正或负子数组后数组的最大和

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

给定一个由N个非零整数组成的数组arr[] ,任务是通过精确删除一组连续的正或负元素来找到数组的最大和。

例子:

方法:给定的问题可以基于以下观察来解决,即为了获得最大和,一组连续的负元素将被移除,因为移除正元素会减少数组和。但是,如果没有负元素,则删除数组的最小元素。请按照以下步骤解决问题:

  • 遍历数组, arr[]并存储 变量中数组的总和,比如sum
  • 将最大连续负和存储在一个变量中,比如max_neg
  • 如果数组中没有负元素,则将max_neg更新为 数组的最小元素。
  • sum的值更新为(sum – max_neg)
  • 打印sum的值作为结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the maximum sum of
// array after removing either the contiguous
// positive or negative elements
void maxSum(int arr[], int n)
{
    // Store the total sum of array
    int sum = 0;
 
    // Store the maximum contiguous
    // negative sum
    int max_neg = INT_MAX;
 
    // Store the sum of current
    // contiguous negative elements
    int tempsum = 0;
 
    // Store the minimum element of array
    int small = INT_MAX;
 
    // Traverse the array, arr[]
    for (int i = 0; i < n; i++) {
 
        // Update the overall sum
        sum += arr[i];
 
        // Store minimum element of array
        small = min(small, arr[i]);
 
        // If arr[i] is positive
        if (arr[i] > 0) {
 
            // Update temp_sum to 0
            tempsum = 0;
        }
 
        else {
 
            // Add arr[i] to temp_sum
            tempsum += arr[i];
        }
 
        // Update max_neg
        max_neg = min(max_neg, tempsum);
    }
 
    // If no negative element in array
    // then remove smallest positive element
    if (max_neg == 0) {
        max_neg = small;
    }
 
    // Print the required sum
    cout << sum - max_neg;
}
 
// Driver Code
int main()
{
    // Given Input
    int arr[] = { -2, -3, 4, -1, -2, 1, 5, -3 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    // Function Call
    maxSum(arr, n);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
 
class GFG{
 
// Function to find the maximum sum of
// array after removing either the contiguous
// positive or negative elements
static void maxSum(int arr[], int n)
{
     
    // Store the total sum of array
    int sum = 0;
 
    // Store the maximum contiguous
    // negative sum
    int max_neg = Integer.MAX_VALUE;
 
    // Store the sum of current
    // contiguous negative elements
    int tempsum = 0;
 
    // Store the minimum element of array
    int small = Integer.MAX_VALUE;
 
    // Traverse the array, arr[]
    for(int i = 0; i < n; i++)
    {
         
        // Update the overall sum
        sum += arr[i];
 
        // Store minimum element of array
        small = Math.min(small, arr[i]);
 
        // If arr[i] is positive
        if (arr[i] > 0)
        {
             
            // Update temp_sum to 0
            tempsum = 0;
        }
        else
        {
             
            // Add arr[i] to temp_sum
            tempsum += arr[i];
        }
 
        // Update max_neg
        max_neg = Math.min(max_neg, tempsum);
    }
 
    // If no negative element in array
    // then remove smallest positive element
    if (max_neg == 0)
    {
        max_neg = small;
    }
 
    // Print the required sum
    System.out.println(sum - max_neg);
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given Input
    int arr[] = { -2, -3, 4, -1, -2, 1, 5, -3 };
    int n = arr.length;
 
    // Function Call
    maxSum(arr, n);
}
}
 
// This code is contributed by Dharanendra L V.


Python3
# python 3 program for the above approach
 
import sys
# Function to find the maximum sum of
# array after removing either the contiguous
# positive or negative elements
def maxSum(arr, n):
   
    # Store the total sum of array
    sum = 0
 
    # Store the maximum contiguous
    # negative sum
    max_neg = sys.maxsize
 
    # Store the sum of current
    # contiguous negative elements
    tempsum = 0
 
    # Store the minimum element of array
    small = sys.maxsize
 
    # Traverse the array, arr[]
    for i in range(n):
        # Update the overall sum
        sum += arr[i]
 
        # Store minimum element of array
        small = min(small, arr[i])
 
        # If arr[i] is positive
        if (arr[i] > 0):
            # Update temp_sum to 0
            tempsum = 0
 
        else:
 
            # Add arr[i] to temp_sum
            tempsum += arr[i]
 
        # Update max_neg
        max_neg = min(max_neg, tempsum)
 
    # If no negative element in array
    # then remove smallest positive element
    if (max_neg == 0):
        max_neg = small
 
    # Print the required sum
    print(sum - max_neg)
 
# Driver Code
if __name__ == '__main__':
   
    # Given Input
    arr = [-2, -3, 4, -1, -2, 1, 5, -3]
    n = len(arr)
 
    # Function Call
    maxSum(arr, n)
     
    # This code is contributed by bgangwar59.


Javascript


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to find the maximum sum of
// array after removing either the contiguous
// positive or negative elements
static void maxSum(int []arr, int n)
{
     
    // Store the total sum of array
    int sum = 0;
 
    // Store the maximum contiguous
    // negative sum
    int max_neg = Int32.MaxValue;
 
    // Store the sum of current
    // contiguous negative elements
    int tempsum = 0;
 
    // Store the minimum element of array
    int small = Int32.MaxValue;
 
    // Traverse the array, arr[]
    for(int i = 0; i < n; i++)
    {
         
        // Update the overall sum
        sum += arr[i];
 
        // Store minimum element of array
        small = Math.Min(small, arr[i]);
 
        // If arr[i] is positive
        if (arr[i] > 0)
        {
             
            // Update temp_sum to 0
            tempsum = 0;
        }
        else
        {
             
            // Add arr[i] to temp_sum
            tempsum += arr[i];
        }
 
        // Update max_neg
        max_neg = Math.Min(max_neg, tempsum);
    }
 
    // If no negative element in array
    // then remove smallest positive element
    if (max_neg == 0)
    {
        max_neg = small;
    }
 
    // Print the required sum
    Console.Write(sum - max_neg);
}
 
// Driver Code
public static void Main(String[] args)
{
     
    // Given Input
    int []arr = { -2, -3, 4, -1, -2, 1, 5, -3 };
    int n = arr.Length;
 
    // Function Call
    maxSum(arr, n);
}
}
 
// This code is contributed by shivanisinghss2110


输出
4

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

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