Given the number N which denotes the number of stairs, the task is to reach the Nth stair by taking 1, 2 steps any number of times and taking a step of 3 exactly once.
Examples:
Input: N = 4
Output: 2
Explanation:
Since a step of 3 has to be taken compulsorily and only once,
There are only two possible ways: (1, 3) or (3, 1)
Input: N = 5
Output: 5
Explanation:
Since a step of 3 has to be taken compulsorily and only once,
There are only 5 possible ways:
(1, 1, 3), (1, 3, 1), (3, 1, 1), (2, 3), (3, 2)
Approach: This problem can be solved using Dynamic Programming. To Reach Nth stair the person should be on (N – 1)th, (N – 2)th or (N – 3)th. So, To reach Nth stair from the base Reach (N – 1)th stair which includes exact only one step of 3, Reach (N – 2)th step with the steps which includes exactly one step of 3 and Reach the (N – 3)th step without taking any step of 3.
So, The Recurrence Relation for this problem will be –
includes_3[i] = includes_3[i-1] + includes_3[i-2] + not_includes[i-3]
whereas, the Recurrence relation when the 3 number of steps at a time is not allowed is
not_includes[i] = not_includes[i – 1] + not_includes[i – 2]
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
int number_of_ways( int n)
{
int includes_3[n + 1] = {};
int not_includes_3[n + 1] = {};
includes_3[3] = 1;
not_includes_3[1] = 1;
not_includes_3[2] = 2;
not_includes_3[3] = 3;
for ( int i = 4; i <= n; i++) {
includes_3[i]
= includes_3[i - 1] + includes_3[i - 2] + not_includes_3[i - 3];
not_includes_3[i]
= not_includes_3[i - 1] + not_includes_3[i - 2];
}
return includes_3[n];
}
int main()
{
int n = 7;
cout << number_of_ways(n);
return 0;
}
|
Java
class GFG
{
static int number_of_ways( int n)
{
int []includes_3 = new int [n + 1 ];
int []not_includes_3 = new int [n + 1 ];
includes_3[ 3 ] = 1 ;
not_includes_3[ 1 ] = 1 ;
not_includes_3[ 2 ] = 2 ;
not_includes_3[ 3 ] = 3 ;
for ( int i = 4 ; i <= n; i++)
{
includes_3[i]
= includes_3[i - 1 ] + includes_3[i - 2 ] +
not_includes_3[i - 3 ];
not_includes_3[i]
= not_includes_3[i - 1 ] + not_includes_3[i - 2 ];
}
return includes_3[n];
}
public static void main(String[] args)
{
int n = 7 ;
System.out.print(number_of_ways(n));
}
}
|
Python3
def number_of_ways(n):
includes_3 = [ 0 ] * (n + 1 )
not_includes_3 = [ 0 ] * (n + 1 )
includes_3[ 3 ] = 1
not_includes_3[ 1 ] = 1
not_includes_3[ 2 ] = 2
not_includes_3[ 3 ] = 3
for i in range ( 4 , n + 1 ):
includes_3[i] = includes_3[i - 1 ] + \
includes_3[i - 2 ] + \
not_includes_3[i - 3 ]
not_includes_3[i] = not_includes_3[i - 1 ] + \
not_includes_3[i - 2 ]
return includes_3[n]
n = 7
print (number_of_ways(n))
|
C#
using System;
class GFG
{
static int number_of_ways( int n)
{
int []includes_3 = new int [n + 1];
int []not_includes_3 = new int [n + 1];
includes_3[3] = 1;
not_includes_3[1] = 1;
not_includes_3[2] = 2;
not_includes_3[3] = 3;
for ( int i = 4; i <= n; i++)
{
includes_3[i]
= includes_3[i - 1] + includes_3[i - 2] +
not_includes_3[i - 3];
not_includes_3[i]
= not_includes_3[i - 1] + not_includes_3[i - 2];
}
return includes_3[n];
}
public static void Main(String[] args)
{
int n = 7;
Console.Write(number_of_ways(n));
}
}
|
Javascript
<script>
function number_of_ways(n)
{
let includes_3 = new Uint8Array(n + 1);
let not_includes_3 = new Uint8Array(n + 1);
includes_3[3] = 1;
not_includes_3[1] = 1;
not_includes_3[2] = 2;
not_includes_3[3] = 3;
for (let i = 4; i <= n; i++) {
includes_3[i]
= includes_3[i - 1] + includes_3[i - 2] + not_includes_3[i - 3];
not_includes_3[i]
= not_includes_3[i - 1] + not_includes_3[i - 2];
}
return includes_3[n];
}
let n = 7;
document.write(number_of_ways(n));
</script>
|
Time complexity: O(n) where N is given the number of stairs
Auxiliary space: O(n)
Efficient approach: Space Optimization O(1)
The above approach includes_3 and not_includes_3, each of size n + 1. However, we can observe that at any given point in the loop, we only need the values of the arrays for the current iteration and the previous two iterations. So to optimize the space complexity, we can replace the arrays with three variables that store the necessary values.
Implementation Steps:
- Initialize variables: in1, in2, in3, incurr, not_in1, not_in2, not_in3, not_incurr.
- Set the initial value of in3 to 1.
- Iterate from i = 4 to n.
- Calculate the current value of incurr by summing the previous values of in3, in2, and not_in1.
- Calculate the current value of not_incurr by summing the previous values of not_in3 and not_in2.
- Update variables for the next iteration by shifting the values.
- Return the final value of incurr.
Implementation:
C++
#include <iostream>
using namespace std;
int number_of_ways( int n)
{
int in1, in2 = 0, in3, incurr;
int not_in1 = 1, not_in2 = 2, not_in3 = 3, not_incurr;
in3 = 1;
for ( int i = 4; i <= n; i++) {
incurr = in3 + in2 + not_in1;
not_incurr = not_in3 + not_in2;
in1 = in2;
in2 = in3;
in3 = incurr;
not_in1 = not_in2;
not_in2 = not_in3;
not_in3 = not_incurr;
}
return incurr;
}
int main()
{
int n = 7;
cout << number_of_ways(n);
return 0;
}
|
Python3
def number_of_ways(n):
in1, in2 = 0 , 0
in3, incurr = 1 , 0
not_in1, not_in2, not_in3, not_incurr = 1 , 2 , 3 , 0
in3 = 1
for i in range ( 4 , n + 1 ):
incurr = in3 + in2 + not_in1
not_incurr = not_in3 + not_in2
in1, in2, in3 = in2, in3, incurr
not_in1, not_in2, not_in3 = not_in2, not_in3, not_incurr
return incurr
def main():
n = 7
print (number_of_ways(n))
main()
|
C#
using System;
class Program {
static int NumberOfWays( int n)
{
int in2 = 0, in3 = 1, incurr;
int not_in1 = 1, not_in2 = 2, not_in3 = 3,
not_incurr;
incurr = 0;
not_incurr = 0;
for ( int i = 4; i <= n; i++) {
incurr = in3 + in2 + not_in1;
not_incurr = not_in3 + not_in2;
in2 = in3;
in3 = incurr;
not_in1 = not_in2;
not_in2 = not_in3;
not_in3 = not_incurr;
}
return incurr;
}
static void Main()
{
int n = 7;
Console.WriteLine(
NumberOfWays(n));
}
}
|
Javascript
function numberOfWays(n) {
let in1, in2 = 0, in3, inCurr;
let not_in1 = 1, not_in2 = 2, not_in3 = 3, not_inCurr;
in3 = 1;
for (let i = 4; i <= n; i++) {
inCurr = in3 + in2 + not_in1;
not_inCurr = not_in3 + not_in2;
in1 = in2;
in2 = in3;
in3 = inCurr;
not_in1 = not_in2;
not_in2 = not_in3;
not_in3 = not_inCurr;
}
return inCurr;
}
function main() {
const n = 7;
console.log(numberOfWays(n));
}
main();
|
Output:
20
Time complexity: O(n) where N is given the number of stairs
Auxiliary space: O(1)
Similar Article: Count ways to reach Nth Stair using 1, 2 or 3 steps at a time