Number of ways to reach Nth floor by taking at-most K leaps - GeeksforGeeks

31 May, 2021
Given N number of stairs. Also given the number of steps that one can cover at most in one leap (K). The task is to find the number of possible ways one (only consider combinations) can climb to the top of the building in K leaps or less from the ground floor.Examples:  
Input: N = 5, K = 3 Output: 5 To reach stair no-5 we can choose following combination of leaps: 1 1 1 1 1 1 1 1 2 1 2 2 1 1 3 2 3 Therefore the answer is 5.Input: N = 29, K = 5 Output: 603  
 
Let combo[i] be the number of ways to reach the i-th floor. Hence the number of ways to reach combo[i] from combo[j] by taking a leap of i-j will be combo[i] += combo[j]. So iterate for all possible leaps, and for each possible leaps keep adding the possible combinations to the combo array. The final answer will be stored in combo[N]. Below is the implementation of the above approach.  
C++
Java
Python 3
C#
PHP
Javascript
// C++ program to reach N-th stair// by taking a maximum of K leap#include <bits/stdc++.h> using namespace std; int solve(int N, int K){     // elements of combo[] stores the no of    // possible ways to reach it by all    // combinations of k leaps or less     int combo[N + 1] = { 0 };     // assuming leap 0 exist and assigning    // its value to 1 for calculation    combo[0] = 1;     // loop to iterate over all    // possible leaps upto k;    for (int i = 1; i <= K; i++) {         // in this loop we count all possible        // leaps to reach the jth stair with        // the help of ith leap or less        for (int j = 0; j <= N; j++) {             // if the leap is not more than the i-j            if (j >= i) {                 // calculate the value and                // store in combo[j]                // to reuse it for next leap                // calculation for the jth stair                combo[j] += combo[j - i];            }        }    }     // returns the no of possible number    // of leaps to reach the top of    // building of n stairs    return combo[N];} // Driver Codeint main(){    // N i the no of total stairs    // K is the value of the greatest leap    int N = 29;    int K = 5;     cout << solve(N, K);     solve(N, K);    return 0;}
// Java program to reach N-th// stair by taking a maximum// of K leapclass GFG{static int solve(int N, int K){     // elements of combo[] stores    // the no. of possible ways    // to reach it by all combinations    // of k leaps or less    int[] combo;    combo = new int[50];     // assuming leap 0 exist    // and assigning its value    // to 1 for calculation    combo[0] = 1;     // loop to iterate over all    // possible leaps upto k;    for (int i = 1; i <= K; i++)    {         // in this loop we count all        // possible leaps to reach        // the jth stair with the        // help of ith leap or less        for (int j = 0; j <= N; j++)        {             // if the leap is not            // more than the i-j            if (j >= i)            {                 // calculate the value and                // store in combo[j] to                // reuse it for next leap                // calculation for the                // jth stair                combo[j] += combo[j - i];            }        }    }     // returns the no of possible    // number of leaps to reach    // the top of building of    // n stairs    return combo[N];} // Driver Codepublic static void main(String args[]){    // N i the no of total stairs    // K is the value of the    // greatest leap    int N = 29;    int K = 5;     System.out.println(solve(N, K));     solve(N, K);}} // This code is contributed// by ankita_saini
# Python3 program to reach N-th stair# by taking a maximum of K leap def solve(N, K) :     # elements of combo[] stores the no of     # possible ways to reach it by all     # combinations of k leaps or less    combo = [0] * (N + 1)     # assuming leap 0 exist and assigning     # its value to 1 for calculation    combo[0] = 1     # loop to iterate over all     # possible leaps upto k;    for i in range(1, K + 1) :         #  in this loop we count all possible         # leaps to reach the jth stair with         # the help of ith leap or less         for j in range(0, N + 1) :             # if the leap is not more than the i-j             if j >= i :                 # calculate the value and                 # store in combo[j]                 # to reuse it for next leap                 # calculation for the jth stair                combo[j] += combo[j - i]      # returns the no of possible number     # of leaps to reach the top of     # building of n stairs     return combo[N]   # Driver Codeif __name__ == "__main__" :     # N i the no of total stairs     # K is the value of the greatest leap    N, K = 29, 5     print(solve(N, K)) # This code is contributed by ANKITRAI1
// C# program to reach N-th// stair by taking a maximum// of K leapusing System; class GFG{static int solve(int N, int K){     // elements of combo[] stores    // the no. of possible ways    // to reach it by all combinations    // of k leaps or less    int[] combo;    combo = new int[50];     // assuming leap 0 exist    // and assigning its value    // to 1 for calculation    combo[0] = 1;     // loop to iterate over all    // possible leaps upto k;    for (int i = 1; i <= K; i++)    {         // in this loop we count all        // possible leaps to reach        // the jth stair with the        // help of ith leap or less        for (int j = 0; j <= N; j++)        {             // if the leap is not            // more than the i-j            if (j >= i)            {                 // calculate the value and                // store in combo[j] to                // reuse it for next leap                // calculation for the                // jth stair                combo[j] += combo[j - i];            }        }    }     // returns the no of possible    // number of leaps to reach    // the top of building of    // n stairs    return combo[N];} // Driver Codepublic static void Main(){    // N i the no of total stairs    // K is the value of the    // greatest leap    int N = 29;    int K = 5;     Console.WriteLine(solve(N, K));    solve(N, K);}} // This code is contributed// by Akanksha Rai(Abby_akku)
<?phperror_reporting(0);// PHP program to reach N-th// stair by taking a maximum// of K leapfunction solve($N, $K){     // elements of combo[] stores    // the no of possible ways to    // reach it by all combinations    // of k leaps or less    $combo[$N + 1] = array();     // assuming leap 0 exist and    // assigning its value to 1    // for calculation    $combo[0] = 1;         // loop to iterate over all    // possible leaps upto k;    for ($i = 1; $i <= $K; $i++)    {         // in this loop we count        // all possible leaps to        // reach the jth stair with        // the help of ith leap or less        for ($j = 0; $j <= $N; $j++)        {            // if the leap is not            // more than the i-j            if ($j >= $i)            {                 // calculate the value and                // store in combo[j]                // to reuse it for next leap                // calculation for the jth stair                $combo[$j] += $combo[$j - $i];            }        }    }     // returns the no of possible    // number of leaps to reach    // the top of building of n stairs    return $combo[$N];} // Driver Code // N i the no of total stairs// K is the value of the greatest leap$N = 29;$K = 5; echo solve($N, $K); solve($N, $K); // This code is contributed// by Akanksha Rai(Abby_akku)?>
<script>    // Javascript program to reach N-th    // stair by taking a maximum    // of K leap         function solve(N, K)    {         // elements of combo[] stores        // the no. of possible ways        // to reach it by all combinations        // of k leaps or less        let combo = new Array(50);        combo.fill(0);         // assuming leap 0 exist        // and assigning its value        // to 1 for calculation        combo[0] = 1;         // loop to iterate over all        // possible leaps upto k;        for (let i = 1; i <= K; i++)        {             // in this loop we count all            // possible leaps to reach            // the jth stair with the            // help of ith leap or less            for (let j = 0; j <= N; j++)            {                 // if the leap is not                // more than the i-j                if (j >= i)                {                     // calculate the value and                    // store in combo[j] to                    // reuse it for next leap                    // calculation for the                    // jth stair                    combo[j] += combo[j - i];                }            }        }         // returns the no of possible        // number of leaps to reach        // the top of building of        // n stairs        return combo[N];    }         // N i the no of total stairs    // K is the value of the    // greatest leap    let N = 29;    let K = 5;       document.write(solve(N, K));    solve(N, K);         // This code is contributed by decode2207.</script>
603
 
Time Complexity: O(N*K) Auxiliary Space: O(N) 
ankita_saini
Akanksha_Rai
ankthon
decode2207
Algorithms-Dynamic Programming
Combinatorial
Dynamic Programming
Dynamic Programming
Combinatorial
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Find the Number of Permutations that satisfy the given condition in an array
Split array into subarrays at minimum cost by minimizing count of repeating elements in each subarray
Ways to sum to N using Natural Numbers up to K with repetitions allowed
Number of Simple Graph with N Vertices and M Edges
Largest substring with same Characters
0-1 Knapsack Problem | DP-10
Program for Fibonacci numbers
Largest Sum Contiguous Subarray
Longest Common Subsequence | DP-4
Longest Increasing Subsequence | DP-3