Count number of ways to cover a distance

Given a distance ‘dist, count total number of ways to cover the distance with 1, 2 and 3 steps.

**Examples:**

Input:n = 3Output:4Explantion:Below are the four ways 1 step + 1 step + 1 step 1 step + 2 step 2 step + 1 step 3 stepInput:n = 4Output:7Explantion: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*.

- Create a recursive function (
**Implementation:**

## C++

`// A naive recursive C++ program to count number of ways to cover` `// a distance with 1, 2 and 3 steps` `#include<iostream>` `using` `namespace` `std;` `// Returns count of ways to cover 'dist'` `int` `printCountRec(` `int` `dist)` `{` ` ` `// Base cases` ` ` `if` `(dist<0) ` `return` `0;` ` ` `if` `(dist==0) ` `return` `1;` ` ` `// Recur for all previous 3 and add the results` ` ` `return` `printCountRec(dist-1) +` ` ` `printCountRec(dist-2) +` ` ` `printCountRec(dist-3);` `}` `// driver program` `int` `main()` `{` ` ` `int` `dist = 4;` ` ` `cout << printCountRec(dist);` ` ` `return` `0;` `}` |

## Java

`// A naive recursive Java program to count number` `// of ways to cover a distance with 1, 2 and 3 steps` `import` `java.io.*;` `class` `GFG` `{` ` ` `// Function returns count of ways to cover 'dist'` ` ` `static` `int` `printCountRec(` `int` `dist)` ` ` `{` ` ` `// Base cases` ` ` `if` `(dist<` `0` `) ` ` ` `return` `0` `;` ` ` `if` `(dist==` `0` `) ` ` ` `return` `1` `;` ` ` ` ` `// Recur for all previous 3 and add the results` ` ` `return` `printCountRec(dist-` `1` `) +` ` ` `printCountRec(dist-` `2` `) +` ` ` `printCountRec(dist-` `3` `);` ` ` `}` ` ` ` ` `// driver program` ` ` `public` `static` `void` `main (String[] args)` ` ` `{` ` ` `int` `dist = ` `4` `;` ` ` `System.out.println(printCountRec(dist));` ` ` `}` `}` `// This code is contributed by Pramod Kumar` |

## Python3

`# A naive recursive Python3 program` `# to count number of ways to cover` `# a distance with 1, 2 and 3 steps` `# Returns count of ways to` `# cover 'dist'` `def` `printCountRec(dist):` ` ` ` ` `# Base cases` ` ` `if` `dist < ` `0` `:` ` ` `return` `0` ` ` ` ` `if` `dist ` `=` `=` `0` `:` ` ` `return` `1` ` ` `# Recur for all previous 3 and ` ` ` `# add the results` ` ` `return` `(printCountRec(dist` `-` `1` `) ` `+` ` ` `printCountRec(dist` `-` `2` `) ` `+` ` ` `printCountRec(dist` `-` `3` `))` `# Driver code` `dist ` `=` `4` `print` `(printCountRec(dist))` `# This code is contributed by Anant Agarwal.` |

## C#

`// A naive recursive C# program to` `// count number of ways to cover a` `// distance with 1, 2 and 3 steps` `using` `System;` `class` `GFG {` ` ` ` ` `// Function returns count of` ` ` `// ways to cover 'dist'` ` ` `static` `int` `printCountRec(` `int` `dist)` ` ` `{` ` ` `// Base cases` ` ` `if` `(dist < 0)` ` ` `return` `0;` ` ` `if` `(dist == 0)` ` ` `return` `1;` ` ` `// Recur for all previous 3` ` ` `// and add the results` ` ` `return` `printCountRec(dist - 1) +` ` ` `printCountRec(dist - 2) +` ` ` `printCountRec(dist - 3);` ` ` `}` ` ` ` ` `// Driver Code` ` ` `public` `static` `void` `Main ()` ` ` `{` ` ` `int` `dist = 4;` ` ` `Console.WriteLine(printCountRec(dist));` ` ` `}` `}` `// This code is contributed by Sam007.` |

## PHP

`<?php` `// A naive recursive PHP program to` `// count number of ways to cover` `// a distance with 1, 2 and 3 steps` `// Returns count of ways to cover 'dist'` `function` `printCountRec( ` `$dist` `)` `{` ` ` ` ` `// Base cases` ` ` `if` `(` `$dist` `<0) ` `return` `0;` ` ` `if` `(` `$dist` `==0) ` `return` `1;` ` ` `// Recur for all previous 3` ` ` `// and add the results` ` ` `return` `printCountRec(` `$dist` `- 1) +` ` ` `printCountRec(` `$dist` `- 2) +` ` ` `printCountRec(` `$dist` `- 3);` `}` ` ` `// Driver Code` ` ` `$dist` `= 4;` ` ` `echo` `printCountRec(` `$dist` `);` `// This code is contributed by anuj_67.` `?>` |

## Javascript

`<script>` `// A naive recursive javascript program to count number of ways to cover` `// a distance with 1, 2 and 3 steps` `// Returns count of ways to cover 'dist'` `function` `printCountRec(dist)` `{` ` ` `// Base cases` ` ` `if` `(dist<0) ` `return` `0;` ` ` `if` `(dist==0) ` `return` `1;` ` ` `// Recur for all previous 3 and add the results` ` ` `return` `printCountRec(dist-1) +` ` ` `printCountRec(dist-2) +` ` ` `printCountRec(dist-3);` `}` `// driver program` `var` `dist = 4;` `document.write(printCountRec(dist));` `</script>` |

**Output:**

7

**Complexity Analysis:****Time Compelxity:**O(3^{n}).

The time complexity of the above solution is exponential, a close upper bound is O(3^{n}). From each state 3, a recursive function is called. So the upper bound for n states is O(3^{n}).**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.- The first way is to keep the recursive structure intact and just store the value in a HashMap and whenever the function is called, return the value store without computing (Top-Down Approach).
- The second way is to take an extra space of size n and start computing values of states from 1, 2 .. to n, i.e. compute values of i, i+1, i+2 and then use them to calculate the value of i+3 (Bottom-Up Approach).
- Overlapping Subproblems in Dynamic Programming.
- Optimal substructure property in Dynamic Programming.
- Dynamic Programming(DP) problems

**Algorithm:**- Create an array of size n + 1 and initilize the first 3 variables with 1, 1, 2. The base cases.
- Run a loop from 3 to n.
- For each index i, computer 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++

`// A Dynamic Programming based C++ program to count number of ways` `// to cover a distance with 1, 2 and 3 steps` `#include<iostream>` `using` `namespace` `std;` `int` `printCountDP(` `int` `dist)` `{` ` ` `int` `count[dist+1];` ` ` `// Initialize base values. There is one way to cover 0 and 1` ` ` `// distances and two ways to cover 2 distance` ` ` `count[0] = 1;` ` ` `if` `(dist >= 1)` ` ` `count[1] = 1;` ` ` `if` `(dist >= 2)` ` ` `count[2] = 2;` ` ` `// Fill the count array in bottom up manner` ` ` `for` `(` `int` `i=3; i<=dist; i++)` ` ` `count[i] = count[i-1] + count[i-2] + count[i-3];` ` ` `return` `count[dist];` `}` `// driver program` `int` `main()` `{` ` ` `int` `dist = 4;` ` ` `cout << printCountDP(dist);` ` ` `return` `0;` `}` |

## Java

`// A Dynamic Programming based Java program` `// to count number of ways to cover a distance` `// with 1, 2 and 3 steps` `import` `java.io.*;` `class` `GFG` `{` ` ` `// Function returns count of ways to cover 'dist'` ` ` `static` `int` `printCountDP(` `int` `dist)` ` ` `{` ` ` `int` `[] count = ` `new` `int` `[dist+` `1` `];` ` ` ` ` `// Initialize base values. There is one way to` ` ` `// cover 0 and 1 distances and two ways to` ` ` `// cover 2 distance` ` ` `count[` `0` `] = ` `1` `;` ` ` `if` `(dist >= ` `1` `)` ` ` `count[` `1` `] = ` `1` `;` ` ` `if` `(dist >= ` `2` `)` ` ` `count[` `2` `] = ` `2` `;` ` ` ` ` `// Fill the count array in bottom up manner` ` ` `for` `(` `int` `i=` `3` `; i<=dist; i++)` ` ` `count[i] = count[i-` `1` `] + count[i-` `2` `] + count[i-` `3` `];` ` ` ` ` `return` `count[dist];` ` ` `}` ` ` ` ` `// driver program` ` ` `public` `static` `void` `main (String[] args)` ` ` `{` ` ` `int` `dist = ` `4` `;` ` ` `System.out.println(printCountDP(dist));` ` ` `}` `}` `// This code is contributed by Pramod Kumar` |

## Python3

`# A Dynamic Programming based on Python3` `# program to count number of ways to` `# cover a distance with 1, 2 and 3 steps` `def` `printCountDP(dist):` ` ` `count ` `=` `[` `0` `] ` `*` `(dist ` `+` `1` `)` ` ` ` ` `# Initialize base values. There is` ` ` `# one way to cover 0 and 1 distances` ` ` `# and two ways to cover 2 distance` ` ` `count[` `0` `] ` `=` `1` ` ` `if` `dist >` `=` `1` `:` ` ` `count[` `1` `] ` `=` `1` ` ` `if` `dist >` `=` `2` `:` ` ` `count[` `2` `] ` `=` `2` ` ` ` ` `# Fill the count array in bottom` ` ` `# up manner` ` ` `for` `i ` `in` `range` `(` `3` `, dist ` `+` `1` `):` ` ` `count[i] ` `=` `(count[i` `-` `1` `] ` `+` ` ` `count[i` `-` `2` `] ` `+` `count[i` `-` `3` `])` ` ` ` ` `return` `count[dist];` `# driver program` `dist ` `=` `4` `;` `print` `( printCountDP(dist))` `# This code is contributed by Sam007.` |

## C#

`// A Dynamic Programming based C# program` `// to count number of ways to cover a distance` `// with 1, 2 and 3 steps` `using` `System;` `class` `GFG {` ` ` ` ` `// Function returns count of ways` ` ` `// to cover 'dist'` ` ` `static` `int` `printCountDP(` `int` `dist)` ` ` `{` ` ` `int` `[] count = ` `new` `int` `[dist + 1];` ` ` `// Initialize base values. There is one` ` ` `// way to cover 0 and 1 distances` ` ` `// and two ways to cover 2 distance` ` ` `count[0] = 1;` ` ` `count[1] = 1;` ` ` `count[2] = 2;` ` ` `// Fill the count array` ` ` `// in bottom up manner` ` ` `for` `(` `int` `i = 3; i <= dist; i++)` ` ` `count[i] = count[i - 1] +` ` ` `count[i - 2] +` ` ` `count[i - 3];` ` ` `return` `count[dist];` ` ` `}` ` ` ` ` `// Driver Code` ` ` `public` `static` `void` `Main ()` ` ` `{` ` ` `int` `dist = 4;` ` ` `Console.WriteLine(printCountDP(dist));` ` ` `}` `}` `// This code is contributed by Sam007.` |

## PHP

`<?php` `// A Dynamic Programming based PHP program` `// to count number of ways to cover a` `// distance with 1, 2 and 3 steps` `function` `printCountDP( ` `$dist` `)` `{` ` ` `$count` `= ` `array` `();` ` ` `// Initialize base values. There is` ` ` `// one way to cover 0 and 1 distances` ` ` `// and two ways to cover 2 distance` ` ` `$count` `[0] = 1; ` `$count` `[1] = 1;` ` ` `$count` `[2] = 2;` ` ` `// Fill the count array` ` ` `// in bottom up manner` ` ` `for` `( ` `$i` `= 3; ` `$i` `<= ` `$dist` `; ` `$i` `++)` ` ` `$count` `[` `$i` `] = ` `$count` `[` `$i` `- 1] +` ` ` `$count` `[` `$i` `- 2] +` ` ` `$count` `[` `$i` `- 3];` ` ` `return` `$count` `[` `$dist` `];` `}` `// Driver Code` `$dist` `= 4;` `echo` `printCountDP(` `$dist` `);` `// This code is contributed by anuj_67.` `?>` |

## Javascript

`<script>` `// A Dynamic Programming based Javascript program` `// to count number of ways to cover a distance` `// with 1, 2 and 3 steps` ` ` `// Function returns count of ways` `// to cover 'dist'` `function` `printCountDP(dist)` `{` ` ` `let count = ` `new` `Array(dist + 1);` ` ` ` ` `// Initialize base values. There is one` ` ` `// way to cover 0 and 1 distances` ` ` `// and two ways to cover 2 distance` ` ` `count[0] = 1;` ` ` `if` `(dist >= 1)` ` ` `count[1] = 1;` ` ` `if` `(dist >= 2)` ` ` `count[2] = 2;` ` ` `// Fill the count array` ` ` `// in bottom up manner` ` ` `for` `(let i = 3; i <= dist; i++)` ` ` `count[i] = count[i - 1] +` ` ` `count[i - 2] +` ` ` `count[i - 3];` ` ` `return` `count[dist];` `}` `// Driver code` `let dist = 4;` `document.write(printCountDP(dist));` ` ` `// This code is contributed by divyeshrabadiya07` `</script>` |

**Output :**

7

**Complexity Analysis:****Time Compelxity:**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.

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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.