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> Â
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 Code int 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); Â
         return 0; } |
Java
// 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));Â
}}Â
// This code is contributed // by ankita_saini |
Python 3
# 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#
// 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));}}Â
// 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 function 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); Â
Â
// 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         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));         // 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!



