Given an of integers of size N. The task is to separate these integers into two groups g1 and g2 such that (sum of elements of g1) – (sum of elements of g2) becomes maximum. Your task is to print the value of result. We may keep one subset as empty.
Input : 3, 7, -4, 10, -11, 2
Output : 37
g1: 3, 7, 10, 2
g2: -4, -11
result = ( 3 + 7 + 10 + 2 ) – ( -4 + -11) = 22 – (-15) = 37
Input : 2, 2, -2, -2
Output : 8
The idea is to group integers according to their sign value i.e., we group positive integers as g1 and negative integers as g2.
Since, – ( -g2 ) = +g2
Therefore, result becomes g1 + |g2|.
Time Complexity: O(n)
- Minimize the absolute difference of sum of two subsets
- k size subsets with maximum difference d between max and min
- Maximum difference between two subsets of m elements
- Minimum difference between max and min of all K-size subsets
- Maximum possible difference of two subsets of an array
- Partition an array of non-negative integers into two subsets such that average of both the subsets is equal
- Index Mapping (or Trivial Hashing) with negatives allowed
- Find subarray with given sum with negatives allowed in constant space
- Subsets having Sum between A and B
- Sum of XOR of all possible subsets
- Sum of bitwise OR of all possible subsets of given set
- Sum of the sums of all possible subsets
- Sum of bitwise AND of all possible subsets of given set
- Number of subsets with zero sum
- Sum of the products of all possible Subsets
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.