Open In App

Maximum possible difference of two subsets of an array

Last Updated : 24 Mar, 2023
Comments
Improve
Suggest changes
4 Likes
Like
Report

Given an array of n-integers. The array may contain repetitive elements but the highest frequency of any element must not exceed two. You have to make two subsets such that the difference of the sum of their elements is maximum and both of them jointly contain all elements of the given array along with the most important condition, no subset should contain repetitive elements. 

Examples: 

Input : arr[] = {5, 8, -1, 4}
Output : Maximum Difference = 18
Explanation : 
Let Subset A = {5, 8, 4} & Subset B = {-1}
Sum of elements of subset A = 17, of subset B = -1
Difference of Sum of Both subsets = 17 - (-1) = 18

Input : arr[] = {5, 8, 5, 4}
Output : Maximum Difference = 12
Explanation : 
Let Subset A = {5, 8, 4} & Subset B = {5}
Sum of elements of subset A = 17, of subset B = 5
Difference of Sum of Both subsets = 17 - 5 = 12

Before solving this question we have to take care of some given conditions, and they are listed as: 

  • While building up the subsets, take care that no subset should contain repetitive elements. And for this, we can conclude that all such elements whose frequency are 2, going to be part of both subsets, and hence overall they don't have any impact on the difference of subset-sum. So, we can easily ignore them.
  • For making the difference of the sum of elements of both subset maximum we have to make subset in such a way that all positive elements belong to one subset and negative ones to other subsets.

Algorithm with time complexity O(n2): 

for i=0 to n-1
    isSingleOccurrence = true;
    for  j= i+1 to n-1

        // if frequency of any element is two
        // make both equal to zero
        if arr[i] equals arr[j]
            arr[i] = arr[j] = 0
            isSingleOccurrence = false;
            break;
            
    if isSingleOccurrence == true
        if (arr[i] > 0)
            SubsetSum_1 += arr[i];
        else 
            SubsetSum_2 += arr[i];
return abs(SubsetSum_1 - SubsetSum2)

Implementation:

C++
Java Python3 C# PHP JavaScript

Output
Maximum Difference = 20

Time Complexity O(n2)
Auxiliary Space: O(1)

Algorithm with time complexity O(n log n): 

-> sort the array
-> for i =0 to n-2
      // consecutive two elements are not equal
      // add absolute arr[i] to result
      if arr[i] != arr[i+1]
          result += abs(arr[i])
      // else skip next element too
      else
          i++;
          
// special check for last two elements
-> if (arr[n-2] != arr[n-1])
    result += arr[n-1]

-> return result;

Implementation:

C++
Java Python 3 C# PHP JavaScript

Output
Maximum Difference = 20

Time Complexity: O(n log n)
Auxiliary Space: O(1)

Algorithm with time complexity O(n): 

make hash table for positive elements:
    for all positive elements(arr[i])
        if frequency == 1
            SubsetSum_1 += arr[i];
make hash table for negative elements:
    for all negative elements
        if frequency == 1
            SubsetSum_2 += arr[i];
return abs(SubsetSum_1 - SubsetSum2)

Implementation:

C++
Java Python3 C# JavaScript

Output
Maximum Difference = 20

Time Complexity: O(n)
Auxiliary Space: O(n)


Next Article
Article Tags :
Practice Tags :

Similar Reads