Print all ways to reach the Nth stair with the jump of 1 or 2 units at a time
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:
- Perform the two jumps of 1 unit each as 1 -> 1.
- 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++
#include <bits/stdc++.h>
using namespace std;
vector<string> TotalPossibleJumps( int N)
{
if ((N - 1) == 0) {
vector<string> newvec;
newvec.push_back( "" );
return newvec;
}
else {
if (N < 0) {
vector<string> newvec;
return newvec;
}
}
vector<string> jump1
= TotalPossibleJumps(N - 1);
vector<string> jump2
= TotalPossibleJumps(N - 2);
vector<string> totaljumps;
for (string s : jump1) {
totaljumps.push_back( "1" + s);
}
for (string s : jump2) {
totaljumps.push_back( "2" + s);
}
return totaljumps;
}
int main()
{
int N = 3;
vector<string> Ans = TotalPossibleJumps(N);
for ( auto & it : Ans)
cout << it << '\n' ;
return 0;
}
|
Java
import java.util.*;
class GFG {
static ArrayList<String> TotalPossibleJumps( int N)
{
if ((N - 1 ) == 0 ) {
ArrayList<String> newvec
= new ArrayList<String>();
newvec.add( "" );
return newvec;
}
else {
if (N < 0 ) {
ArrayList<String> newvec
= new ArrayList<String>();
return newvec;
}
}
ArrayList<String> jump1 = TotalPossibleJumps(N - 1 );
ArrayList<String> jump2 = TotalPossibleJumps(N - 2 );
ArrayList<String> totaljumps
= new ArrayList<String>();
for (String s : jump1) {
totaljumps.add( "1" + s);
}
for (String s : jump2) {
totaljumps.add( "2" + s);
}
return totaljumps;
}
public static void main(String[] args)
{
int N = 3 ;
ArrayList<String> Ans = TotalPossibleJumps(N);
for (String it : Ans)
System.out.println(it);
}
}
|
Python3
def TotalPossibleJumps(N):
if ((N - 1 ) = = 0 ):
newvec = []
newvec.append("")
return newvec
else :
if (N < 0 ):
newvec = []
return newvec
jump1 = TotalPossibleJumps(N - 1 )
jump2 = TotalPossibleJumps(N - 2 )
totaljumps = []
for s in jump1:
totaljumps.append( "1" + s)
for s in jump2:
totaljumps.append( "2" + s)
return totaljumps
if __name__ = = "__main__" :
N = 3
Ans = TotalPossibleJumps(N)
for it in Ans:
print (it)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static List< string > TotalPossibleJumps( int N)
{
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;
}
}
List< string > jump1
= TotalPossibleJumps(N - 1);
List< string > jump2
= TotalPossibleJumps(N - 2);
List< string > totaljumps = new List< string >();
foreach ( string s in jump1) {
totaljumps.Add( "1" + s);
}
foreach ( string s in jump2) {
totaljumps.Add( "2" + s);
}
return totaljumps;
}
public static void Main()
{
int N = 3;
List< string > Ans = TotalPossibleJumps(N);
foreach ( string it in Ans)
Console.WriteLine(it);
}
}
|
Javascript
<script>
const TotalPossibleJumps = (N) => {
if ((N - 1) == 0) {
let newvec = [];
newvec.push( "" );
return newvec;
}
else {
if (N < 0) {
let newvec = [];
return newvec;
}
}
let jump1 = TotalPossibleJumps(N - 1);
let jump2 = TotalPossibleJumps(N - 2);
let totaljumps = [];
for (let s in jump1) {
totaljumps.push( "1" + jump1[s]);
}
for (let s in jump2) {
totaljumps.push( "2" + jump2[s]);
}
return totaljumps;
}
let N = 3;
let Ans = TotalPossibleJumps(N);
for (let it in Ans)
document.write(`${Ans[it]}<br/>`);
</script>
|
Time Complexity: O(2N )
Auxiliary Space: O(N)
Last Updated :
23 Dec, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...