This is a Facebook interview question I came across at an online portal.
Given a set S, find all the maximal subsets whose sum <= k. For example, if S = {1, 2, 3, 4, 5} and k = 7 Output is: {1, 2, 3} {1, 2, 4} {1, 5} {2, 5} {3, 4}
Hints:
- Output doesn't contain any set which is a subset of other.
- If X = {1, 2, 3} is one of the solution then all the subsets of X {1} {2} {3} {1, 2} {1, 3} {2, 3} are omitted.
- Lexicographic ordering may be used to solve it.
Any ideas how could this be solved?
I know it's late to answer, but I think I've found a simple solution for this problem. We enumerate subsets of
S
in lexicographical order using backtracking and check thesum
of subset generated so far.When the
sum
exceedsk
, the interesting part comes: we need to check if the generatedsubset
is a proper subset of previously reported items.One solution is to keep all the reported subsets and check for inclusion, but it's wasteful.
Instead, we calculate the difference between the
k
and thesum
. If there is an elemente
inS
such thate not in subset
ande <= (k - sum)
, then the set we generated is a proper subset of a previously reported subset, and we can safely skip it.Here is the complete working program in plain old C++, demonstrating the idea:
The output is all maximum subsets in lexicographical order, one per line:
I am sorry for chipping in so late. But how about doing this?
1) Build a MIN-HEAP structure from the given array/set
2) traverse the structure from the root and keep subtracting the value at the node that you visit. Once you exceed the required sum (curr_sum > k), output this path, backtrack to the parent and take another path (this can be done recursively).
3) If backtracking takes you back to the original node that you started from, implement the entire algorithm recursively from root->left node.
4) Do the same two steps (2) and (3) above but with a MAX-HEAP now.
I am new to algorithms and data structures, and have only started reading Intro to Algos-Cormen. This might be a faulty solution, but I would be more than happy if anyone points out the fault to me :)
This is a powerset problem. Recently I found this website about algorithms and it's been painting my imagination: hence the powerset/combinations solution following. You can simply copy, paste, and run the program.
An old question but still an interesting one.
Here's a recursive Java 8 solution, with a "permutational" approach.
Optimized for cleaner and shorter code rather than performance -- for example the sorting and pruning would only need to take place once.
To test it:
Algorithm is the following:
Working solution on Java is below:
I have some idea - you need a tree.
If you have given input of
{1, 2, 3, 4, 5}
, and you're searching for maximal subsets - you should build a tree starting from the biggest numbers, and allways expandwhile sum <= k
(so don't stop on 4-2, but go down to 1 to get 4-2-1).So, nodes starting from 5 would be: 5-1 / 5-2 - only those 2 have sum <= 7
starting from 4: 4-3 / 4-2-1 / 4-1 (subset of previous)
starting from 3: 3-2-1 / 3-1 (subset of previous)
starting from 2: 2-1 (subset of 3-2-1)
starting from 1: 1 (subset of 2-1)
Then you can sort valid outputs and get {1, 2, 3} {1, 2, 4} {1, 5} {2, 5} {3, 4}