Climbing Stairs Problem using Mathematics

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

Order does not matter means for n = 4  {1 2 1}  ,{2 1 1} , {1 1 2} are considered same.

In this method, simply count the number of sets having 2, which will be equal to n/2. So total number of ways will be n/2+1 (one is added to incude the set which does not contain any 2)

Below is the implementation of above approach:

C++
#include <iostream>
using namespace std;

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.

    cout << "Number of ways when order of steps does not "
            "matter is : "
         << 1 + (n / 2) << endl;

    return 0;
}
Java
import java.util.*;

class GFG {

    public static void main(String[] args)
    {
        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.
        System.out.print(
            "Number of ways when order of steps "
            + "does not matter is : " + (1 + (n / 2)));
    }
}

// This code is contributed by todaysgaurav
Python
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.
print("Number of ways when order "
      "of steps does not matter is : ", 1 + (n // 2))

# This code is contributed by rohitsingh07052
C#
using System;

class GFG {
    static public void 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.
        Console.WriteLine(
            "Number of ways when order of steps "
            + "does not matter is : " + (1 + (n / 2)));
    }
}

// This code is contributed by Ankita saini
Javascript
var 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. 
console.log("Number of ways when order " + 
               "of steps does not matter is : ", 
               parseInt(1 + (n / 2)));    

// This code is contributed by Ankita saini

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

Time Complexity : O(1)
Auxiliary Space : O(1)

Climbing Stairs to reach at the top.

There are n stairs, a person standing at the bottom wants to climb stairs to reach the nth stair. The person can climb either 1 stair or 2 stairs at a time, the task is to count the number of ways that a person can reach at the top.
 


Consider the example shown in the 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 (1, 1, 1, 1), (1, 1, 2), (2, 1, 1), (1, 2, 1), (2, 2)

Similar Reads

Climbing Stairs using Recursion:

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 Recurrence relation for such an approach comes out to be :...

Climbing Stairs Problem using Dynamic Programming (Memoization) :

The above recursive solution has Optimal Substructure and Overlapping Subproblems so Dynamic programming (Memoization) can be used to solve the problem. So a 1D array can be used to store results of previously solved subproblems....

Climbing Stairs using Dynamic Programming (Tabulation):

Create a table dp[] in bottom up manner using the following relation:...

Climbing Stairs Problem using the Space Optimized approach:

In the above approach it can be seen that dp[i] only depends on previous states. We can optimize the space complexity of the dynamic programming solution to O(1) by using only two variables prev1 and prev2 to keep track of the previous two values of dp[i-1] and dp[i-2]. Since we only need these two values to calculate the next value, we don’t need to store the entire array....

Climbing Stairs Problem using Mathematics:

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

Climbing Stairs Problem using Matrix Exponentiation:

Approach: The number of ways to reach nth stair (Order matters) is equal to the sum of number of ways to reach (n-1)th stair and (n-2)th stair...