OpenGenus / cosmos

World's largest Contributor driven code dataset | Used in Quark Search Engine, @OpenGenus IQ, OpenGenus Visual Project
http://internship.opengenus.org
GNU General Public License v3.0
13.56k stars 3.68k forks source link

Dynamic Programming/CoinChange.cpp #1027

Closed saiprasanna94 closed 1 month ago

saiprasanna94 commented 6 years ago

For the following input, int coins[] = {1,2,3}; int amount = 4;

Expected Output : 4 .....({1,1,1,1},{1,1,2},{1,3},{2,2})

Actual Result : 7

Correct me if I am wrong.

Vitorvgc commented 6 years ago

Yes, the code needs fixing. The problem is that current solution is considering different permutations of the same subset as different solutions. This way it is returning 7 because it is counting all these solutions: {1, 1, 1, 1}, {1, 1, 2}, {1, 2, 1}, {2, 1, 1}, {1, 3}, {3, 1}, {2, 2}, while {1, 2, 1}, {2, 1, 1} and {3, 1} shouldn't be counted.

amartyaamp commented 6 years ago

Please check PR #1116 - this fixes the above issue. Also added python implementation.