Open In App

C Program to Count ways to reach the n’th stair

Last Updated : 15 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

There are n stairs and 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.
 

stairs

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 
Examples: 
 

Input: n = 1
Output: 1
There is only one way to climb 1 stair
Input: n = 2
Output: 2
There are two ways: (1, 1) and (2)
Input: n = 4
Output: 5
There are five ways: (1, 1, 1, 1), (1, 1, 2), 
(2, 1, 1), (1, 2, 1), (2, 2)

 

Method 1: Recursion.
Approach: We can easily find the recursive nature in the above problem. The person can reach nth stair from either (n-1)th stair or from (n-2)th stair. Hence, for each stair n, we try to find out the number of ways to reach n-1th stair and n-2th stair and add them to give the answer for the nth stair. Therefore the expression for such an approach comes out to be : 
 

ways(n) = ways(n-1) + ways(n-2)

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). 
 

ways(1) = fib(2) = 1
ways(2) = fib(3) = 2
ways(3) = fib(4) = 3

For a better understanding, let’s refer to the recursion tree below -: 
 

Input: N = 4
                  fib(5)
           '3'  /        \   '2'
               /          \
           fib(4)         fib(3)
     '2'  /      \ '1'   /      \  
         /        \     /        \ 
     fib(3)     fib(2)fib(2)      fib(1) 
     /    \ '1' /   \ '0'
'1' /   '1'\   /     \ 
   /        \ fib(1) fib(0) 
fib(2)     fib(1)

So we can use the function for Fibonacci numbers to find the value of ways(n). 
Following is the implementation of the above idea. 
 

C




// A C program to count number of
// ways to reach n'th stair when
// a person can climb 1, 2, ..m stairs at a time.
#include <stdio.h>
 
// A simple recursive program to find
// n'th fibonacci number
int fib(int n)
{
    if (n <= 1)
        return n;
    return fib(n - 1) + fib(n - 2);
}
 
// Returns number of ways to reach s'th stair
int countWays(int s)
{
    return fib(s + 1);
}
 
// Driver program to test above functions
int main()
{
    int s = 4;
    printf("Number of ways = %d", countWays(s));
    getchar();
    return 0;
}


Output

Number of ways = 5

Complexity Analysis: 
 

  1. As in the above approach we are doing redundant calculations such as finding fib(3) more than ‘1’ number of times. This increases it’s time complexity to exponential.
  2. It can be optimized to work in O(Logn) time using the previously discussed Fibonacci function optimizations.

Generalization of the above problem 
How to count the 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.
Approach: For the generalization of above approach we can use the following recursive relation. 
We can write the recurrence as following. 
 

ways(n, m) = ways(n-1, m) + ways(n-2, m) + 
             ... + ways(n-m, m) 

In this approach to reach a stair-‘n’ we try climbing all possible number of stairs lesser than equal to ‘n’ from present stair. 
 

C




// A C program to count number of
// ways to reach n'th stair when
// a person can climb either 1 or 2
// stairs at a time
#include <stdio.h>
 
// A recursive function used by countWays
int countWaysUtil(int n, int m)
{
    if (n <= 1)
        return n;
    int res = 0;
    for (int i = 1; i <= m && i <= n; i++)
        res += countWaysUtil(n - i, m);
    return res;
}
 
// Returns number of ways to reach s'th stair
int countWays(int s, int m)
{
    return countWaysUtil(s + 1, m);
}
 
// Driver program to test above functions-
int main()
{
    int s = 4, m = 2;
    printf("Number of ways = %d", countWays(s, m));
    return 0;
}


Output

Number of ways = 5

Complexity Analysis: 
 

  • Time Complexity: O(2^n). 
    1. As in the above approach we are doing redundant calculations such as finding fib(3, 2) more than ‘1’ number of times which increases it’s time complexity to exponential.
    2. It can be optimized to O(m*n) by using dynamic programming. We build a table ‘res[]’ in bottom up manner.
  • Auxiliary Space: O(1). 
    o use of any data structure for storing values. 
     

Method 2: Dynamic Programming.
Approach: This method uses the technique of Dynamic Programming to arrive at the solution.
Approach: We create a table res[] in bottom up manner using the following relation: 
 

res[i] = res[i] + res[i-j] for every (i-j) >= 0

such that the ith index of the array will contain the number of ways required to reach the ith step considering all the possibilities of climbing (i.e. from 1 to i).
Below code implements the above approach:
 

C




// A C program to count number
// of ways to reach n'th stair when
// a person can climb 1, 2, ..m
// stairs at a time
#include <stdio.h>
 
// A recursive function used by countWays
int countWaysUtil(int n, int m)
{
    int res[n];
    res[0] = 1;
    res[1] = 1;
    for (int i = 2; i < n; i++) {
        res[i] = 0;
        for (int j = 1; j <= m && j <= i; j++)
            res[i] += res[i - j];
    }
    return res[n - 1];
}
 
// Returns number of ways to reach s'th stair
int countWays(int s, int m)
{
    return countWaysUtil(s + 1, m);
}
 
// Driver program to test above functions
int main()
{
    int s = 4, m = 2;
    printf("Number of ways = %d", countWays(s, m));
    return 0;
}


Output

Number of ways = 5

Complexity Analysis: 
 

  • Time Complexity: O(m*n). 
    As for each stair we check all possibilities ‘m’ and number of stairs is ‘n’.
  • Auxiliary Space:O(n). 
    Use of array for storing mid-way values.

Method 3: Mathematics.

This method uses simple mathematics but this is only applicable if the Order does not matter while counting. Order does not matter means for n = 4 {1 2 1} ,{2 1 1} , {1 1 2} are considered same.

Approach: In this method we simply count the number of sets having 2.

C




// A C program
// to find number of ways
// to reach nth stair
#include <stdio.h>
 
int main() {
    int n;
      n=5;
   
    // Here n/2 is done to count the number 2's in n
    // 1 is added for case where there is no 2.
    // eg: if n=4 ans will be 3.
    // {1,1,1,1} set having no 2.
    // {1,1,2} ans {2,2} (n/2) sets containing 2.
   
      printf("Number of ways when order of steps does not matter: %d", 1+(n/2));
   
    return 0;
}


Output

Number of ways when order of steps does not matter: 3

Complexity Analysis:

  • Time Complexity : O(1)
  • Space Complexity : O(1)
Note: This Method is only applicable for the question Count ways to N’th Stair(Order does not matter) .

Please refer complete article on Count ways to reach the n’th stair for more details!
 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads