Find K-Sum Subsets
Given a set of positive integers, find all the subsets of the given array that sum up to a number K.
Statement
Given a n
positive integers, find all the possible subsets of integers that sum up to a number K
.
The following example elaborates this statement further:
Example
In the below example, we can see three possible subsets that sum to K = 10
.
Sample input
set_of_integers = {1, 3, 5, 21, 19, 7, 2}
K = 10
Expected output
{{3, 7}, {3, 5, 2}, {1, 7, 2}}
Try it yourself #
#include <iostream>#include <string>#include <vector>#include <unordered_set>using namespace std;vector<unordered_set<int>> GetKSumSubsets(vector<int>& setOfIntegers, int targetSum) {//TODO: Write - Your - Codevector<unordered_set<int>> subsets;return subsets;}
Solution 1
We know that for a set of n
elements, there are subsets. For example, a set with ...
Access this course and 1400+ top-rated courses and projects.