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

// 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; }

Output:

7

The time complexity of above solution is exponential, a close upper bound is O(3^{n}). 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.

// 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; }

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