Click here to Skip to main content
15,881,871 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
I have the following denominations 2,3,7. If we pass an input to the function then the function should return the total number of combinations.

For Example I passed the input as 9. Then the function should return 3. Because when we add the denominations
2+2+2+3=9 - Combination 1.
3+3+3=9 - Combination 2.
2+7=9 - Combination 3.
Posted
Comments
Sergey Alexandrovich Kryukov 5-Jul-13 3:54am    
Isn't it a fairly simple problem for someone who published 45 answers and one article?
Please start here: http://whathaveyoutried.com.
—SA
Johnny J. 5-Jul-13 4:06am    
Very good blog post you linked to there! (y)
Sergey Alexandrovich Kryukov 5-Jul-13 4:09am    
:-)

1 solution

The simple brute force approach would be this:

The general combination can be represented as
2A + 3B + 7C
where A, B and C are less or equal to 0 and no more then N/2, N/3 or N/7, where N is the required sum. Then you can list all the combinations in a triple nested loop and check up if each of them fit or not. This process is easy to optimize a bit, as once you found some combination or exceeded the sum, it would not make any sense to increment any of the factors. Also, think about proper ordering of those three loops.

This is the first thing which comes to mind.

—SA
 
Share this answer
 
v3
Comments
thanh_bkhn 5-Jul-13 4:16am    
I'm thinking like that :) :thumbup:
Sergey Alexandrovich Kryukov 5-Jul-13 4:18am    
Sure, thank you. I just added a fix to my statement on optimization, it was inaccurate.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900