{"id":27209,"date":"2018-01-05T20:31:54","date_gmt":"2018-01-05T15:01:54","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=27209"},"modified":"2018-01-05T20:31:54","modified_gmt":"2018-01-05T15:01:54","slug":"c-programming-count-of-n-digit-numbers-whose-sum-of-digits-equals-to-given-sum","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/c-programming-count-of-n-digit-numbers-whose-sum-of-digits-equals-to-given-sum\/","title":{"rendered":"Cpp Programming &#8211; Count of n digit numbers whose sum of digits equals to given sum"},"content":{"rendered":"<p>Given two integers \u2018n\u2019 and \u2018sum\u2019, find count of all n digit numbers with sum of digits as \u2018sum\u2019. Leading 0\u2019s are not counted as digits.<br \/>\n1 &lt;= n &lt;= 100 and 1 &lt;= sum &lt;= 50000<span id=\"more-135347\"><\/span><\/p>\n<p>Example:<\/p>\n<pre>Input:  n = 2, sum = 2\r\nOutput: 2\r\nExplanation: Numbers are 11 and 20\r\n\r\nInput:  n = 2, sum = 5\r\nOutput: 5\r\nExplanation: Numbers are 14, 23, 32, 41 and 50\r\n\r\nInput:  n = 3, sum = 6\r\nOutput: 21<\/pre>\n<p>The idea is simple, we subtract all values from 0 to 9 from given sum and recur for sum minus that digit. Below is recursive formula.<\/p>\n<pre>    countRec(n, sum) = \u2211countRec(n-1, sum-x)\r\n                            where 0 =&lt; x &lt;= 9 and\r\n                                 sum-x &gt;= 0\r\n\r\n    One important observation is, leading 0's must be\r\n    handled explicitly as they are not counted as digits.\r\n    So our final count can be written as below.\r\n    finalCount(n, sum) = \u2211countRec(n-1, sum-x)\r\n                           where 1 =&lt; x &lt;= 9 and\r\n                                 sum-x &gt;= 0<\/pre>\n[ad type=&#8221;banner&#8221;]\n<p>Below is a simple recursive solution based on above recursive formula.<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">C++<\/span> <\/div> <pre class=\"language-cpp code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-cpp code-embed-code\">\/\/ A recursive program to count numbers with sum<br\/>\/\/ of digits as given &#039;sum&#039;<br\/>#include&lt;bits\/stdc++.h&gt;<br\/>using namespace std;<br\/> <br\/>\/\/ Recursive function to count &#039;n&#039; digit numbers<br\/>\/\/ with sum of digits as &#039;sum&#039;. This function<br\/>\/\/ considers leading 0&#039;s also as digits, that is<br\/>\/\/ why not directly called<br\/>unsigned long long int countRec(int n, int sum)<br\/>{<br\/>    \/\/ Base case<br\/>    if (n == 0)<br\/>       return sum == 0;<br\/> <br\/>    \/\/ Initialize answer<br\/>    unsigned long long int ans = 0;<br\/> <br\/>    \/\/ Traverse through every digit and count<br\/>    \/\/ numbers beginning with it using recursion<br\/>    for (int i=0; i&lt;=9; i++)<br\/>       if (sum-i &gt;= 0)<br\/>          ans += countRec(n-1, sum-i);<br\/> <br\/>    return ans;<br\/>}<br\/> <br\/>\/\/ This is mainly a wrapper over countRec. It<br\/>\/\/ explicitly handles leading digit and calls<br\/>\/\/ countRec() for remaining digits.<br\/>unsigned long long int finalCount(int n, int sum)<br\/>{<br\/>    \/\/ Initialize final answer<br\/>    unsigned long long int ans = 0;<br\/> <br\/>    \/\/ Traverse through every digit from 1 to<br\/>    \/\/ 9 and count numbers beginning with it<br\/>    for (int i = 1; i &lt;= 9; i++)<br\/>      if (sum-i &gt;= 0)<br\/>         ans += countRec(n-1, sum-i);<br\/> <br\/>    return ans;<br\/>}<br\/> <br\/>\/\/ Driver program<br\/>int main()<br\/>{<br\/>    int n = 2, sum = 5;<br\/>    cout &lt;&lt; finalCount(n, sum);<br\/>    return 0;<br\/>}<\/code><\/pre> <\/div>\n<p>Output:<\/p>\n<pre>5<\/pre>\n<p>The time complexity of above solution is exponential. If we draw the complete recursion tree, we can observer that many subproblems are solved again and again. For example, if we start with n = 3 and sum = 10, we can reach n = 1, sum = 8, by considering digit sequences 1,1 or 2, 0.<br \/>\nSince same suproblems are called again, this problem has Overlapping Subprolems property. So min square sum problem has both properties of a dynamic programming problem.<\/p>\n<p>Below is Memoization based the implementation.<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">C++<\/span> <\/div> <pre class=\"language-cpp code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-cpp code-embed-code\">\/\/ A memoization based recursive program to count <br\/>\/\/ numbers with sum of n as given &#039;sum&#039;<br\/>#include&lt;bits\/stdc++.h&gt;<br\/>using namespace std;<br\/> <br\/>\/\/ A lookup table used for memoization<br\/>unsigned long long int lookup[101][50001];<br\/> <br\/>\/\/ Memoizatiob based implementation of recursive<br\/>\/\/ function<br\/>unsigned long long int countRec(int n, int sum)<br\/>{<br\/>    \/\/ Base case<br\/>    if (n == 0)<br\/>       return sum == 0;<br\/> <br\/>    \/\/ If this subproblem is already evaluated,<br\/>    \/\/ return the evaluated value<br\/>    if (lookup[n][sum] != -1)<br\/>       return lookup[n][sum];<br\/> <br\/>    \/\/ Initialize answer<br\/>    unsigned long long int ans = 0;<br\/> <br\/>    \/\/ Traverse through every digit and<br\/>    \/\/ recursively count numbers beginning<br\/>    \/\/ with it<br\/>    for (int i=0; i&lt;10; i++)<br\/>       if (sum-i &gt;= 0)<br\/>          ans += countRec(n-1, sum-i);<br\/> <br\/>    return lookup[n][sum] = ans;<br\/>}<br\/> <br\/>\/\/ This is mainly a wrapper over countRec. It<br\/>\/\/ explicitly handles leading digit and calls<br\/>\/\/ countRec() for remaining n.<br\/>unsigned long long int finalCount(int n, int sum)<br\/>{<br\/>    \/\/ Initialize all entries of lookup table<br\/>    memset(lookup, -1, sizeof lookup);<br\/> <br\/>    \/\/ Initialize final answer<br\/>    unsigned long long int ans = 0;<br\/> <br\/>    \/\/ Traverse through every digit from 1 to<br\/>    \/\/ 9 and count numbers beginning with it<br\/>    for (int i = 1; i &lt;= 9; i++)<br\/>      if (sum-i &gt;= 0)<br\/>         ans += countRec(n-1, sum-i);<br\/>    return ans;<br\/>}<br\/> <br\/>\/\/ Driver program<br\/>int main()<br\/>{<br\/>    int n = 3, sum = 5;<br\/>    cout &lt;&lt; finalCount(n, sum);<br\/>    return 0;<br\/>}<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n<p>Output:<\/p>\n<pre>5<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>C++ Programming &#8211; Count of n digit numbers whose sum of digits equals to given sum &#8211; Dynamic Programming Given two integers \u2018n\u2019 and \u2018sum\u2019, find count of all<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[83515,1,70145],"tags":[81124,72844,72850,72839,81132,81128,81119,72852,81131,72855,72853],"class_list":["post-27209","post","type-post","status-publish","format-standard","hentry","category-c-programming-3","category-coding","category-dynamic-programming","tag-a-problem-from-a-programming-competition","tag-dynamic-programming-software","tag-explain-dynamic-programming","tag-how-to-solve-dynamic-programming-problems","tag-number-of-occurrences-of-the-digit-1-in-the-numbers-from-0-to-n","tag-numbers-of-length-n-and-value-less-than-k","tag-print-all-n-digit-numbers-whose-sum-of-digits-equals-to-given-sum","tag-problems-on-dynamic-programming","tag-program-to-find-sum-of-digits-of-a-number-in-c","tag-simple-dynamic-programming-example","tag-types-of-dynamic-programming"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27209","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=27209"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27209\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=27209"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=27209"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=27209"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}