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)
- k size subsets with maximum difference d between max and min
- Minimum difference between max and min of all K-size subsets
- Maximum possible difference of two subsets of an array
- Minimize the absolute difference of sum of two subsets
- Maximum difference between two subsets of m elements
- Index Mapping (or Trivial Hashing) with negatives allowed
- Sum of XOR of all possible subsets
- Subsets having Sum between A and B
- Sum of the products of all possible Subsets
- Sum of bitwise AND of all possible subsets of given set
- Sum of bitwise OR of all possible subsets of given set
- Number of subsets with zero sum
- Sum of average of all subsets
- Finding all subsets of a given set in Java
- Count number of subsets having a particular XOR value
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.