Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Problem Statement Description and Explanation
3.
Bruteforce Approach
3.1.
Algorithm Complexity
4.
Sorting First
4.1.
Algorithm Complexity
5.
Hashtable-Based Approach
5.1.
Algorithm Complexity
6.
Frequently Asked Questions
6.1.
How do we store values in a hash table?
6.2.
How many total subsets can be formed from an array of size N?
6.3.
What is the time complexity of hashing?
6.4.
Which Among the two - map or an unordered_map, uses hashing to store elements?
6.5.
Why is hashmap preferred over hashtable in some scenarios?
7.
Conclusion
Last Updated: Mar 27, 2024
Medium

The Maximum possible difference of the sum of two subsets of an array

Author Dhruv Sharma
0 upvote

Introduction

In this blog, we would be going through a frequently asked medium-rated problem in technical interviews. Consider a problem on subsets on an array of integers where on being given ‘N’ integers we need to find the difference between the sum of elements in two different subsets such that their difference is maximised. 

Challenging enough!?

So here, we would be taking a look at a brief description and explanation of the same problem along with various approaches that one can take towards solving the problem and we would also be covering the algorithm and the algorithmic complexity analysis on the same.

Must Recommended Topic, Hash Function in Data Structure.

Problem Statement Description and Explanation

Description: So, here, one would be given a problem which is based on subsets of the array. One would be given an array of ‘n’ integers. Where the array could contain repetitive elements with the highest frequency of any elements not exceeding two. One needs to make two subsets out of the given array in such a way that the difference of the sum of their elements is maximum and both of them jointly contain all elements of the given array with a crucial additional condition that no subset should contain repetitive elements. 

The above problem can be better understood using the example below:

Input : a[] = {6, 9, -2, 3}
Output : Maximum Difference of sums of subsets 1 and 2 = 16
Explanation : 
Let Subset 1 = {6, 9, 3} and Subset 2 = {-2}
Sum of elements of subset 1 = 18, of subset B = -2
Difference of Sum of Both subsets = 18 - (-2) = 16

Input : arr[] = {6, 9, 6, 5}
Output : Maximum Difference of sums of subsets 1 and 2 = 14
Explanation: 
Let Subset 1 = {6, 9, 5} and Subset 2 = {6}
Sum of elements of subset 1 = 20, of subset 2 = 6
Difference of Sum of Both subsets = 20 - 6 = 14

For also solving this question one should consider the following conditions which 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 is 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 the subset in such a way that all positive elements belong to one subset and negative ones to other subsets.

Bruteforce Approach

The brute-force approach here focuses on checking whether the elements present in the array appear once or twice in it. If the element is present in the array twice then we don’t include the sum of the element in either of the subsets as it would end up cancelling out. 

If the element appears just once then we keep adding it to the first subset-sum if the element is positive and to the other subset-sum in case the element is negative. 

for i=0 to n-1
    appearsOnce = 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
            appearsOnce = false;
            break;
    if appearsOnce == true
        if (arr[i] > 0)
            firstSubsetSum += arr[i];
        else 
            secondSubsetSum += arr[i];
return abs(firstSubsetSum - secondSubsetSum)

Algorithm Complexity

Time Complexity: O(N^2)

Here we are traversing two nested loops that will be O(N^2).

Space Complexity: O(1)

Since we are using no extra space for the intermediate steps it makes the space complexity a constant.

Sorting First

This approach is an optimised brute-force approach where one sorts the array first and then checks whether the elements present in the array appear once or twice in it and then checks and follows the same set of steps as the brute force approach.

-> first sort the array
-> for i =0 to n-2
      // if subsequent two elements are not equal
      // then add the absolute value of arr[i] to the result
      if arr[i] != arr[i+1]
          result += abs(arr[i])
      // else skip the next element as well
      else
          i++;
          
// specific check for the last two elements
-> if (arr[n-2] != arr[n-1])
    result += arr[n-1]

-> return result;

Algorithm Complexity

Time Complexity: O(N*log(N))

Here we are traversing only once in a loop that will be O(N), and for sorting the vector, it will be (N*log(N)).

Space Complexity: O(1)

Since we are using no extra space for the intermediate steps it makes the space complexity a constant.

Hashtable-Based Approach

This is an optimised approach to the problem where one builds the hashmap/hashtable of the frequency values of the elements in the array after which one can follow the same set of steps as the other approaches to find the maximum difference between the subset sums.

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

Algorithm Complexity

Time Complexity: O(N)

Here we are traversing only once in a loop that will be O(N) after we created a hashtable of the counts of the frequencies for the elements present in the array.

Space Complexity: O(N)

Here, we are using an additional space to create a hashtable (unordered_map in C++) that will take O(N) space in the worst case.

Frequently Asked Questions

How do we store values in a hash table?

In the hash table, we store values in the form of key-value pairs.

How many total subsets can be formed from an array of size N?

From an array of size N, you can form 2^N subsets.

What is the time complexity of hashing?

The overall time complexity of the hash function is O(1).

Which Among the two - map or an unordered_map, uses hashing to store elements?

unordered_map uses hashing to store the elements.

Why is hashmap preferred over hashtable in some scenarios?

Usually, they both are preferred equally, but in the case of NULL keys, hashmap can store them, whereas hashtable cannot store NULL keys.

Conclusion

In this blog, We have extensively discussed various approaches to solve the problem of finding the maximum difference from a given array between two different subsets sums. This problem is often rated and categorised as a Medium to a Hard Level problem.

Recommended problems -

 

To practice more problems similar to this one, you can refer here Minimize Partitioned Subset Sum in an arrayRefer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enrol in our courses, refer to the mock test and problems; look at the interview experiences and interview bundle for placement preparations.

Do upvote our blog to help other ninjas grow.

Happy Learning!

Live masterclass