Count number of ways to cover a distance | Set 2

Given a distance N. The task is to count the total number of ways to cover the distance with 1, 2 and 3 steps.

Examples:

Input: N = 3
Output: 4
All the required ways are (1 + 1 + 1), (1 + 2), (2 + 1) and (3).

Input: N = 4
Output: 7

Approach: In previous article, a recursive and dynamic programming based approach has been discussed. Here we will reduce the space complexity. It can be onserved that to calculate the number of steps to cover the distance i, only the last three states are required (i – 1, i – 2, i – 3). So, the result can be calculated using the last three states.



Below is the implementation of the above approach:

CPP

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <iostream>
using namespace std;
  
// Function to return the count of the
// total number of ways to cover the
// distance with 1, 2 and 3 steps
int countWays(int n)
{
    // Base conditions
    if (n == 0)
        return 1;
    if (n <= 2)
        return n;
  
    // To store the last three stages
    int f0 = 1, f1 = 1, f2 = 2, ans;
  
    // Find the numbers of steps required
    // to reach the distance i
    for (int i = 3; i <= n; i++) {
        ans = f0 + f1 + f2;
        f0 = f1;
        f1 = f2;
        f2 = ans;
    }
  
    // Return the required answer
    return ans;
}
  
// Driver code
int main()
{
    int n = 4;
  
    cout << countWays(n);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
import java.io.*;
  
class GFG 
{
      
// Function to return the count of the
// total number of ways to cover the
// distance with 1, 2 and 3 steps
static int countWays(int n)
{
    // Base conditions
    if (n == 0)
        return 1;
    if (n <= 2)
        return n;
  
    // To store the last three stages
    int f0 = 1, f1 = 1, f2 = 2;
    int ans=0;
  
    // Find the numbers of steps required
    // to reach the distance i
    for (int i = 3; i <= n; i++) 
    {
        ans = f0 + f1 + f2;
        f0 = f1;
        f1 = f2;
        f2 = ans;
    }
  
    // Return the required answer
    return ans;
}
  
// Driver code
public static void main (String[] args) 
{
      
    int n = 4;
    System.out.println (countWays(n));
}
}
  
// This code is contributed by jit_t

chevron_right


Python

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach
  
# Function to return the count of the
# total number of ways to cover the
# distance with 1, 2 and 3 steps
def countWays(n):
      
    # Base conditions
    if (n == 0):
        return 1
    if (n <= 2):
        return n
  
    # To store the last three stages
    f0 = 1
    f1 = 1
    f2 = 2
    ans = 0
  
    # Find the numbers of steps required
    # to reach the distance i
    for i in range(3, n + 1):
        ans = f0 + f1 + f2
        f0 = f1
        f1 = f2
        f2 = ans
  
    # Return the required answer
    return ans
  
# Driver code
n = 4
  
print(countWays(n))
  
# This code is contributed by mohit kumar 29

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach
using System;
  
class GFG 
{
      
// Function to return the count of the
// total number of ways to cover the
// distance with 1, 2 and 3 steps
static int countWays(int n)
{
    // Base conditions
    if (n == 0)
        return 1;
    if (n <= 2)
        return n;
  
    // To store the last three stages
    int f0 = 1, f1 = 1, f2 = 2;
    int ans = 0;
  
    // Find the numbers of steps required
    // to reach the distance i
    for (int i = 3; i <= n; i++) 
    {
        ans = f0 + f1 + f2;
        f0 = f1;
        f1 = f2;
        f2 = ans;
    }
  
    // Return the required answer
    return ans;
}
  
// Driver code
public static void Main(String[] args) 
{
    int n = 4;
    Console.WriteLine (countWays(n));
}
}
  
// This code is contributed by PrinciRaj1992

chevron_right


Output:

7

Time Complexity: O(N)
Space Complexity O(1)

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.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.