{"id":26212,"date":"2017-10-26T20:29:16","date_gmt":"2017-10-26T14:59:16","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=26212"},"modified":"2017-10-26T20:29:16","modified_gmt":"2017-10-26T14:59:16","slug":"c-programming-count-ways-reach-n-stair","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/c-programming-count-ways-reach-n-stair\/","title":{"rendered":"C Programming &#8211; Count ways to reach the n stair"},"content":{"rendered":"<p>There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top.<\/p>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-26216\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/stairs.png\" alt=\"Stairs\" width=\"160\" height=\"176\" \/><\/p>\n<p>Consider the example shown in diagram. The value of n is 3. There are 3 ways to reach the top. The diagram is taken from Easier Fibonacci puzzles<\/p>\n[ad type=&#8221;banner&#8221;]\n<p><strong>More Examples:<\/strong><\/p>\n<pre>Input: n = 1\r\nOutput: 1\r\nThere is only one way to climb 1 stair\r\n\r\nInput: n = 2\r\nOutput: 2\r\nThere are two ways: (1, 1) and (2)\r\n\r\nInput: n = 4\r\nOutput: 5\r\n(1, 1, 1, 1), (1, 1, 2), (2, 1, 1), (1, 2, 1), (2, 2)<\/pre>\n<p>We can easily find recursive nature in above problem. The person can reach n\u2019th stair from either (n-1)\u2019th stair or from (n-2)\u2019th stair. Let the total number of ways to reach n\u2019t stair be \u2018ways(n)\u2019. The value of \u2018ways(n)\u2019 can be written as following.<\/p>\n<pre>    ways(n) = ways(n-1) + ways(n-2)\r\n<\/pre>\n<p>The above expression is actually the expression for Fibonacci numbers, but there is one thing to notice, the value of ways(n) is equal to fibonacci(n+1).<\/p>\n<p>ways(1) = fib(2) = 1<br \/>\nways(2) = fib(3) = 2<br \/>\nways(3) = fib(4) = 3<\/p>\n<p>So we can use function for fibonacci numbers to find the value of ways(n).<\/p>\n[ad type=&#8221;banner&#8221;]\n<p>Following is C implementation of the above idea.<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">C Program<\/span> <\/div> <pre class=\"language-c code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-c code-embed-code\">\/\/ A C program to count number of ways to reach n&#039;t stair when<br\/>\/\/ a person can climb 1, 2, ..m stairs at a time.<br\/>#include&lt;stdio.h&gt;<br\/> <br\/>\/\/ A simple recursive program to find n&#039;th fibonacci number<br\/>int fib(int n)<br\/>{<br\/>   if (n &lt;= 1)<br\/>      return n;<br\/>   return fib(n-1) + fib(n-2);<br\/>}<br\/> <br\/>\/\/ Returns number of ways to reach s&#039;th stair<br\/>int countWays(int s)<br\/>{<br\/>    return fib(s + 1);<br\/>}<br\/> <br\/>\/\/ Driver program to test above functions<br\/>int main ()<br\/>{<br\/>  int s = 4;<br\/>  printf(&quot;Number of ways = %d&quot;, countWays(s));<br\/>  getchar();<br\/>  return 0;<br\/>}<\/code><\/pre> <\/div>\n<p><strong>Output:<\/strong><br \/>\nNumber of ways = 5<br \/>\nThe time complexity of the above implementation is exponential (golden ratio raised to power n). It can be optimized to work in O(Logn) time using the previously discussed Fibonacci function optimizations.<\/p>\n<p>Generalization of the above problem<br \/>\nHow to count number of ways if the person can climb up to m stairs for a given value m? For example if m is 4, the person can climb 1 stair or 2 stairs or 3 stairs or 4 stairs at a time.<\/p>\n<p>We can write the recurrence as following.<\/p>\n<p>ways(n, m) = ways(n-1, m) + ways(n-2, m) + &#8230; ways(n-m, m)<br \/>\nFollowing is C implementation of above recurrence.<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">C Program2<\/span> <\/div> <pre class=\"language-c code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-c code-embed-code\">\/\/ A C program to count number of ways to reach n&#039;t stair when<br\/>\/\/ a person can climb either 1 or 2 stairs at a time<br\/>#include&lt;stdio.h&gt;<br\/> <br\/>\/\/ A recursive function used by countWays<br\/>int countWaysUtil(int n, int m)<br\/>{<br\/>    if (n &lt;= 1)<br\/>        return n;<br\/>    int res = 0;<br\/>    for (int i = 1; i&lt;=m &amp;&amp; i&lt;=n; i++)<br\/>        res += countWaysUtil(n-i, m);<br\/>    return res;<br\/>}<br\/> <br\/>\/\/ Returns number of ways to reach s&#039;th stair<br\/>int countWays(int s, int m)<br\/>{<br\/>    return countWaysUtil(s+1, m);<br\/>}<br\/> <br\/>\/\/ Driver program to test above functions<br\/>int main ()<br\/>{<br\/>    int s = 4, m = 2;<br\/>    printf(&quot;Nuber of ways = %d&quot;, countWays(s, m));<br\/>    return 0;<br\/>}<\/code><\/pre> <\/div>\n<p><strong>Output:<\/strong><\/p>\n<pre>Number of ways = 5<\/pre>\n<p>The time complexity of above solution is exponential. It can be optimized to O(mn) by using dynamic programming. Following is dynamic programming based solution. We build a table res[] in bottom up manner.<\/p>\n[ad type=&#8221;banner&#8221;]\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">C Program<\/span> <\/div> <pre class=\"language-c code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-c code-embed-code\">\/\/ A C program to count number of ways to reach n&#039;t stair when<br\/>\/\/ a person can climb 1, 2, ..m stairs at a time<br\/>#include&lt;stdio.h&gt;<br\/> <br\/>\/\/ A recursive function used by countWays<br\/>int countWaysUtil(int n, int m)<br\/>{<br\/>    int res[n];<br\/>    res[0] = 1; res[1] = 1;<br\/>    for (int i=2; i&lt;n; i++)<br\/>    {<br\/>       res[i] = 0;<br\/>       for (int j=1; j&lt;=m &amp;&amp; j&lt;=i; j++)<br\/>         res[i] += res[i-j];<br\/>    }<br\/>    return res[n-1];<br\/>}<br\/> <br\/>\/\/ Returns number of ways to reach s&#039;th stair<br\/>int countWays(int s, int m)<br\/>{<br\/>    return countWaysUtil(s+1, m);<br\/>}<br\/> <br\/>\/\/ Driver program to test above functions<br\/>int main ()<br\/>{<br\/>    int s = 4, m = 2;<br\/>    printf(&quot;Nuber of ways = %d&quot;, countWays(s, m));<br\/>    return 0;<br\/>}<\/code><\/pre> <\/div>\n<p><strong>Output:<\/strong><\/p>\n<pre>Number of ways = 5<\/pre>\n[ad type=&#8221;banner&#8221;]\n","protected":false},"excerpt":{"rendered":"<p>C Programming &#8211; Count ways to reach the n stair &#8211; Mathematical Algorithms &#8211; There are n stairs, a person standing at the bottom wants to reach the top. <\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69969,69866,1,74058],"tags":[70571,77877,77874,77867,72143,77876,74091,77880,77853,77857,77852,74084,74061,77875,77862,77858,70458,75821,70483,71496,77882,77854,71503,77514,77860,74986,77859,77855,77856,72839,77869,77863,72235,77868,77865,73799,77881,77879,77878,77861,77864,77866,77884,77873,71700,77883,77870,77871,77872,77851],"class_list":["post-26212","post","type-post","status-publish","format-standard","hentry","category-algorithm","category-c-programming","category-coding","category-mathematical-algorithms","tag-0-1-knapsack-problem","tag-3-step-stairs","tag-a-staircase-contains-three-steps","tag-algorithm-programming","tag-algorithm-questions","tag-algorithms-for-interviews-pdf","tag-basic-programming-interview-questions","tag-best-book-for-dynamic-programming","tag-best-book-on-dynamic-programming","tag-climbing-stairs","tag-climbing-stairs-problem","tag-coding-interview-questions","tag-common-programming-interview-questions","tag-computer-programming-tutorial","tag-data-structures-and-algorithms-interview-questions","tag-different-dp","tag-dynamic","tag-dynamic-programing","tag-dynamic-programming","tag-dynamic-programming-algorithm","tag-dynamic-programming-book","tag-dynamic-programming-course","tag-dynamic-programming-explained","tag-dynamic-programming-topcoder","tag-dynamic-tutorial","tag-fibonacci-series-using-dynamic-programming","tag-good-dp","tag-google-programming-interview-questions","tag-how-to-calculate-number-of-steps-in-stairs","tag-how-to-solve-dynamic-programming-problems","tag-interview-programs-in-java","tag-java-algorithm-interview-questions","tag-java-algorithms","tag-java-coding-interview-questions","tag-java-coding-questions","tag-java-data-structures-interview-questions","tag-java-interview-coding-questions","tag-java-interview-programming-questions","tag-java-interview-programs","tag-java-problem-solving-questions","tag-java-programming-interview-questions","tag-java-programming-questions","tag-java-programs-asked-in-interview","tag-java-programs-for-interview","tag-programming-algorithms","tag-programming-questions-in-java","tag-programming-tutorial","tag-step-climber","tag-three-step-stairs","tag-what-is-dp"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26212","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\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=26212"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26212\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=26212"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=26212"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=26212"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}