# Print all ways to reach the Nth stair with the jump of 1 or 2 units at a time

• Last Updated : 23 Dec, 2021

Given a positive integer N representing N stairs and a person it at the first stair, the task is to print all ways to reach the Nth stair with the jump of 1 or 2 units at a time.

Examples:

Input: N = 3
Output:
11
2
Explanation:
Nth stairs can be reached in the following ways with the jumps of 1 or 2 units each as:

1. Perform the two jumps of 1 unit each as 1 -> 1.
2. Perform the two jumps of 1 unit each as 2.

Input: N = 5
Output:
1111
112
121
211
22

Approach: The given problem can be solved using Recursion. The idea is to cover both the cases of one or two jumps at a time at each index and print all the possible ways to reach the Nth stair. Follow the steps below to solve the given problem:

• Define a recursive function, say totalPossibleJumps(int N) that returns all possible ways of jumps to reach the Nth stair.
• At the starting point of recursion check for the base cases as:
• If the N < 0, then it is not a valid way. So return an empty array
• If the N = 0, then it is a valid way. So return an array of size 1 containing an empty space.
• Call recursive function two times at each recursion call one for 1 unit jump and another for the 2 unit jump and store results separately.
• Initialize an array of strings, say totalJumps to store ways to reach the ith index and store all possible ways of reaching the index i with the results stored in the above two recursive calls.
• After completing the above steps, print all the possible combinations return by the above recursive calls as totalPossibleJumps(N).

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach``#include ``using` `namespace` `std;` `// Function to find all the ways to reach``// Nth stair using one or two jumps``vector TotalPossibleJumps(``int` `N)``{``    ``// Base Cases``    ``if` `((N - 1) == 0) {``        ``vector newvec;``        ``newvec.push_back(``""``);``        ``return` `newvec;``    ``}``    ``else` `{``        ``if` `(N < 0) {``            ``vector newvec;``            ``return` `newvec;``        ``}``    ``}` `    ``// Recur for jump1 and jump2``    ``vector jump1``        ``= TotalPossibleJumps(N - 1);``    ``vector jump2``        ``= TotalPossibleJumps(N - 2);` `    ``// Stores the total possible jumps``    ``vector totaljumps;` `    ``// Add "1" with every element``    ``// present in jump1``    ``for` `(string s : jump1) {``        ``totaljumps.push_back(``"1"` `+ s);``    ``}` `    ``// Add "2" with every element``    ``// present in jump2``    ``for` `(string s : jump2) {``        ``totaljumps.push_back(``"2"` `+ s);``    ``}` `    ``return` `totaljumps;``}` `// Driver Code``int` `main()``{``    ``int` `N = 3;``    ``vector Ans = TotalPossibleJumps(N);``    ``for` `(``auto``& it : Ans)``        ``cout << it << ``'\n'``;` `    ``return` `0;``}`

## Java

 `// Java program for the above approach``import` `java.util.*;` `class` `GFG {` `    ``// Function to find all the ways to reach``    ``// Nth stair using one or two jumps``    ``static` `ArrayList TotalPossibleJumps(``int` `N)``    ``{``        ``// Base Cases``        ``if` `((N - ``1``) == ``0``) {``            ``ArrayList newvec``                ``= ``new` `ArrayList();``            ``newvec.add(``""``);``            ``return` `newvec;``        ``}``        ``else` `{``            ``if` `(N < ``0``) {``                ``ArrayList newvec``                    ``= ``new` `ArrayList();``                ``return` `newvec;``            ``}``        ``}` `        ``// Recur for jump1 and jump2``        ``ArrayList jump1 = TotalPossibleJumps(N - ``1``);``        ``ArrayList jump2 = TotalPossibleJumps(N - ``2``);` `        ``// Stores the total possible jumps``        ``ArrayList totaljumps``            ``= ``new` `ArrayList();` `        ``// Add "1" with every element``        ``// present in jump1``        ``for` `(String s : jump1) {``            ``totaljumps.add(``"1"` `+ s);``        ``}` `        ``// Add "2" with every element``        ``// present in jump2``        ``for` `(String s : jump2) {``            ``totaljumps.add(``"2"` `+ s);``        ``}` `        ``return` `totaljumps;``    ``}` `    ``// Driver Code``    ``public` `static` `void` `main(String[] args)``    ``{``        ``int` `N = ``3``;``        ``ArrayList Ans = TotalPossibleJumps(N);``        ``for` `(String it : Ans)``            ``System.out.println(it);``    ``}``}` `// This code is contributed by ukasp.`

## Python3

 `# python program for the above approach` `# Function to find all the ways to reach``# Nth stair using one or two jumps`  `def` `TotalPossibleJumps(N):` `    ``# Base Cases``    ``if` `((N ``-` `1``) ``=``=` `0``):``        ``newvec ``=` `[]``        ``newvec.append("")``        ``return` `newvec` `    ``else``:``        ``if` `(N < ``0``):``            ``newvec ``=` `[]``            ``return` `newvec` `    ``# Recur for jump1 and jump2``    ``jump1 ``=` `TotalPossibleJumps(N ``-` `1``)``    ``jump2 ``=` `TotalPossibleJumps(N ``-` `2``)` `    ``# Stores the total possible jumps``    ``totaljumps ``=` `[]` `    ``# Add "1" with every element``    ``# present in jump1``    ``for` `s ``in` `jump1:``        ``totaljumps.append(``"1"` `+` `s)` `    ``# Add "2" with every element``    ``# present in jump2``    ``for` `s ``in` `jump2:``        ``totaljumps.append(``"2"` `+` `s)` `    ``return` `totaljumps`  `# Driver Code``if` `__name__ ``=``=` `"__main__"``:` `    ``N ``=` `3``    ``Ans ``=` `TotalPossibleJumps(N)``    ``for` `it ``in` `Ans:``        ``print``(it)` `    ``# This code is contributed by rakeshsahni`

## C#

 `// C# program for the above approach``using` `System;``using` `System.Collections.Generic;` `class` `GFG{` `// Function to find all the ways to reach``// Nth stair using one or two jumps``static` `List<``string``> TotalPossibleJumps(``int` `N)``{``    ``// Base Cases``    ``if` `((N - 1) == 0) {``        ``List<``string``> newvec = ``new` `List<``string``>();``        ``newvec.Add(``""``);``        ``return` `newvec;``    ``}``    ``else` `{``        ``if` `(N < 0) {``            ``List<``string``> newvec = ``new` `List<``string``>();``            ``return` `newvec;``        ``}``    ``}` `    ``// Recur for jump1 and jump2``    ``List<``string``> jump1``        ``= TotalPossibleJumps(N - 1);``    ``List<``string``> jump2``        ``= TotalPossibleJumps(N - 2);` `    ``// Stores the total possible jumps``    ``List<``string``> totaljumps = ``new` `List<``string``>();` `    ``// Add "1" with every element``    ``// present in jump1``    ``foreach` `(``string` `s ``in` `jump1) {``        ``totaljumps.Add(``"1"` `+ s);``    ``}` `    ``// Add "2" with every element``    ``// present in jump2``    ``foreach` `(``string` `s ``in` `jump2) {``        ``totaljumps.Add(``"2"` `+ s);``    ``}` `    ``return` `totaljumps;``}` `// Driver Code``public` `static` `void` `Main()``{``    ``int` `N = 3;``    ``List<``string``> Ans = TotalPossibleJumps(N);``    ``foreach``(``string` `it ``in` `Ans)``        ``Console.WriteLine(it);``}``}` `// This code is contributed by SURENDRA_GANGWAR.`

## Javascript

 ``

Output:

```11
2```

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

My Personal Notes arrow_drop_up