The Wayback Machine - https://web.archive.org/web/20210818005713/https://www.geeksforgeeks.org/maximum-possible-difference-of-two-subsets-of-an-array-set-2/
Skip to content
Related Articles

Related Articles

Improve Article

Maximum possible difference of two subsets of an array | Set 2

  • Last Updated : 10 Aug, 2021

Given an array arr[ ] consisting of N integers, the task is to find maximum difference between two subsets obtained by partitioning the array into two subsets.
Note: The subsets cannot any common element. A subset can contain repeating elements.

Examples:

Input: arr[] = {1, 3, 2, 4, 5}
Output: 13
Explanation: The partitions {3, 2, 4, 5} and {1} maximizes the difference between the subsets.

Input: arr[] = {1, -5, 3, 2, -7}
Output: 18

Approach: This problem can be solved using greedy approach. follow the steps given below to solve the problem:



  • When arr[ ] contains both non-negative and negative numbers, put all non-negative numbers in subset A and negative numbers in subset B, and print sum(A) – sum(B).
  • When all numbers are positive, put all numbers in subset A except the smallest positive number put that in subset B, and print sum(A) – sum(B).
  • When all numbers are negative, put all numbers in subset B except the largest negative put that in subset A, and print sum(A) – sum(B).  

Below is the implementation of the above approach:

C++




// C++ Program for the above approach
  
#include <bits/stdc++.h>
using namespace std;
  
int maxSumAfterPartition(int arr[], int n)
{
    // Stores the positive elements
    vector<int> pos;
  
    // Stores the negative elements
    vector<int> neg;
  
    // Stores the count of 0s
    int zero = 0;
  
    // Sum of all positive numbers
    int pos_sum = 0;
  
    // Sum of all negative numbers
    int neg_sum = 0;
  
    // Iterate over the array
    for (int i = 0; i < n; i++) {
  
        if (arr[i] > 0) {
  
            pos.push_back(arr[i]);
            pos_sum += arr[i];
        }
        else if (arr[i] < 0) {
  
            neg.push_back(arr[i]);
            neg_sum += arr[i];
        }
        else {
  
            zero++;
        }
    }
  
    // Stores the difference
    int ans = 0;
  
    // Sort the positive numbers
    // in ascending order
    sort(pos.begin(), pos.end());
  
    // Sort the negative numbers
    // in decreasing order
    sort(neg.begin(), neg.end(), greater<int>());
  
    // Case 1: Include both positive
    // and negative numbers
    if (pos.size() > 0 && neg.size() > 0) {
  
        ans = (pos_sum - neg_sum);
    }
    else if (pos.size() > 0) {
  
        if (zero > 0) {
  
            // Case 2:  When all numbers are
            // positive and array contains 0s
            ans = (pos_sum);
        }
        else {
  
            // Case 3: When all numbers are positive
            ans = (pos_sum - 2 * pos[0]);
        }
    }
    else {
        if (zero > 0) {
  
            // Case 4: When all numbers are
            // negative and array contains 0s
  
            // Put all numbers in subset b
            // and one 0 in subset a
            ans = (-1 * neg_sum);
        }
        else {
            // Case 5: When all numbers are negative
  
            // Place the largest negative number
            // in subset and remaining in b
            ans = (neg[0] - (neg_sum - neg[0]));
        }
    }
  
    return ans;
}
int main()
{
    int arr[] = { 1, 2, 3, -5, -7 };
    int n = sizeof(arr) / sizeof(arr[0]);
  
    cout << maxSumAfterPartition(arr, n);
    return 0;
}
Output:
18

Time Complexity: O(N)
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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.




My Personal Notes arrow_drop_up
Recommended Articles
Page :