r/csinterviewproblems • u/lc929 • Jan 15 '16
Given a value n, find how many different ways you can use the numbers of a set to sum to n.
Given a value n, find how many different ways you can use the numbers of a set to sum to n.
For example, if we had numbers in our set = {1,2,3,4,5,6} and n=10, we could have
1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 = 10
2 + 2 + 2 + 2 + 2 = 10
2 + 2 + 2 + 2 + 1 + 1 = 10
3 + 3 + 2 + 2 = 10
etc.So there would be over 4 ways.
I was thinking of a dynamic programming method, but am not sure how to approach. Perhaps take numbers in the set one at a time?
7
Upvotes
4
u/gluesticktambourine Jan 15 '16 edited Jan 15 '16
I've only started programming this past year so forgive me if this isn't the best way to do it but I think you would have a recursive function f(n, s) that takes n and the set as arguments ,with the base case of if there are no numbers in the set <= n you return 0. The second case is if n is in the set you return 1+ f(n, a new set where that n value is removed) . The third case is if n isn't in the set but a value less than n (let's call it m) is, you would call f(n-m, s) + f(n, new set with m removed). Hopefully that made sense .
Edit: I didn't notice you could repeat numbers from the set so I made that change
4
u/102564 Jan 20 '16
This is the coin change problem. DP solution here
3
u/zhay Jan 15 '16 edited Jan 23 '16
You can solve in O(nk) time with dynamic programming, where n is the number of values in the set and k is the target value. Note: this is pseudo-polynomial time.
The basic idea is to try to select each value from the set (one at a time), subtract that value from the sum remaining, and recurse. I assume you only want to count the number of sets (i.e., combinations, not permutations). Therefore, to make sure you don't count { 1, 2 } as separate from { 2, 1 }, you also maintain a start index, which denotes which value you start from in the set when making the next choice. You can either choose the same value that was selected last time, or you may select a value later in the list.
1
u/_KetzA Jan 15 '16 edited Jan 15 '16
As zhay explained, you take a value of the set, and then you try to achieve n - value with the set recursively. zhay did a good job using dynamic programming. However if you are not really sure on the approach here is a simple/straightforward recursion without dynamic programming. The code is a bit slower but simpler to undestand:
In my _inner function I keep the current sum, and also the "actual" value to make sure that I do not count <1, 2> and <2, 1> as different solutions (assuming that the set is sorted).
There is two base cases : _ either the sum is equal to n, in that case we need to increment the number of solution by one (return 1) _ or the sum is greater than n !, as this solution does not work we need to return 0.
Finally the common case is to pick each number of the set one by one and recurse on sum + number.
0
u/[deleted] Jan 15 '16
[deleted] — view deleted comment