Open In App

Climb n-th stair with all jumps from 1 to n allowed (Three Different Approaches)

Improve
Improve
Like Article
Like
Save
Share
Report

A monkey is standing below at a staircase having N steps. Considering it can take a leap of 1 to N steps at a time, calculate how many ways it can reach the top of the staircase?

Examples: 

Input : 2
Output : 2
It can either take (1, 1) or (2) to
reach the top. So, total 2 ways

Input : 3
Output : 4
Possibilities : (1, 1, 1), (1, 2), (2, 1),
(3). So, total 4 ways 

There are 3 different ways to think of the problem. 

  1. In all possible solutions, a step is either stepped on by the monkey or can be skipped. So using the fundamental counting principle, the first step has 2 ways to take part, and for each of these, 2nd step also has 2 ways, and so on. but the last step always has to be stepped on.
  2 x 2 x 2 x .... x 2(N-1 th step) x 1(Nth step) 
  = 2(N-1) different ways. 
  1. Let’s define a function F(n) for the use case. F(n) denotes all possible way to reach from bottom to top of a staircase having N steps, where min leap is 1 step and max leap is N step. Now, for the monkey, the first move it can make is possible in N different ways ( 1 step, 2 steps, 3 steps .. N steps). If it takes the first leap as 1 step, it will be left with N-1 more steps to conquer, which can be achieved in F(N-1) ways. And if it takes the first leap as 2 steps, it will have N-2 steps more to cover, which can be achieved in F(N-2) ways. Putting together,
F(N) = F(N-1) + F(N-2) + F(N-3) + ... + 
                      F(2) + F(1) + F(0) 
Now, 
F(0) = 1
F(1) = 1
F(2) = 2
F(3) = 4

Hence,
F(N) = 1 + 1 + 2 + 4 + ... + F(n-1)
     = 1 + 2^0 + 2^1 + 2^2 + ... + 2^(n-2)
     = 1 + [2^(n-1) - 1]

C++




// C++ program to count total number of ways
// to reach n-th stair with all jumps allowed
#include <iostream>
 
int calculateLeaps(int n)
{
    if (n == 0 || n == 1) {
        return 1;
    }
    else {
        int leaps = 0;
        for (int i = 0; i < n; i++)
            leaps += calculateLeaps(i);
        return leaps;
    }
}
 
// Driver code
int main()
{
    int calculateLeaps(int);
    std::cout << calculateLeaps(4) << std::endl;
    return 0;
}


Java




// Java program to count total number of ways
// to reach n-th stair with all jumps allowed
class GFG {
    static int calculateLeaps(int n)
    {
        if (n == 0 || n == 1) {
            return 1;
        }
        else {
            int leaps = 0;
            for (int i = 0; i < n; i++)
                leaps += calculateLeaps(i);
            return leaps;
        }
    }
 
    // Driver code
    public static void main(String[] args)
    {
        System.out.println(calculateLeaps(4));
    }
}
// This code is contributed by Anant Agarwal.


Python3




# Python program to count
# total number of ways
# to reach n-th stair with
# all jumps allowed
 
def calculateLeaps(n):
    if n == 0 or n == 1:
        return 1;
    else:
        leaps = 0;
        for i in range(0,n):
            leaps = leaps + calculateLeaps(i);
        return leaps;
 
# Driver code
print(calculateLeaps(4));
 
# This code is contributed by mits


C#




// C# program to count total number of ways
// to reach n-th stair with all jumps allowed
using System;
 
class GFG {
 
    // Function to calculate leaps
    static int calculateLeaps(int n)
    {
        if (n == 0 || n == 1) {
            return 1;
        }
        else {
            int leaps = 0;
            for (int i = 0; i < n; i++)
                leaps += calculateLeaps(i);
            return leaps;
        }
    }
 
    // Driver code
    public static void Main()
    {
        Console.WriteLine(calculateLeaps(4));
    }
}
 
// This code is contributed by vt_m.


PHP




<?php
// PHP program to count total
// number of ways to reach
// n-th stair with all
// jumps allowed
 
// function return the
// number of ways
function calculateLeaps($n)
{
    if ($n == 0 || $n == 1)
    {
        return 1;
    }
    else
    {
        $leaps = 0;
        for ($i = 0; $i < $n; $i++)
            $leaps += calculateLeaps($i);
        return $leaps;
    }
}
 
    // Driver Code
    echo calculateLeaps(4), "\n";
 
// This code is contributed by ajit
?>


Javascript




<script>
    // Javascript program to count total number of ways
    // to reach n-th stair with all jumps allowed
     
    // Function to calculate leaps
    function calculateLeaps(n)
    {
        if (n == 0 || n == 1) {
            return 1;
        }
        else {
            let leaps = 0;
            for (let i = 0; i < n; i++)
                leaps += calculateLeaps(i);
            return leaps;
        }
    }
     
    document.write(calculateLeaps(4));
     
</script>


  1. Output:
8

Time Complexity: O(2n)

Auxiliary Space: O(n) due to recursive stack space

2. The above solution can be improved by using Dynamic programming (Bottom-Up Approach)

                              Leaps(3)
                         3/      2|         1\
                     Leaps(0)   Leaps(1)            Leaps(2)
                    /   |   \                   3/       2|     1\
          Leaps(-3) Leaps(-2)  Leaps(-1)   Lepas(-1) Leaps(0) Leaps(1)

C++




// C++ program to count total number of ways
// to reach n-th stair with all jumps allowed
 
#include <iostream>
using namespace std;
 
int calculateLeaps(int n, int dp[]){
    if(n == 0){
        return 1 ;
    }else if(n < 0){
        return 0 ;
    }
     
    if(dp[n] != 0 ){
       return dp[n] ;
    }
 
    int count = 0;
    for(int i = 0 ; i < n ; i++ ){
        count += calculateLeaps(i, dp)  ;
    }
     
    dp[n] = count ;
    return count ;
     
}
int main() {
    int n = 4 ;
     
    int dp[n+1] = {0} ;
     
    cout<<calculateLeaps(n,dp) ;
 
    return 0;
   
}


Java




// Java program to count total number of ways
// to reach n-th stair with all jumps allowed
import java.io.*;
 
class GFG
{
  public static int calculateLeaps(int n, int dp[])
  {
    if(n == 0)
    {
      return 1 ;
    }
    else if(n < 0)
    {
      return 0 ;
    }
 
    if(dp[n] != 0)
    {
      return dp[n] ;
    }
 
    int count = 0;
    for(int i = 0 ; i < n ; i++)
    {
      count += calculateLeaps(i, dp);
    }
 
    dp[n] = count;
    return count;
  }
 
  // Driver Code
  public static void main (String[] args)
  {
    int n = 4;
    int dp[] = new int[n+1];
    System.out.println(calculateLeaps(n,dp));
  }
}
 
// This code is contributed by kothavvsaakash


Python3




# Python program to count total number of ways
# to reach n-th stair with all jumps allowed
def calculateLeaps(n, dp):
 
    if(n == 0):
        return 1
    elif(n < 0):
        return 0
     
    if(dp[n] != 0 ):
       return dp[n]
 
    count = 0
    for i in range(n):
        count += calculateLeaps(i, dp)
     
    dp[n] = count
    return count
     
# driver code
n = 4
dp = [0]*(n+1)
print(calculateLeaps(n,dp))
 
# This code is contributed by shinjanpatra


C#




// C# program to count total number of ways
// to reach n-th stair with all jumps allowed
using System;
 
class GFG
{
  public static int calculateLeaps(int n, int[] dp)
  {
    if(n == 0)
    {
      return 1 ;
    }
    else if(n < 0)
    {
      return 0 ;
    }
 
    if(dp[n] != 0)
    {
      return dp[n] ;
    }
 
    int count = 0;
    for(int i = 0 ; i < n ; i++)
    {
      count += calculateLeaps(i, dp);
    }
 
    dp[n] = count;
    return count;
  }
 
  // Driver Code
  public static void Main (string[] args)
  {
    int n = 4;
    int[] dp = new int[n+1];
    Console.WriteLine(calculateLeaps(n,dp));
  }
}
 
// This code is contributed by phasing17


Javascript




<script>
 
// JavaScript program to count total number of ways
// to reach n-th stair with all jumps allowed
function calculateLeaps(n, dp){
    if(n == 0){
        return 1
    }else if(n < 0){
        return 0
    }
     
    if(dp[n] != 0 ){
       return dp[n]
    }
 
    let count = 0
    for(let i = 0 ; i < n ; i++ ){
        count += calculateLeaps(i, dp)
    }
     
    dp[n] = count
    return count
     
}
 
// driver code
let n = 4
let dp = new Array(n+1).fill(0)
document.write(calculateLeaps(n,dp))
 
// This code is contributed by shinjanpatra
 
</script>


Time Complexity: O(n) // maximum different states

Auxiliary Space : O(n) + O(n) -> O(n) // auxiliary stack space + dp array size

3. Let’s break this problem into small subproblems. The monkey has to step on the last step, the first N-1 steps are optional. The monkey can step on 0 steps before reaching the top step, which is the biggest leap to the top. Or it can decide to step on only once in between, which can be achieved in n-1 ways [ (N-1)C1 ]. And so on, it can step on only 2 steps before reaching the top in (N-1)C2 ways. Putting together..
F(N) = (N-1)C0 + (N-1)C1 + (N-1)C2 + … + (N-1)C(N-2) + (N-1)C(N-1) 
Which is sum of binomial coefficient. 
= 2^(n-1)

C++




// C++ program to count total number of ways
// to reach n-th stair with all jumps allowed
#include <bits/stdc++.h>
using namespace std;
 
     int calculateLeaps(int n)
    {
        if (n == 0)
            return 1;
        return (1 << (n - 1));
    }
 
// Driver code
int main()
{
    int calculateLeaps(int);
    std::cout << calculateLeaps(4) << std::endl;
    return 0;
}
 
// This code is contributed by shivanisinghss2110.


Java




// Java program to count total number of ways
// to reach n-th stair with all jumps allowed
class GFG {
    static int calculateLeaps(int n)
    {
        if (n == 0)
            return 1;
        return (1 << (n - 1));
    }
 
    // Driver code
    public static void main(String[] args)
    {
        System.out.println(calculateLeaps(4));
    }
}
// This code is contributed by Anant Agarwal.


Python3




# python3 program to count
# total number of ways
# to reach n-th stair with
# all jumps allowed
 
def calculateLeaps(n):
    if (n == 0):
        return 1;
    return (1 << (n - 1));
 
# Driver code
print(calculateLeaps(4));
 
# This code is contributed
# by mits


C#




// C# program to count total number of ways
// to reach n-th stair with all jumps allowed
using System;
 
class GFG {
 
    // Function to calculate leaps
    static int calculateLeaps(int n)
    {
        if (n == 0)
            return 1;
        return (1 << (n - 1));
    }
 
    // Driver code
    public static void Main()
    {
        Console.WriteLine(calculateLeaps(4));
    }
}
 
// This code is contributed by vt_m.


PHP




<?php
// PHP program to count total
// number of ways to reach n-th
// stair with all jumps allowed
 
// Function to calculate leaps
function calculateLeaps($n)
{
    if ($n == 0)
        return 1;
    return (1 << ($n - 1));
}
 
// Driver code
echo calculateLeaps(4);
 
// This code is contributed by Sam007
?>


Javascript




<script>
 
// javascript program to count total number of ways
// to reach n-th stair with all jumps allowed
 
function calculateLeaps(n)
{
    if (n == 0)
        return 1;
    return (1 << (n - 1));
}
 
// Driver code
document.write(calculateLeaps(4));
 
// This code is contributed by Amit Katiyar
</script>


Output:  

8

Time Complexity: O(1)

Auxiliary Space: O(1)

 



Last Updated : 20 Jul, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads