I have a set of integers M and a target sum k. I want to find the subset of M that when added together is the closest to k without going over.
For example:
M = {1, 3, 5, 5, 14}
k = 12
answer = {1, 5, 5}
because 1 + 5 + 5 = 11 and there is no way to make 12.
I have the additional constraint that the subset can contain at most 4 elements.
In my application, the size of |M| can be large (on the order of thousands of elements). If it is not possible to find the optimal answer in a reasonable time, I am interested in solutions that at least give a "good" answer.
Right now I am solving this problem by generating 10,000 random subsets and selecting the closest one, which works better than one might expect but is slow. I'm not sure how far from optimal this actually is, but any insight on that would be interesting to me as well.