Given a distance N. The task is to count the total number of ways to cover the distance with 1, 2 and 3 steps.
Input: N = 3
All the required ways are (1 + 1 + 1), (1 + 2), (2 + 1) and (3).
Input: N = 4
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:
Time Complexity: O(N)
Space Complexity O(1)
- Count number of ways to cover a distance
- Count number of ways to get Odd Sum
- Count number of ways to divide a number in 4 parts
- Count the number of ways to traverse a Matrix
- Count ways to express even number ‘n’ as sum of even integers
- Count number of ways to arrange first N numbers
- Count number of ways to jump to reach end
- Count number of ways to partition a set into k subsets
- Count number of ways to reach a given score in a Matrix
- Count number of ways to divide an array into two halves with same sum
- Count number of ways to fill a "n x 4" grid using "1 x 4" tiles
- Count number of ways to reach a given score in a game
- Count ways to express a number as sum of consecutive numbers
- Count number of ways to reach destination in a Maze
- Count ways to spell a number with repeated digits
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.