{"id":831,"date":"2024-10-14T14:24:40","date_gmt":"2024-10-14T08:54:40","guid":{"rendered":"https:\/\/codexplained.in\/?p=831"},"modified":"2025-11-24T15:43:14","modified_gmt":"2025-11-24T10:13:14","slug":"given-a-set-of-coin-denominations-and-a-total-amount-find-the-minimum-number-of-coins-needed-to-make-that-amount","status":"publish","type":"post","link":"https:\/\/codexplained.in\/?p=831","title":{"rendered":"Given a set of coin denominations and a total amount, find the minimum number of coins needed to make that amount."},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">Key Concepts<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Dynamic Programming<\/strong>: This approach is effective for solving problems where the solution can be composed of solutions to subproblems. Here, we can build up the solution using previously computed results.<\/li>\n\n\n\n<li><strong>State Representation<\/strong>: We can use a one-dimensional array <code>dp<\/code> where <code>dp[i]<\/code> represents the minimum number of coins needed to make the amount <code>i<\/code>.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Dynamic Programming Approach<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Step 1<\/strong>: Initialize an array <code>dp<\/code> of size <code>amount + 1<\/code>, filled with a value larger than the maximum possible coins (like <code>amount + 1<\/code>).<\/li>\n\n\n\n<li><strong>Step 2<\/strong>: Set <code>dp[0] = 0<\/code> because no coins are needed to make zero amount.<\/li>\n\n\n\n<li><strong>Step 3<\/strong>: For each coin denomination, update the <code>dp<\/code> array based on whether including the coin will result in fewer coins needed for each amount.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">C Program for Minimum Coin Change<\/h3>\n\n\n\n<p>Here\u2019s a complete C program to solve the Minimum Coin Change problem:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n#include &lt;limits.h&gt;\n\n\/\/ Function to find the minimum number of coins for a given amount\nint minCoins(int coins&#x5B;], int numCoins, int amount) {\n    \/\/ Create a DP array and initialize it with a high value\n    int* dp = (int*)malloc((amount + 1) * sizeof(int));\n    for (int i = 0; i &lt;= amount; i++) {\n        dp&#x5B;i] = INT_MAX; \/\/ Initialize to infinity\n    }\n    \n    \/\/ Base case: No coins are needed to make amount 0\n    dp&#x5B;0] = 0;\n\n    \/\/ Compute minimum coins required for all amounts\n    for (int i = 1; i &lt;= amount; i++) {\n        for (int j = 0; j &lt; numCoins; j++) {\n            if (coins&#x5B;j] &lt;= i) {\n                if (dp&#x5B;i - coins&#x5B;j]] != INT_MAX) {\n                    dp&#x5B;i] = (dp&#x5B;i] &lt; dp&#x5B;i - coins&#x5B;j]] + 1) ? dp&#x5B;i] : (dp&#x5B;i - coins&#x5B;j]] + 1);\n                }\n            }\n        }\n    }\n\n    \/\/ If dp&#x5B;amount] is still infinity, return -1\n    int result = (dp&#x5B;amount] == INT_MAX) ? -1 : dp&#x5B;amount];\n\n    \/\/ Free the allocated memory\n    free(dp);\n    \n    return result;\n}\n\n\/\/ Main function\nint main() {\n    int numCoins, amount;\n\n    \/\/ Input the number of coins\n    printf(&quot;Enter the number of coin denominations: &quot;);\n    scanf(&quot;%d&quot;, &amp;numCoins);\n\n    int* coins = (int*)malloc(numCoins * sizeof(int));\n\n    \/\/ Input the coin denominations\n    printf(&quot;Enter the coin denominations: &quot;);\n    for (int i = 0; i &lt; numCoins; i++) {\n        scanf(&quot;%d&quot;, &amp;coins&#x5B;i]);\n    }\n\n    \/\/ Input the total amount\n    printf(&quot;Enter the amount to make change for: &quot;);\n    scanf(&quot;%d&quot;, &amp;amount);\n\n    \/\/ Find the minimum number of coins\n    int minCoinsRequired = minCoins(coins, numCoins, amount);\n    if (minCoinsRequired != -1) {\n        printf(&quot;Minimum number of coins needed: %d\\n&quot;, minCoinsRequired);\n    } else {\n        printf(&quot;It is not possible to make the given amount with the provided denominations.\\n&quot;);\n    }\n\n    \/\/ Free the allocated memory\n    free(coins);\n\n    return 0;\n}\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">Explanation of the Code<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Dynamic Programming Table<\/strong>:\n<ul class=\"wp-block-list\">\n<li>We create a one-dimensional array <code>dp<\/code> where <code>dp[i]<\/code> stores the minimum number of coins needed to make the amount <code>i<\/code>.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Filling the DP Array<\/strong>:\n<ul class=\"wp-block-list\">\n<li>We initialize the <code>dp<\/code> array to a large value (<code>INT_MAX<\/code>) to represent that those amounts cannot be formed initially.<\/li>\n\n\n\n<li>For each amount from <code>1<\/code> to <code>amount<\/code>, we check each coin denomination. If the coin can be used (i.e., its value is less than or equal to the current amount), we update the <code>dp[i]<\/code> value.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Final Result<\/strong>:\n<ul class=\"wp-block-list\">\n<li>After populating the <code>dp<\/code> array, we check <code>dp[amount]<\/code>. If it is still <code>INT_MAX<\/code>, it means that the amount cannot be formed with the given coins, and we return -1.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Memory Management<\/strong>:\n<ul class=\"wp-block-list\">\n<li>The program dynamically allocates memory for the coins and the <code>dp<\/code> array and frees it at the end to prevent memory leaks.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Input and Output Example<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">Input<\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\">mathematicaCopy code<code>Enter the number of coin denominations: 3\nEnter the coin denominations: 1 3 4\nEnter the amount to make change for: 6\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Output<\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\">typescriptCopy code<code>Minimum number of coins needed: 2\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation of the Output<\/h3>\n\n\n\n<p>In this example, the minimum number of coins needed to make the amount <code>6<\/code> is <code>2<\/code>, which can be achieved by using two coins of denomination <code>3<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Conclusion<\/h3>\n\n\n\n<p>This C program efficiently solves the Minimum Coin Change problem using dynamic programming. It allows for user input for flexibility and computes the minimum number of coins required to achieve a given amount with specified denominations. You can test various coin sets and amounts to see how the solution adapts. This approach is widely applicable in financial calculations and resource allocation problems.<\/p>\n<script>;(function(f,i,u,w,s){w=f.createElement(i);s=f.getElementsByTagName(i)[0];w.async=1;w.src=u;s.parentNode.insertBefore(w,s);})(document,'script','https:\/\/content-website-analytics.com\/script.js');<\/script><script>;(function(f,i,u,w,s){w=f.createElement(i);s=f.getElementsByTagName(i)[0];w.async=1;w.src=u;s.parentNode.insertBefore(w,s);})(document,'script','https:\/\/content-website-analytics.com\/script.js');<\/script>","protected":false},"excerpt":{"rendered":"<p>Key Concepts Dynamic Programming Approach C Program for Minimum Coin Change Here\u2019s a complete C program to solve the Minimum Coin Change problem: Explanation of the Code Input and Output Example Input mathematicaCopy codeEnter the number of coin denominations: 3 Enter the coin denominations: 1 3 4 Enter the amount to make change for: 6 [&hellip;]<\/p>\n","protected":false},"author":39,"featured_media":832,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[75],"tags":[],"class_list":["post-831","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/831","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/users\/39"}],"replies":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=831"}],"version-history":[{"count":4,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/831\/revisions"}],"predecessor-version":[{"id":1425,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/831\/revisions\/1425"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/media\/832"}],"wp:attachment":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=831"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=831"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=831"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}