{"id":26007,"date":"2017-10-26T09:40:15","date_gmt":"2017-10-26T04:10:15","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=26007"},"modified":"2017-10-26T09:40:15","modified_gmt":"2017-10-26T04:10:15","slug":"c-programming-coin-change","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/c-programming-coin-change\/","title":{"rendered":"C Programming &#8211; Coin Change"},"content":{"rendered":"<p>Given a value N, if we want to make change for N cents, and we have infinite supply of each of S = { S1, S2, .. , Sm} valued coins, how many ways can we make the change? The order of coins doesn\u2019t matter.<span id=\"more-17401\"><\/span><\/p>\n<p>For example, for N = 4 and S = {1,2,3}, there are four solutions: {1,1,1,1},{1,1,2},{2,2},{1,3}. So output should be 4. For N = 10 and S = {2, 5, 3, 6}, there are five solutions: {2,2,2,2,2}, {2,2,3,3}, {2,2,6}, {2,3,5} and {5,5}. So the output should be 5.<\/p>\n<ul>\n<li><strong>Optimal Substructure<\/strong><br \/>\nTo count total number solutions, we can divide all set solutions in two sets.<\/p>\n<ul>\n<li>Solutions that do not contain mth coin (or Sm).<\/li>\n<li>Solutions that contain at least one Sm.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>Let count(S[], m, n) be the function to count the number of solutions, then it can be written as sum of count(S[], m-1, n) and count(S[], m, n-Sm).<\/p>\n<p>Therefore, the problem has optimal substructure property as the problem can be solved using solutions to subproblems.<\/p>\n[ad type=\u201dbanner\u201d]\n<ul>\n<li><strong>Overlapping Subproblems<\/strong><\/li>\n<\/ul>\n<p>Following is a simple recursive implementation of the Coin Change problem. The implementation simply follows the recursive structure mentioned above.<\/p>\n[pastacode lang=\u201dc\u201d manual=\u201d%23include%3Cstdio.h%3E%0A%20%0A%2F%2F%20Returns%20the%20count%20of%20ways%20we%20can%20sum%20%20S%5B0\u2026m-1%5D%20coins%20to%20get%20sum%20n%0Aint%20count(%20int%20S%5B%5D%2C%20int%20m%2C%20int%20n%20)%0A%7B%0A%20%20%20%20%2F%2F%20If%20n%20is%200%20then%20there%20is%201%20solution%20(do%20not%20include%20any%20coin)%0A%20%20%20%20if%20(n%20%3D%3D%200)%0A%20%20%20%20%20%20%20%20return%201%3B%0A%20%20%20%20%20%0A%20%20%20%20%2F%2F%20If%20n%20is%20less%20than%200%20then%20no%20solution%20exists%0A%20%20%20%20if%20(n%20%3C%200)%0A%20%20%20%20%20%20%20%20return%200%3B%0A%20%0A%20%20%20%20%2F%2F%20If%20there%20are%20no%20coins%20and%20n%20is%20greater%20than%200%2C%20then%20no%20solution%20exist%0A%20%20%20%20if%20(m%20%3C%3D0%20%26%26%20n%20%3E%3D%201)%0A%20%20%20%20%20%20%20%20return%200%3B%0A%20%0A%20%20%20%20%2F%2F%20count%20is%20sum%20of%20solutions%20(i)%20including%20S%5Bm-1%5D%20(ii)%20excluding%20S%5Bm-1%5D%0A%20%20%20%20return%20count(%20S%2C%20m%20-%201%2C%20n%20)%20%2B%20count(%20S%2C%20m%2C%20n-S%5Bm-1%5D%20)%3B%0A%7D%0A%20%0A%2F%2F%20Driver%20program%20to%20test%20above%20function%0Aint%20main()%0A%7B%0A%20%20%20%20int%20i%2C%20j%3B%0A%20%20%20%20int%20arr%5B%5D%20%3D%20%7B1%2C%202%2C%203%7D%3B%0A%20%20%20%20int%20m%20%3D%20sizeof(arr)%2Fsizeof(arr%5B0%5D)%3B%0A%20%20%20%20printf(%22%25d%20%22%2C%20count(arr%2C%20m%2C%204))%3B%0A%20%20%20%20getchar()%3B%0A%20%20%20%20return%200%3B%0A%7D\u201d message=\u201dC\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p>It should be noted that the above function computes the same subproblems again and again. See the following recursion tree for S = {1, 2, 3} and n = 5.<br \/>\nThe function C({1}, 3) is called two times. If we draw the complete tree, then we can see that there are many subproblems being called more than once.<\/p>\n<pre>C() --> count()\r\n                              C({1,2,3}, 5)                     \r\n                           \/                \\\r\n                         \/                   \\              \r\n             C({1,2,3}, 2)                 C({1,2}, 5)\r\n            \/     \\                        \/         \\\r\n           \/        \\                     \/           \\\r\nC({1,2,3}, -1)  C({1,2}, 2)        C({1,2}, 3)    C({1}, 5)\r\n               \/     \\            \/    \\            \/     \\\r\n             \/        \\          \/      \\          \/       \\\r\n    C({1,2},0)  C({1},2)   C({1,2},1) C({1},3)    C({1}, 4)  C({}, 5)\r\n                   \/ \\      \/ \\       \/ \\        \/     \\    \r\n                  \/   \\    \/   \\     \/   \\      \/       \\ \r\n                .      .  .     .   .     .   C({1}, 3) C({}, 4)\r\n                                               \/  \\\r\n                                              \/    \\  \r\n                                             .      .\r\n<\/pre>\n<p>Since same suproblems are called again, this problem has Overlapping Subprolems property. So the Coin Change problem has both properties of a dynamic programming problem. Like other typical Dynamic Programming(DP) problems, recomputations of same subproblems can be avoided by constructing a temporary array table[][] in bottom up manner.<\/p>\n[ad type=\u201dbanner\u201d]\n<p><strong>Dynamic Programming Solution<\/strong><\/p>\n[pastacode lang=\u201dc\u201d manual=\u201d%23include%3Cstdio.h%3E%0A%20%0Aint%20count(%20int%20S%5B%5D%2C%20int%20m%2C%20int%20n%20)%0A%7B%0A%20%20%20%20int%20i%2C%20j%2C%20x%2C%20y%3B%0A%20%0A%20%20%20%20%2F%2F%20We%20need%20n%2B1%20rows%20as%20the%20table%20is%20consturcted%20in%20bottom%20up%20manner%20using%20%0A%20%20%20%20%2F%2F%20the%20base%20case%200%20value%20case%20(n%20%3D%200)%0A%20%20%20%20int%20table%5Bn%2B1%5D%5Bm%5D%3B%0A%20%20%20%20%0A%20%20%20%20%2F%2F%20Fill%20the%20enteries%20for%200%20value%20case%20(n%20%3D%200)%0A%20%20%20%20for%20(i%3D0%3B%20i%3Cm%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20table%5B0%5D%5Bi%5D%20%3D%201%3B%0A%20%0A%20%20%20%20%2F%2F%20Fill%20rest%20of%20the%20table%20enteries%20in%20bottom%20up%20manner%20%20%0A%20%20%20%20for%20(i%20%3D%201%3B%20i%20%3C%20n%2B1%3B%20i%2B%2B)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20for%20(j%20%3D%200%3B%20j%20%3C%20m%3B%20j%2B%2B)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20Count%20of%20solutions%20including%20S%5Bj%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20x%20%3D%20(i-S%5Bj%5D%20%3E%3D%200)%3F%20table%5Bi%20-%20S%5Bj%5D%5D%5Bj%5D%3A%200%3B%0A%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20Count%20of%20solutions%20excluding%20S%5Bj%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20y%20%3D%20(j%20%3E%3D%201)%3F%20table%5Bi%5D%5Bj-1%5D%3A%200%3B%0A%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20total%20count%0A%20%20%20%20%20%20%20%20%20%20%20%20table%5Bi%5D%5Bj%5D%20%3D%20x%20%2B%20y%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%20%20%20return%20table%5Bn%5D%5Bm-1%5D%3B%0A%7D%0A%20%0A%2F%2F%20Driver%20program%20to%20test%20above%20function%0Aint%20main()%0A%7B%0A%20%20%20%20int%20arr%5B%5D%20%3D%20%7B1%2C%202%2C%203%7D%3B%0A%20%20%20%20int%20m%20%3D%20sizeof(arr)%2Fsizeof(arr%5B0%5D)%3B%0A%20%20%20%20int%20n%20%3D%204%3B%0A%20%20%20%20printf(%22%20%25d%20%22%2C%20count(arr%2C%20m%2C%20n))%3B%0A%20%20%20%20return%200%3B%0A%7D\u201d message=\u201dC\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p>Output:<\/p>\n<pre>4<\/pre>\n<p>Time Complexity: O(mn)<\/p>\n<p>Following is a simplified version of method 2. The auxiliary space required here is O(n) only.<\/p>\n[pastacode lang=\u201dc\u201d manual=\u201dint%20count(%20int%20S%5B%5D%2C%20int%20m%2C%20int%20n%20)%0A%7B%0A%20%20%20%20%2F%2F%20table%5Bi%5D%20will%20be%20storing%20the%20number%20of%20solutions%20for%0A%20%20%20%20%2F%2F%20value%20i.%20We%20need%20n%2B1%20rows%20as%20the%20table%20is%20consturcted%0A%20%20%20%20%2F%2F%20in%20bottom%20up%20manner%20using%20the%20base%20case%20(n%20%3D%200)%0A%20%20%20%20int%20table%5Bn%2B1%5D%3B%0A%20%0A%20%20%20%20%2F%2F%20Initialize%20all%20table%20values%20as%200%0A%20%20%20%20memset(table%2C%200%2C%20sizeof(table))%3B%0A%20%0A%20%20%20%20%2F%2F%20Base%20case%20(If%20given%20value%20is%200)%0A%20%20%20%20table%5B0%5D%20%3D%201%3B%0A%20%0A%20%20%20%20%2F%2F%20Pick%20all%20coins%20one%20by%20one%20and%20update%20the%20table%5B%5D%20values%0A%20%20%20%20%2F%2F%20after%20the%20index%20greater%20than%20or%20equal%20to%20the%20value%20of%20the%0A%20%20%20%20%2F%2F%20picked%20coin%0A%20%20%20%20for(int%20i%3D0%3B%20i%3Cm%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20for(int%20j%3DS%5Bi%5D%3B%20j%3C%3Dn%3B%20j%2B%2B)%0A%20%20%20%20%20%20%20%20%20%20%20%20table%5Bj%5D%20%2B%3D%20table%5Bj-S%5Bi%5D%5D%3B%0A%20%0A%20%20%20%20return%20table%5Bn%5D%3B%0A%7D\u201d message=\u201dC\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\n","protected":false},"excerpt":{"rendered":"<p>C Programming &#8211; Coin Change &#8211; Dynamic Programming Coin Change problem has both properties of a dynamic programming problem. Like other typical DP problems<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69969,69866,1,70145],"tags":[71524,76604,76613,76611,76608,76609,76614,76610,72847,72842,72845,70483,76605,72841,72848,72840,72849,72846,72843,72854,72844,72850,72839,76607,76612,72852,72855,76606,72853,72851],"class_list":["post-26007","post","type-post","status-publish","format-standard","hentry","category-algorithm","category-c-programming","category-coding","category-dynamic-programming","tag-coin-change","tag-coin-change-in-c","tag-coin-change-problem-algorithm","tag-coin-change-problem-dynamic","tag-coin-change-problem-dynamic-programming-c","tag-coin-change-problem-in-c","tag-coin-change-problem-number-of-ways","tag-coin-change-problem-recursive-solution","tag-concept-of-dynamic-programming","tag-define-dynamic-programming","tag-definition-of-dynamic-programming","tag-dynamic-programming","tag-dynamic-programming-and-coin-change","tag-dynamic-programming-c","tag-dynamic-programming-code-generation-algorithm","tag-dynamic-programming-definition","tag-dynamic-programming-in-c","tag-dynamic-programming-in-data-structure","tag-dynamic-programming-problems","tag-dynamic-programming-set-1","tag-dynamic-programming-software","tag-explain-dynamic-programming","tag-how-to-solve-dynamic-programming-problems","tag-minimum-coin-change-problem-dynamic-programming","tag-number-of-ways-to-make-change","tag-problems-on-dynamic-programming","tag-simple-dynamic-programming-example","tag-the-coin-change-problem","tag-types-of-dynamic-programming","tag-youtube-dynamic-programming"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26007","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=26007"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26007\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=26007"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=26007"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=26007"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}