# Find subset with maximum sum under given condition

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 = 2Output:17Explanation: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 = 2Output: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 occurrence is <= limit.

Below is the implementation of the above approach:

`// C++ program to Find subset with ` `// maximum sum under given condition. ` `#include <bits/stdc++.h> ` `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++) ` ` ` `{ ` ` ` ` ` `if` `(++map[it->second] <= limit) ` ` ` `{ ` ` ` `res += it->first; ` ` ` `n--; ` ` ` `} ` ` ` `} ` ` ` `return` `res; ` `} ` `// Driver code ` `int` `main() ` `{ ` ` ` `vector<` `int` `> values = { 5, 3, 7, 1, 2 }; ` ` ` `vector<` `int` `> labels = { 5, 7, 7, 7, 6 }; ` ` ` ` ` `int` `n = ` `sizeof` `(values) / ` `sizeof` `(values[0]); ` ` ` `int` `limit = 2; ` ` ` ` ` `cout << MaxSumSubset(values, labels, ` ` ` `n, limit); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

17

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

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- Find the maximum subset XOR of a given set
- Find the sum of maximum difference possible from all subset of a given array.
- Maximum size of sub-array that satisfies the given condition
- Maximum path sum for each position with jumps under divisibility condition
- Maximum size subset with given sum
- Maximum size subset with given sum using Backtracking
- Maximum subset sum such that no two elements in set have same digit in them
- Largest subset with maximum difference as 1
- Maximum product subset of an array
- Size of the smallest subset with maximum Bitwise OR
- Maximum sum subset having equal number of positive and negative elements
- Maximum number of elements greater than X after equally distributing subset of array
- Find if there is any subset of size K with 0 sum in an array of -1 and +1
- Find a subset with greatest geometric mean
- Find the Largest divisor Subset in the Array
- Find the smallest positive integer value that cannot be represented as sum of any subset of a given array
- Given a string and an integer k, find the kth sub-string when all the sub-strings are sorted according to the given condition
- Find all unique pairs of maximum and second maximum elements over all sub-arrays in O(NlogN)
- Find whether an array is subset of another array | Added Method 3
- Find whether an array is subset of another array using Map

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.