Related Articles

# Find subset with maximum sum under given condition

• Last Updated : 03 Jun, 2021

Given values[] and labels[] of n items and a positive integer limit, We need to choose a subset of these items in such a way that the number of the same type of label in the subset should be <= limit and sum of values are maximum among all possible subset choices.

Examples:

```Input: values[] = [5, 3, 7, 1, 2],
labels[] = [5, 7, 7, 7, 6],
limit = 2
Output: 17
Explanation:
You can select first, second, third
and Fifth values.
So, there is 1 value of the label 5 -> {5},
2 value of the label 7 -> {3, 7} ,
1 value of the label 6 -> {2}.
Final subset = {5, 3, 7, 2}
Sum  = 5 + 3 + 7 + 2 = 17.

Input: values[] = [9, 8, 7, 6, 5],
labels[] = [5, 7, 7, 7, 6],
limit = 2
Output: 29```

Approach: The idea is to use a Multimap and Hashmap to solve this problem.

• We will store all the values and the respective labels as a pair in the Multimap.
• In Multimap the {values, labels} pairs are sorted in increasing order. So, we will traverse the Multimap in reverse to get the pairs in decreasing order.
• Now, we will add the values in our answer and store the occurrence of each label in Hashmap to check if the number of occurrences is <= limit.

Below is the implementation of the above approach:

## C++

 `// C++ program to Find subset with``// maximum sum under given condition.``#include ``using` `namespace` `std;` `// Function to return the maximum``// sum of the subset``int` `MaxSumSubset(vector<``int``>& values,``                 ``vector<``int``>& labels,``                 ``int` `n, ``int` `limit)``{``    ``int` `res = 0;``    ``multimap<``int``, ``int``> s;``    ``unordered_map<``int``, ``int``> map;` `    ``if` `(n == 0)``    ``{``        ``return` `0;``    ``}` `    ``// Pushing the pair into``    ``// the multimap``    ``for` `(``int` `i = 0; i < n; i++)``    ``{``        ``s.insert({ values[i],``                  ``labels[i] });``    ``}` `    ``// Traversing the multimap``    ``// in reverse``    ``for` `(``auto` `it = s.rbegin();``         ``it != s.rend() && n > 0; it++)``    ``{``            ``cout<<(it->first)<<``" "``<second<second] <= limit)``        ``{``            ``res += it->first;``          ``//cout<<"res = "< values = { 5, 3, 7, 1, 2 };``    ``vector<``int``> labels = { 5, 7, 7, 7, 6 };``    ` `    ``int` `n = ``sizeof``(values) / ``sizeof``(values);``    ``int` `limit = 2;``    ` `    ``cout << MaxSumSubset(values, labels,``                         ``n, limit);``    ``return` `0;``}`

## Python3

 `# Python3 program to Find subset with``# maximum sum under given condition.``from` `collections ``import` `defaultdict` `# Function to return the maximum``# sum of the subset``def` `MaxSumSubset(values, labels,``                 ``n, limit):``                     ` `    ``res ``=` `0``    ``s ``=` `{}``    ``map` `=` `defaultdict(``int``)` `    ``if` `(n ``=``=` `0``):``        ``return` `0` `    ``# Pushing the pair into``    ``# the multimap``    ``for` `i ``in` `range``(n):``        ``s[values[i]] ``=` `labels[i]` `    ``# Traversing the multimap``    ``# in reverse``    ``#s = reversed(sorted(s.keys()))``    ``for` `it ``in` `sorted``(s.keys(), reverse ``=` `True``):``        ``if` `n > ``0``:``            ``if` `(``map``[s[it]] < limit):``                ``res ``+``=` `it``                ``#print("res = ",res)` `                ``map``[s[it]] ``+``=` `1` `        ``n ``-``=` `1` `    ``return` `res` `# Driver code``if` `__name__ ``=``=` `"__main__"``:` `    ``values ``=` `[``5``, ``3``, ``7``, ``1``, ``2``]``    ``labels ``=` `[``5``, ``7``, ``7``, ``7``, ``6``]` `    ``n ``=` `len``(values)``    ``limit ``=` `2` `    ``print``(MaxSumSubset(values, labels,``                       ``n, limit))` `# This code is contributed by ukasp`

## Javascript

 ``
Output:

`17`

Time Complexity: O(N), where N is the length of the array.

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