Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added the solution for Count ways to N'th Stair. issue #1455 #11

Merged
merged 1 commit into from
Dec 16, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions Java/cp/Staircase_problem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// This program finds the total number of possible combinations that can be used to
// climb statirs . EG : for 3 stairs ,combination and output will be 1,1,1 , 1,2 , 2,1 i.e 3 .
import java.util.Scanner;

class Staircase_problem{
public static void main(String args[]){
int count_stairs=0;
Scanner input = new Scanner(System.in);
//Asking for number of steps to find combination.
System.out.println("Enter total number of Stairs:");
count_stairs = input.nextInt();
Staircase_problem sp = new Staircase_problem();
// Making object of this class and passing stair number to the function.
int final_steps = sp.possibilities_count(count_stairs);
//Printing the number of combination which is given by the function.
System.out.println("Total Number of possible Combinations = "+final_steps);
}

int possibilities_count(int a){
int result = a;
//Using Recursion to find the total number of stairs as f(a) = f(a-1) + a(a-2)
// where f(a) is the final result
if(result <= 1){
result = 1;
}else{
result = possibilities_count(a-1) + possibilities_count(a-2);
}
return result;
}
}
/* Output
Enter total number of Stairs:
4
Total Number of possible Combinations = 5
*/
// Time Complexity : O(2^n)
// Space Complexity :O(1)
// Created by Shubham Patel on 16-12-2020 on WoC