Given a distance ‘dist’, count total number of ways to cover the distance with 1, 2 and 3 steps.
Examples:
Input: n = 3
Output: 4
Explanation:
Below are the four ways
1 step + 1 step + 1 step
1 step + 2 step
2 step + 1 step
3 step
Input: n = 4
Output: 7
Explanation:
Below are the four ways
1 step + 1 step + 1 step + 1 step
1 step + 2 step + 1 step
2 step + 1 step + 1 step
1 step + 1 step + 2 step
2 step + 2 step
3 step + 1 step
1 step + 3 step
Recursive solution
- Approach: There are n stairs, and a person is allowed to next step, skip one position or skip two positions. So there are n positions. The idea is standing at the ith position the person can move by i+1, i+2, i+3 position. So a recursive function can be formed where at current index i the function is recursively called for i+1, i+2 and i+3 positions.
There is another way of forming the recursive function. To reach position i, a person has to jump either from i-1, i-2 or i-3 position where i is the starting position.
- Algorithm:
- Create a recursive function (count(int n)) which takes only one parameter.
- Check the base cases. If the value of n is less than 0 then return 0, and if value of n is equal to zero then return 1 as it is the starting position.
- Call the function recursively with values n-1, n-2 and n-3 and sum up the values that are returned, i.e. sum = count(n-1) + count(n-2) + count(n-3).
- Return the value of sum.
- Implementation:
C++
#include<iostream>
using namespace std;
int printCountRec( int dist)
{
if (dist<0) return 0;
if (dist==0) return 1;
return printCountRec(dist-1) +
printCountRec(dist-2) +
printCountRec(dist-3);
}
int main()
{
int dist = 4;
cout << printCountRec(dist);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int printCountRec( int dist)
{
if (dist< 0 )
return 0 ;
if (dist== 0 )
return 1 ;
return printCountRec(dist- 1 ) +
printCountRec(dist- 2 ) +
printCountRec(dist- 3 );
}
public static void main (String[] args)
{
int dist = 4 ;
System.out.println(printCountRec(dist));
}
}
|
Python3
def printCountRec(dist):
if dist < 0 :
return 0
if dist = = 0 :
return 1
return (printCountRec(dist - 1 ) +
printCountRec(dist - 2 ) +
printCountRec(dist - 3 ))
dist = 4
print (printCountRec(dist))
|
C#
using System;
class GFG {
static int printCountRec( int dist)
{
if (dist < 0)
return 0;
if (dist == 0)
return 1;
return printCountRec(dist - 1) +
printCountRec(dist - 2) +
printCountRec(dist - 3);
}
public static void Main ()
{
int dist = 4;
Console.WriteLine(printCountRec(dist));
}
}
|
PHP
<?php
function printCountRec( $dist )
{
if ( $dist <0) return 0;
if ( $dist ==0) return 1;
return printCountRec( $dist - 1) +
printCountRec( $dist - 2) +
printCountRec( $dist - 3);
}
$dist = 4;
echo printCountRec( $dist );
?>
|
Javascript
<script>
function printCountRec(dist)
{
if (dist<0) return 0;
if (dist==0) return 1;
return printCountRec(dist-1) +
printCountRec(dist-2) +
printCountRec(dist-3);
}
var dist = 4;
document.write(printCountRec(dist));
</script>
|
Output:
7
- Complexity Analysis:
- Time Complexity: O(3n).
The time complexity of the above solution is exponential, a close upper bound is O(3n). From each state 3, a recursive function is called. So the upper bound for n states is O(3n). - Space complexity: O(1).
No extra space is required.
Efficient solution
- Approach: The idea is similar, but it can be observed that there are n states but the recursive function is called 3 ^ n times. That means that some states are called repeatedly. So the idea is to store the value of states. This can be done in two ways.
- Algorithm:
- Create an array of size n + 1 and initialize the first 3 variables with 1, 1, 2. The base cases.
- Run a loop from 3 to n.
- For each index i, compute value of ith position as dp[i] = dp[i-1] + dp[i-2] + dp[i-3].
- Print the value of dp[n], as the Count of number of ways to cover a distance.
- Implementation:
C++
#include<iostream>
using namespace std;
int printCountDP( int dist)
{
int count[dist+1];
count[0] = 1;
if (dist >= 1)
count[1] = 1;
if (dist >= 2)
count[2] = 2;
for ( int i=3; i<=dist; i++)
count[i] = count[i-1] + count[i-2] + count[i-3];
return count[dist];
}
int main()
{
int dist = 4;
cout << printCountDP(dist);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int printCountDP( int dist)
{
int [] count = new int [dist+ 1 ];
count[ 0 ] = 1 ;
if (dist >= 1 )
count[ 1 ] = 1 ;
if (dist >= 2 )
count[ 2 ] = 2 ;
for ( int i= 3 ; i<=dist; i++)
count[i] = count[i- 1 ] + count[i- 2 ] + count[i- 3 ];
return count[dist];
}
public static void main (String[] args)
{
int dist = 4 ;
System.out.println(printCountDP(dist));
}
}
|
Python3
def printCountDP(dist):
count = [ 0 ] * (dist + 1 )
count[ 0 ] = 1
if dist > = 1 :
count[ 1 ] = 1
if dist > = 2 :
count[ 2 ] = 2
for i in range ( 3 , dist + 1 ):
count[i] = (count[i - 1 ] +
count[i - 2 ] + count[i - 3 ])
return count[dist];
dist = 4 ;
print ( printCountDP(dist))
|
C#
using System;
class GFG {
static int printCountDP( int dist)
{
int [] count = new int [dist + 1];
count[0] = 1;
count[1] = 1;
count[2] = 2;
for ( int i = 3; i <= dist; i++)
count[i] = count[i - 1] +
count[i - 2] +
count[i - 3];
return count[dist];
}
public static void Main ()
{
int dist = 4;
Console.WriteLine(printCountDP(dist));
}
}
|
PHP
<?php
function printCountDP( $dist )
{
$count = array ();
$count [0] = 1; $count [1] = 1;
$count [2] = 2;
for ( $i = 3; $i <= $dist ; $i ++)
$count [ $i ] = $count [ $i - 1] +
$count [ $i - 2] +
$count [ $i - 3];
return $count [ $dist ];
}
$dist = 4;
echo printCountDP( $dist );
?>
|
Javascript
<script>
function printCountDP(dist)
{
let count = new Array(dist + 1);
count[0] = 1;
if (dist >= 1)
count[1] = 1;
if (dist >= 2)
count[2] = 2;
for (let i = 3; i <= dist; i++)
count[i] = count[i - 1] +
count[i - 2] +
count[i - 3];
return count[dist];
}
let dist = 4;
document.write(printCountDP(dist));
</script>
|
Output :
7
- Complexity Analysis:
- Time Complexity: O(n).
Only one traversal of the array is needed. So Time Complexity is O(n) - Space complexity: O(n).
To store the values in a DP O(n) extra space is needed.
More Optimal Solution
Approach: Instead of using array of size n+1 we can use array of size 3 because for calculating no of ways for a particular step we need only last 3 steps no of ways.
Algorithm:
- Create an array of size 3 and initialize the values for step 0,1,2 as 1,1,2 (Base cases).
- Run a loop from 3 to n(dist).
- For each index compute the value as ways[i%3] = ways[(i-1)%3] + ways[(i-2)%3] + ways[(i-3)%3] and store its value at i%3 index of array ways. If we are computing value for index 3 then the computed value will go at index 0 because for larger indices(4 ,5,6…..) we don’t need the value of index 0.
- Return the value of ways[n%3].
C++
#include<iostream>
using namespace std;
int printCountDP( int dist)
{
int ways[3] , n = dist;
ways[0] = 1;
ways[1] = 1;
ways[2] = 2;
for ( int i=3 ;i<=n ;i++)
ways[i%3] = ways[(i-1)%3] + ways[(i-2)%3] + ways[(i-3)%3];
return ways[n%3];
}
int main()
{
int dist = 4;
cout << printCountDP(dist);
return 0;
}
|
Java
import java.util.*;
class GFG {
static int printCountDP( int dist)
{
int []ways = new int [ 3 ];
int n = dist;
ways[ 0 ] = 1 ;
ways[ 1 ] = 1 ;
ways[ 2 ] = 2 ;
for ( int i= 3 ;i<=n ;i++)
ways[i% 3 ] = ways[(i- 1 )% 3 ] + ways[(i- 2 )% 3 ] + ways[(i- 3 )% 3 ];
return ways[n% 3 ];
}
public static void main(String arg[])
{
int dist = 4 ;
System.out.print(printCountDP(dist));
}
}
|
Python3
def prCountDP( dist):
ways = [ 0 ] * 3
n = dist
ways[ 0 ] = 1
ways[ 1 ] = 1
ways[ 2 ] = 2
for i in range ( 3 , n + 1 ):
ways[i % 3 ] = ways[(i - 1 ) % 3 ] + ways[(i - 2 ) % 3 ] + ways[(i - 3 ) % 3 ]
return ways[n % 3 ]
dist = 4
print (prCountDP(dist))
|
C#
using System;
class GFG{
static int printCountDP( int dist)
{
int []ways = new int [3];
int n = dist;
ways[0] = 1;
ways[1] = 1;
ways[2] = 2;
for ( int i = 3; i <= n; i++)
ways[i % 3] = ways[(i - 1) % 3] +
ways[(i - 2) % 3] +
ways[(i - 3) % 3];
return ways[n % 3];
}
public static void Main(String []arg)
{
int dist = 4;
Console.Write(printCountDP(dist));
}
}
|
Javascript
<script>
function printCountDP( dist)
{
var ways= [] , n = dist;
ways.length = 3 ;
ways[0] = 1;
ways[1] = 1;
ways[2] = 2;
for ( var i=3 ;i<=n ;i++)
ways[i%3] = ways[(i-1)%3] + ways[(i-2)%3] + ways[(i-3)%3];
return ways[n%3];
}
var dist = 4;
document.write(printCountDP(dist));
</script>
|
Output :
7
Time Complexity : O(n)
Space Complexity : O(1)
This article is contributed by Vignesh Venkatesan. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above