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++

`// C++ implementation to find the ` `// maximum sum subset having equal ` `// number of positive and negative ` `// elements in the subset ` ` ` `#include <bits/stdc++.h> ` ` ` `using` `namespace` `std; ` ` ` `// Function to find maximum sum ` `// subset with equal number of ` `// positive and negative elements ` `int` `findMaxSum(` `int` `* arr, ` `int` `n) ` `{ ` ` ` `vector<` `int` `> a; ` ` ` `vector<` `int` `> b; ` ` ` ` ` `// Loop to store the positive ` ` ` `// and negative elements in ` ` ` `// two different array ` ` ` `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 both the array ` ` ` `sort(a.begin(), a.end()); ` ` ` `sort(b.begin(), b.end()); ` ` ` ` ` `// Pointers starting from ` ` ` `// the highest elements ` ` ` `int` `p = a.size() - 1; ` ` ` `int` `q = b.size() - 1; ` ` ` `int` `s = 0; ` ` ` ` ` `// Find pairs having sum ` ` ` `// greater than zero ` ` ` `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; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `arr1[] = { 1, -2, 3, 4, -5, 8 }; ` ` ` `int` `n1 = ` `sizeof` `(arr1) / ` `sizeof` `(arr1[0]); ` ` ` ` ` `cout << findMaxSum(arr1, n1) << endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation to find the ` `// maximum sum subset having equal ` `// number of positive and negative ` `// elements in the subset ` `import` `java.util.*; ` ` ` `class` `GFG{ ` ` ` `// Function to find maximum sum ` `// subset with equal number of ` `// positive and negative elements ` `static` `int` `findMaxSum(` `int` `[]arr, ` `int` `n) ` `{ ` ` ` `Vector<Integer> a = ` `new` `Vector<Integer>(); ` ` ` `Vector<Integer> b = ` `new` `Vector<Integer>(); ` ` ` ` ` `// Loop to store the positive ` ` ` `// and negative elements in ` ` ` `// two different array ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) ` ` ` `{ ` ` ` `if` `(arr[i] > ` `0` `) ` ` ` `{ ` ` ` `a.add(arr[i]); ` ` ` `} ` ` ` `else` `if` `(arr[i] < ` `0` `) ` ` ` `{ ` ` ` `b.add(arr[i]); ` ` ` `} ` ` ` `} ` ` ` ` ` `// Sort both the array ` ` ` `Collections.sort(a); ` ` ` `Collections.sort(b); ` ` ` ` ` `// Pointers starting from ` ` ` `// the highest elements ` ` ` `int` `p = a.size() - ` `1` `; ` ` ` `int` `q = b.size() - ` `1` `; ` ` ` `int` `s = ` `0` `; ` ` ` ` ` `// Find pairs having sum ` ` ` `// greater than zero ` ` ` `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; ` `} ` ` ` ` ` `// Driver code ` `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"` `); ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation to find the ` `# maximum sum subset having equal ` `# number of positive and negative ` `# elements in the subset ` ` ` `# Function to find maximum sum ` `# subset with equal number of ` `# positive and negative elements ` `def` `findMaxSum(arr, n): ` ` ` ` ` `a ` `=` `[] ` ` ` `b ` `=` `[] ` ` ` ` ` `# Loop to store the positive ` ` ` `# and negative elements in ` ` ` `# two different array ` ` ` `for` `i ` `in` `range` `(n): ` ` ` `if` `(arr[i] > ` `0` `): ` ` ` `a.append(arr[i]) ` ` ` ` ` `elif` `(arr[i] < ` `0` `): ` ` ` `b.append(arr[i]) ` ` ` ` ` `# Sort both the array ` ` ` `a.sort() ` ` ` `b.sort() ` ` ` ` ` `# Pointers starting from ` ` ` `# the highest elements ` ` ` `p ` `=` `len` `(a) ` `-` `1` ` ` `q ` `=` `len` `(b) ` `-` `1` ` ` `s ` `=` `0` ` ` ` ` `# Find pairs having sum ` ` ` `# greater than zero ` ` ` `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 ` ` ` `# Driver code ` `arr1 ` `=` `[ ` `1` `, ` `-` `2` `, ` `3` `, ` `4` `, ` `-` `5` `, ` `8` `] ` `n1 ` `=` `len` `(arr1) ` ` ` `print` `(findMaxSum(arr1, n1)) ` ` ` `# This code is contributed by shubhamsingh10 ` |

*chevron_right*

*filter_none*

**Output:**

6

**Performance Analysis:**

**Time Complexity:**O(N*logN)**Auxiliary Space:**O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Minimum number of changes such that elements are first Negative and then Positive
- Find maximum subset sum formed by partitioning any subset of array into 2 partitions with equal sum
- Positive elements at even and negative at odd positions (Relative order not maintained)
- Maximum number of elements greater than X after equally distributing subset of array
- Replace all elements by difference of sums of positive and negative numbers after that element
- Check if array elements are consecutive in O(n) time and O(1) space (Handles Both Positive and negative numbers)
- Maximum subset with bitwise OR equal to k
- Only integer with positive value in positive negative value in array
- Find maximum number of elements such that their absolute difference is less than or equal to 1
- Maximum subset sum such that no two elements in set have same digit in them
- Sum of elements in 1st array such that number of elements less than or equal to them in 2nd array is maximum
- Partition negative and positive without comparison with 0
- Probability such that two subset contains same number of elements
- Pairs of Positive Negative values in an array
- Smallest number that never becomes negative when processed against array elements
- C program to count Positive and Negative numbers in an Array
- Segregating negative and positive maintaining order and O(1) space
- Print all the pairs that contains the positive and negative values of an element
- Make three non-empty sets with negative, positive and 0 products
- Number of arrays of size N whose elements are positive integers and sum is K

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.