Maximum sum of array after removing a positive or negative subarray
Last Updated :
02 Jul, 2021
Given an array arr[] of N non-zero integers, the task is to find the maximum sum of the array by removing exactly one contiguous set of positive or negative elements.
Examples:
Input: arr[] = {-2, -3, 4, -1, -2, 1, 5, -3}
Output: 4
Explanation: Maximum array sum can be obtained by removing subarray arr[0, 1] since arr[0, 1] has same type of elements i.e., negative. Thus, the required sum is 4.
Input: arr[] = {2, -10, 4, 2, -8, -7}
Output: -2
Explanation: Maximum array sum can be obtained by removing subarray arr[4, 5] since arr[4, 5] has same type of elements i.e., negative. Thus, the required sum is -2.
Approach: The given problem can be solved based on the following observation i.e to obtain the maximum sum, a contiguous set of negative elements are to be removed since removing positive elements will reduce the array sum. However, if there are no negative elements then remove the smallest element of the array. Follow the steps to solve the problem:
- Traverse the array, arr[] and store the total sum of the array in the variable, say sum.
- Store the maximum contiguous negative sum in a variable, say max_neg.
- If there are no negative elements in the array, then update max_neg to the smallest element of an array.
- Update the value of sum to (sum – max_neg).
- Print the value of sum as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void maxSum( int arr[], int n)
{
int sum = 0;
int max_neg = INT_MAX;
int tempsum = 0;
int small = INT_MAX;
for ( int i = 0; i < n; i++) {
sum += arr[i];
small = min(small, arr[i]);
if (arr[i] > 0) {
tempsum = 0;
}
else {
tempsum += arr[i];
}
max_neg = min(max_neg, tempsum);
}
if (max_neg == 0) {
max_neg = small;
}
cout << sum - max_neg;
}
int main()
{
int arr[] = { -2, -3, 4, -1, -2, 1, 5, -3 };
int n = sizeof (arr) / sizeof (arr[0]);
maxSum(arr, n);
return 0;
}
|
Java
import java.io.*;
class GFG{
static void maxSum( int arr[], int n)
{
int sum = 0 ;
int max_neg = Integer.MAX_VALUE;
int tempsum = 0 ;
int small = Integer.MAX_VALUE;
for ( int i = 0 ; i < n; i++)
{
sum += arr[i];
small = Math.min(small, arr[i]);
if (arr[i] > 0 )
{
tempsum = 0 ;
}
else
{
tempsum += arr[i];
}
max_neg = Math.min(max_neg, tempsum);
}
if (max_neg == 0 )
{
max_neg = small;
}
System.out.println(sum - max_neg);
}
public static void main(String[] args)
{
int arr[] = { - 2 , - 3 , 4 , - 1 , - 2 , 1 , 5 , - 3 };
int n = arr.length;
maxSum(arr, n);
}
}
|
Python3
import sys
def maxSum(arr, n):
sum = 0
max_neg = sys.maxsize
tempsum = 0
small = sys.maxsize
for i in range (n):
sum + = arr[i]
small = min (small, arr[i])
if (arr[i] > 0 ):
tempsum = 0
else :
tempsum + = arr[i]
max_neg = min (max_neg, tempsum)
if (max_neg = = 0 ):
max_neg = small
print ( sum - max_neg)
if __name__ = = '__main__' :
arr = [ - 2 , - 3 , 4 , - 1 , - 2 , 1 , 5 , - 3 ]
n = len (arr)
maxSum(arr, n)
|
Javascript
<script>
function maxSum(arr, n) {
let sum = 0;
let max_neg = Number.MAX_SAFE_INTEGER;
let tempsum = 0;
let small = Number.MAX_SAFE_INTEGER;
for (let i = 0; i < n; i++) {
sum += arr[i];
small = Math.min(small, arr[i]);
if (arr[i] > 0) {
tempsum = 0;
}
else {
tempsum += arr[i];
}
max_neg = Math.min(max_neg, tempsum);
}
if (max_neg == 0) {
max_neg = small;
}
document.write(sum - max_neg);
}
let arr = [-2, -3, 4, -1, -2, 1, 5, -3];
let n = arr.length;
maxSum(arr, n);
</script>
|
C#
using System;
class GFG{
static void maxSum( int []arr, int n)
{
int sum = 0;
int max_neg = Int32.MaxValue;
int tempsum = 0;
int small = Int32.MaxValue;
for ( int i = 0; i < n; i++)
{
sum += arr[i];
small = Math.Min(small, arr[i]);
if (arr[i] > 0)
{
tempsum = 0;
}
else
{
tempsum += arr[i];
}
max_neg = Math.Min(max_neg, tempsum);
}
if (max_neg == 0)
{
max_neg = small;
}
Console.Write(sum - max_neg);
}
public static void Main(String[] args)
{
int []arr = { -2, -3, 4, -1, -2, 1, 5, -3 };
int n = arr.Length;
maxSum(arr, n);
}
}
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...