Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Count the number of ways to reach Nth stair by taking jumps of 1 to N

  • Last Updated : 10 Jan, 2022

Given an integer N which depicts the number of stairs, the task is to count the number of ways to reach Nth stair by taking jumps of 1 to N.

Examples: 

Input: N = 2
Output: 2
Explanation: Two ways to reach are: (1, 1) & (2)

Input: N = 3
Output: 4

Input: N = 4
Output: 8

 

Approach: In this problem, the number of ways to reach the ith stair is:

Ways to reach ith stair = (Sum of ways to reach stairs 1 to i-1)+1
As for any stair before i, ith stair can be reached in a single jump. And +1 for jumping directly to i.

Now to solve this problem: 

  1. Create a variable sum to store the number of ways to reach on a particular stair. Initialise it with 0.
  2. Run a loop from i=1 to i=N-1 and for each iteration:
    1. Create a variable, say cur to store the number of ways to the current stair. So, cur = sum + 1.
    2. Change sum to sum = sum + cur.
  3. Return sum + 1 after the loop ends as the answer to this problem.

Below is the implementation of the above approach:

C++




// C++ code for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to count the number of ways
// to reach Nth stair
int findWays(int N)
{
 
    int sum = 0;
    for (int i = 1; i < N; i++) {
        int cur = sum + 1;
        sum += cur;
    }
 
    return sum + 1;
}
 
// Driver Code
int main()
{
    int N = 10;
    cout << findWays(N);
}

Java




// Java code for the above approach
import java.util.*;
public class GFG
{
 
  // Function to count the number of ways
  // to reach Nth stair
  static int findWays(int N)
  {
 
    int sum = 0;
    for (int i = 1; i < N; i++) {
      int cur = sum + 1;
      sum += cur;
    }
 
    return sum + 1;
  }
 
  // Driver Code
  public static void main(String args[])
  {
    int N = 10;
    System.out.print(findWays(N));
  }
}
 
// This code is contributed by Samim Hossain Mondal.

Python3




# python3 code for the above approach
 
# Function to count the number of ways
# to reach Nth stair
def findWays(N):
 
    sum = 0
    for i in range(1, N):
        cur = sum + 1
        sum += cur
 
    return sum + 1
 
# Driver Code
if __name__ == "__main__":
 
    N = 10
    print(findWays(N))
 
# This code is contributed by rakeshsahni

C#




// C# code to implement above approach
using System;
class GFG
{
 
  // Function to count the number of ways
  // to reach Nth stair
  static int findWays(int N)
  {
 
    int sum = 0;
    for (int i = 1; i < N; i++) {
      int cur = sum + 1;
      sum += cur;
    }
 
    return sum + 1;
  }
 
  // Driver code
  public static void Main()
  {
    int N = 10;
    Console.Write(findWays(N));
  }
}
 
// This code is contributed by Samim Hossain Mondal.

Javascript




<script>
      // JavaScript code for the above approach
 
      // Function to count the number of ways
      // to reach Nth stair
      function findWays(N) {
 
          let sum = 0;
          for (let i = 1; i < N; i++) {
              let cur = sum + 1;
              sum += cur;
          }
 
          return sum + 1;
      }
 
      // Driver Code
 
      let N = 10;
      document.write(findWays(N));
 
// This code is contributed by Potta Lokesh
  </script>

 
 

Output
512

 

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

 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!