There are n stairs, and a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does not matter).
Note: The problem is similar to Climbing Stairs - Count ways to reach Nth stair with the only difference that in this problem, we don't have to count those ways which only differ in ordering of the steps.
Examples:
Input: n = 1
Output: 1
Explanation: There is only one way to climb 1 stair.Input: n = 2
Output: 2
Explanation: There are two ways to climb 2 stairs: {1, 1} and {2}.Input: n = 4
Output: 3
Explanation: Three ways to reach 4th stair: {1, 1, 1, 1}, {1, 1, 2} and {2, 2}.Input: n = 5
Output: 3
Explanation: Three ways to reach 5th stair: {1, 1, 1, 1, 1}, {1, 1, 1, 2} and {1, 2, 2}.
Table of Content
[Naive Approach] Using Recursion - O(n) Time and O(n) Space:
To avoid counting ways which only differ in order, we can assume that a person initially takes only steps of size 1 followed by steps of size 2. In other words, once a person takes a step of size 2, he will continue taking steps of size 2 till he reaches the nth stair.
A person can reach nth stair from either (n-1)th stair or from (n-2)th stair. So, there are two cases:
- The person has reached nth step from (n - 1)th step, this means that the last step was of size 1 and all the previous steps should also be of size 1. So, there is only 1 way.
- The person has reached nth step from (n - 2)th step, this means that the last step was of size 2 and the previous steps can either be of size 1 or size 2.
Therefore the Recurrence relation will be:
nthStair(n) = 1 ( last step was of size 1) + nthStair(n - 2) ( last step was of size 2 )
// C++ Program to count the number of ways to reach nth stair
// where ordering does not matter
#include <iostream>
using namespace std;
// function to count the number of ways
int nthStair(int n) {
// If n < 0, it means this is not a valid way so return 0
if(n < 0)
return 0;
// If n = 0, it means we have reached the bottom, so return 1
if(n == 0)
return 1;
// If the last step was of size 1, then we have only 1 way
// If the last step was of size 2, then we can recur from the
// (n - 2)th step, nthStair(n - 2)
return 1 + nthStair(n - 2);
}
int main() {
int n = 5;
cout << nthStair(n);
return 0;
}
// C Program to count the number of ways to reach nth stair
// where ordering does not matter
#include <stdio.h>
// function to count the number of ways
int nthStair(int n) {
// If n < 0, it means this is not a valid way so return 0
if (n < 0)
return 0;
// If n = 0, it means we have reached the bottom, so return 1
if (n == 0)
return 1;
// If the last step was of size 1, then we have only 1 way
// If the last step was of size 2, then we can recur from the
// (n - 2)th step, nthStair(n - 2)
return 1 + nthStair(n - 2);
}
int main() {
int n = 5;
printf("%d\n", nthStair(n));
return 0;
}
// Java Program to count the number of ways to reach nth stair
// where ordering does not matter
class GfG {
// function to count the number of ways
static int nthStair(int n) {
// If n < 0, it means this is not a valid way so return 0
if (n < 0)
return 0;
// If n = 0, it means we have reached the bottom, so return 1
if (n == 0)
return 1;
// If the last step was of size 1, then we have only 1 way
// If the last step was of size 2, then we can recur from the
// (n - 2)th step, nthStair(n - 2)
return 1 + nthStair(n - 2);
}
public static void main(String[] args) {
int n = 5;
System.out.println(nthStair(n));
}
}
# Python Program to count the number of ways to reach nth stair
# where ordering does not matter
# function to count the number of ways
def nthStair(n):
# If n < 0, it means this is not a valid way so return 0
if n < 0:
return 0
# If n = 0, it means we have reached the bottom, so return 1
if n == 0:
return 1
# If the last step was of size 1, then we have only 1 way
# If the last step was of size 2, then we can recur from the
# (n - 2)th step, nthStair(n - 2)
return 1 + nthStair(n - 2)
if __name__ == "__main__":
n = 5
print(nthStair(n))
// C# Program to count the number of ways to reach nth stair
// where ordering does not matter
using System;
class GfG {
// function to count the number of ways
static int nthStair(int n) {
// If n < 0, it means this is not a valid way so return 0
if (n < 0)
return 0;
// If n = 0, it means we have reached the bottom, so return 1
if (n == 0)
return 1;
// If the last step was of size 1, then we have only 1 way
// If the last step was of size 2, then we can recur from the
// (n - 2)th step, nthStair(n - 2)
return 1 + nthStair(n - 2);
}
static void Main() {
int n = 5;
Console.WriteLine(nthStair(n));
}
}
// JavaScript Program to count the number of ways to reach nth stair
// where ordering does not matter
// function to count the number of ways
function nthStair(n) {
// If n < 0, it means this is not a valid way so return 0
if (n < 0)
return 0;
// If n = 0, it means we have reached the bottom, so return 1
if (n === 0)
return 1;
// If the last step was of size 1, then we have only 1 way
// If the last step was of size 2, then we can recur from the
// (n - 2)th step, nthStair(n - 2)
return 1 + nthStair(n - 2);
}
const n = 5;
console.log(nthStair(n));
Output
3
Time Complexity: O(n), as for a given n, we are making recursive calls for (n -2), (n - 4), (n - 6) and so on, until we reach a base case.
Auxiliary Space: O(n), as we are using recursive stack space for function calls.
[Better Approach] Using Dynamic Programming - O(n) Time and O(1) Space
From the recurrence relation ways(n) = 1 + ways(n - 2), we can say that in order to find the number of ways to reach nth stair, we need to know the number of ways to reach (n-2)th stair. So, we can start from the bottom of the stairs and maintain two variables, say last and secondLast to keep track of the number of ways to reach the last two stairs.
// C++ Program to count the number of ways to reach nth stair
// where ordering does not matter
#include <iostream>
using namespace std;
// function to count the number of ways
int nthStair(int n) {
int ans = 0;
int last = 0, secondLast = 0;
// Iterate from bottom to the nth step
for (int i = 0; i <= n; i++) {
// Calculate the number of steps to reach ith stair
ans = 1 + secondLast;
// Update second last with last
secondLast = last;
// Update last with ans
last = ans;
}
return ans;
}
int main() {
int n = 5;
cout << nthStair(n);
return 0;
}
// C Program to count the number of ways to reach nth stair
// where ordering does not matter
#include <stdio.h>
// function to count the number of ways
int nthStair(int n) {
int ans = 0;
int last = 0, secondLast = 0;
// Iterate from bottom to the nth step
for (int i = 0; i <= n; i++) {
// Calculate the number of steps to reach ith stair
ans = 1 + secondLast;
// Update second last with last
secondLast = last;
// Update last with ans
last = ans;
}
return ans;
}
int main() {
int n = 5;
printf("%d\n", nthStair(n));
return 0;
}
// Java Program to count the number of ways to reach nth stair
// where ordering does not matter
class GfG {
// function to count the number of ways
static int nthStair(int n) {
int ans = 0;
int last = 0, secondLast = 0;
// Iterate from bottom to the nth step
for (int i = 0; i <= n; i++) {
// Calculate the number of steps to reach ith stair
ans = 1 + secondLast;
// Update second last with last
secondLast = last;
// Update last with ans
last = ans;
}
return ans;
}
public static void main(String[] args) {
int n = 5;
System.out.println(nthStair(n));
}
}
# Python Program to count the number of ways to reach nth stair
# where ordering does not matter
# function to count the number of ways
def nthStair(n):
ans = 0
last = 0
secondLast = 0
# Iterate from bottom to the nth step
for i in range(n + 1):
# Calculate the number of steps to reach ith stair
ans = 1 + secondLast
# Update second last with last
secondLast = last
# Update last with ans
last = ans
return ans
if __name__ == "__main__":
n = 5
print(nthStair(n))
// C# Program to count the number of ways to reach nth stair
// where ordering does not matter
using System;
class GfG {
// function to count the number of ways
static int nthStair(int n) {
int ans = 0;
int last = 0, secondLast = 0;
// Iterate from bottom to the nth step
for (int i = 0; i <= n; i++) {
// Calculate the number of steps to reach ith stair
ans = 1 + secondLast;
// Update second last with last
secondLast = last;
// Update last with ans
last = ans;
}
return ans;
}
static void Main() {
int n = 5;
Console.WriteLine(nthStair(n));
}
}
// JavaScript Program to count the number of ways to reach nth stair
// where ordering does not matter
// function to count the number of ways
function nthStair(n) {
let ans = 0;
let last = 0, secondLast = 0;
// Iterate from bottom to the nth step
for (let i = 0; i <= n; i++) {
// Calculate the number of steps to reach ith stair
ans = 1 + secondLast;
// Update second last with last
secondLast = last;
// Update last with ans
last = ans;
}
return ans;
}
const n = 5;
console.log(nthStair(n));
Output
3
Time Complexity: O(n), as we are iterating from 0 to n.
Auxiliary Space: O(1), as we are only using two extra variables, last and secondLast.
[Expected Approach] Using Mathematical Formula - O(1) Time and O(1) Space
The basic idea is that we can first try to reach the nth stair using as many steps of size 2 as possible. So, if n is even we can reach nth stair by using (s = n/2) steps of size 2 else if n is odd, we can reach (s = n/2) steps of size 2 and 1 step of size 1. Now, for every subsequent way we can break the steps of size 2 into two steps of size 1.
If n is even,
- The first way will be s steps of size 2 and 0 steps of size 1.
- The second way will be (s – 1) steps of size 2 and 2 steps of size 1 .
- In this way, we will have a total of (s + 1) ways, where the last way will be 0 steps of size 2 and (2 * s) steps of size 1.
If n is odd,
- The first way will be s steps of size 2 and 1 step of size 1.
- The second way will be (s – 1) steps of size 2 and 3 steps of size 1 .
- In this way, we will have a total of (s + 1) ways, where the last way will be 0 steps of size 2 and (2 * s + 1) steps of size 1.
So, in both the cases there are a total of (s + 1) ways to reach the nth stair.
// C++ Program to count the number of ways to reach nth stair
// where ordering does not matter using Mathematical formula
#include <iostream>
using namespace std;
// function to count the number of ways
int nthStair(int n) {
// group n steps in the form of s pairs
int s = n / 2;
return s + 1;
}
int main() {
int n = 5;
cout << nthStair(n);
return 0;
}
// C Program to count the number of ways to reach nth stair
// where ordering does not matter using Mathematical formula
#include <stdio.h>
// function to count the number of ways
int nthStair(int n) {
// group n steps in the form of s pairs
int s = n / 2;
return s + 1;
}
int main() {
int n = 5;
printf("%d\n", nthStair(n));
return 0;
}
// Java Program to count the number of ways to reach nth stair
// where ordering does not matter using Mathematical formula
class GfG {
// function to count the number of ways
static int nthStair(int n) {
// group n steps in the form of s pairs
int s = n / 2;
return s + 1;
}
public static void main(String[] args) {
int n = 5;
System.out.println(nthStair(n));
}
}
# Python Program to count the number of ways to reach nth stair
# where ordering does not matter using Mathematical formula
# function to count the number of ways
def nthStair(n):
# group n steps in the form of s pairs
s = n // 2
return s + 1
if __name__ == "__main__":
n = 5
print(nthStair(n))
// C# Program to count the number of ways to reach nth stair
// where ordering does not matter using Mathematical formula
using System;
class GfG {
// function to count the number of ways
static int nthStair(int n) {
// group n steps in the form of s pairs
int s = n / 2;
return s + 1;
}
static void Main() {
int n = 5;
Console.WriteLine(nthStair(n));
}
}
// JavaScript Program to count the number of ways to reach nth stair
// where ordering does not matter using Mathematical formula
// function to count the number of ways
function nthStair(n) {
// group n steps in the form of s pairs
let s = Math.floor(n / 2);
return s + 1;
}
const n = 5;
console.log(nthStair(n));
Output
3
Time Complexity: O(1)
Auxiliary Space: O(1)