Given an array arr[], the task is to find the maximum sum subset containing the equal number of positive and negative elements.
Examples:
Input: arr[] = {1, -2, 3, 4, -5, 8}
Output: 6
Explanation:
Maximum sum subset with equal number of positive and negative elements {8, -2}
Input: arr[] = {-1, -2, -3, -4, -5}
Output: 0
Explanation:
As there are no positive element in the array, Maximum sum subset will be {}
Approach: The idea is to store negative and positive elements into two different arrays and then sort them individually in increasing order. Then use two pointers starting from the highest element of each array and include those pairs whose sum is greater than 0. Otherwise, If the sum of the pair is less than 0 then stop finding more elements because there will be no such pair possible with a sum greater than 0 in the left pairs.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findMaxSum( int * arr, int n)
{
vector< int > a;
vector< int > b;
for ( int i = 0; i < n; i++) {
if (arr[i] > 0) {
a.push_back(arr[i]);
}
else if (arr[i] < 0) {
b.push_back(arr[i]);
}
}
sort(a.begin(), a.end());
sort(b.begin(), b.end());
int p = a.size() - 1;
int q = b.size() - 1;
int s = 0;
while (p >= 0 && q >= 0) {
if (a[p] + b[q] > 0) {
s = s + a[p] + b[q];
}
else {
break ;
}
p = p - 1;
q = q - 1;
}
return s;
}
int main()
{
int arr1[] = { 1, -2, 3, 4, -5, 8 };
int n1 = sizeof (arr1) / sizeof (arr1[0]);
cout << findMaxSum(arr1, n1) << endl;
return 0;
}
|
Java
import java.util.*;
class GFG{
static int findMaxSum( int []arr, int n)
{
Vector<Integer> a = new Vector<Integer>();
Vector<Integer> b = new Vector<Integer>();
for ( int i = 0 ; i < n; i++)
{
if (arr[i] > 0 )
{
a.add(arr[i]);
}
else if (arr[i] < 0 )
{
b.add(arr[i]);
}
}
Collections.sort(a);
Collections.sort(b);
int p = a.size() - 1 ;
int q = b.size() - 1 ;
int s = 0 ;
while (p >= 0 && q >= 0 )
{
if (a.get(p) + b.get(q) > 0 )
{
s = s + a.get(p) + b.get(q);
}
else
{
break ;
}
p = p - 1 ;
q = q - 1 ;
}
return s;
}
public static void main(String[] args)
{
int arr1[] = { 1 , - 2 , 3 , 4 , - 5 , 8 };
int n1 = arr1.length;
System.out.print(
findMaxSum(arr1, n1) + "\n" );
}
}
|
Python3
def findMaxSum(arr, n):
a = []
b = []
for i in range (n):
if (arr[i] > 0 ):
a.append(arr[i])
elif (arr[i] < 0 ):
b.append(arr[i])
a.sort()
b.sort()
p = len (a) - 1
q = len (b) - 1
s = 0
while (p > = 0 and q > = 0 ):
if (a[p] + b[q] > 0 ):
s = s + a[p] + b[q]
else :
break
p = p - 1
q = q - 1
return s
arr1 = [ 1 , - 2 , 3 , 4 , - 5 , 8 ]
n1 = len (arr1)
print (findMaxSum(arr1, n1))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int findMaxSum( int []arr, int n)
{
List< int > a = new List< int >();
List< int > b = new List< int >();
for ( int i = 0; i < n; i++)
{
if (arr[i] > 0)
{
a.Add(arr[i]);
}
else if (arr[i] < 0)
{
b.Add(arr[i]);
}
}
a.Sort();
b.Sort();
int p = a.Count - 1;
int q = b.Count - 1;
int s = 0;
while (p >= 0 && q >= 0)
{
if (a[p] + b[q] > 0)
{
s = s + a[p] + b[q];
}
else
{
break ;
}
p = p - 1;
q = q - 1;
}
return s;
}
public static void Main(String[] args)
{
int []arr1 = { 1, -2, 3, 4, -5, 8 };
int n1 = arr1.Length;
Console.Write(findMaxSum(arr1, n1) + "\n" );
}
}
|
Javascript
<script>
function findMaxSum(arr, n)
{
var a = [];
var b = [];
for ( var i = 0; i < n; i++)
{
if (arr[i] > 0)
{
a.push(arr[i]);
}
else if (arr[i] < 0)
{
b.push(arr[i]);
}
}
a.sort((a, b) => a - b)
b.sort((a, b) => a - b)
var p = a.length - 1;
var q = b.length - 1;
var s = 0;
while (p >= 0 && q >= 0)
{
if (a[p] + b[q] > 0)
{
s = s + a[p] + b[q];
}
else
{
break ;
}
p = p - 1;
q = q - 1;
}
return s;
}
var arr1 = [ 1, -2, 3, 4, -5, 8 ];
var n1 = arr1.length;
document.write( findMaxSum(arr1, n1));
</script>
|
Performance Analysis:
- Time Complexity: O (N*log N) as the inbuilt sort function takes N log N time to complete all operations, hence the overall time taken by the algorithm is N log N
- Auxiliary Space: O(N) since an extra array is used and in the worst case, all elements will be inserted inside the vector hence algorithm takes up linear space