Count number of ways to cover a distance

1.9

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
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

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


Output:

7

The time complexity of above solution is exponential, a close upper bound is O(3n). If we draw the complete recursion tree, we can observer that many subproblems are solved again and again. For example, when we start from n = 6, we can reach 4 by subtracting one 2 times and by subtracting 2 one times. So the subproblem for 4 is called twice.
Since same suproblems are called again, this problem has Overlapping Subprolems property. So min square sum problem has both properties (see this and this) of a dynamic programming problem. Like other typical Dynamic Programming(DP) problems, recomputations of same subproblems can be avoided by constructing a temporary array count[] in bottom up manner.

Below is Dynamic Programming based C++ 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,  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 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;
        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 program
	public static void main (String[] args) 
	{
		int dist = 4;
        System.out.println(printCountDP(dist));
	}
}

// This code is contributed by Pramod Kumar


Output:
7

Asked in: Amazon

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

GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.

Recommended Posts:



1.9 Average Difficulty : 1.9/5.0
Based on 67 vote(s)










Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.