Print all ways to reach the Nth stair with the jump of 1 or 2 units at a time | Java program for the above approach ; Function to find all the ways to reach Nth stair using one or two jumps ; Base Cases ; Recur for jump1 and jump2 ; Stores the total possible jumps ; Add "1" with every element present in jump1 ; Add "2" with every element present in jump2 ; Driver Code
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 ) ; } }