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

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++
| // C++ program to reach N-th stair // by taking a maximum of K leap #include <bits/stdc++.h>   usingnamespacestd;   intsolve(intN, intK) {       // elements of combo[] stores the no of     // possible ways to reach it by all     // combinations of k leaps or less       intcombo[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(inti = 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(intj = 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     returncombo[N]; }   // Driver Code intmain() {     // N i the no of total stairs     // K is the value of the greatest leap     intN = 29;     intK = 5;       cout << solve(N, K);       Â    return0; }  | 
Java
| // Java program to reach N-th // stair by taking a maximum // of K leapclassGFG{staticintsolve(intN, intK){      // elements of combo[] stores     // the no. of possible ways     // to reach it by all combinations    // of k leaps or less    int[] combo;    combo = newint[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(inti = 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(intj = 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    returncombo[N];}  // Driver Codepublicstaticvoidmain(String args[]){    // N i the no of total stairs    // K is the value of the     // greatest leap    intN = 29;    intK = 5;      System.out.println(solve(N, K));  }}  // This code is contributed // by ankita_saini | 
Python 3
| # Python3 program to reach N-th stair# by taking a maximum of K leap   defsolve(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;     fori inrange(1, K +1) :          #  in this loop we count all possible          # leaps to reach the jth stair with          # the help of ith leap or less          forj inrange(0, N +1) :              # if the leap is not more than the i-j              ifj >=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      returncombo[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#
| // C# program to reach N-th // stair by taking a maximum // of K leapusingSystem;  classGFG{staticintsolve(intN, intK){      // elements of combo[] stores     // the no. of possible ways     // to reach it by all combinations    // of k leaps or less    int[] combo;    combo = newint[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(inti = 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(intj = 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    returncombo[N];}  // Driver CodepublicstaticvoidMain(){    // N i the no of total stairs    // K is the value of the     // greatest leap    intN = 29;    intK = 5;      Console.WriteLine(solve(N, K));}}  // This code is contributed // by Akanksha Rai(Abby_akku) | 
PHP
| <?phperror_reporting(0);// PHP program to reach N-th // stair by taking a maximum // of K leap functionsolve($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;   echosolve($N, $K);     // This code is contributed // by Akanksha Rai(Abby_akku) ?> | 
Javascript
| <script>    // Javascript program to reach N-th     // stair by taking a maximum     // of K leap    Â    functionsolve(N, K)    {          // elements of combo[] stores         // the no. of possible ways         // to reach it by all combinations        // of k leaps or less        let combo = newArray(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        returncombo[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));    Â    // This code is contributed by decode2207.</script> | 
603
Â
Time Complexity: O(N*K) 
Auxiliary Space: O(N)
 
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!
 
				 
					


