Consider a game where a player can score 3 or 5 or 10 points in a move. Given a total score n, find number of ways to reach the given score.
Input: n = 20 Output: 4 There are following 4 ways to reach 20 (10, 10) (5, 5, 10) (5, 5, 5, 5) (3, 3, 3, 3, 3, 5) Input: n = 13 Output: 2 There are following 2 ways to reach 13 (3, 5, 5) (3, 10)
This problem is a variation of coin change problem and can be solved in O(n) time and O(n) auxiliary space.
The idea is to create a table of size n+1 to store counts of all scores from 0 to n. For every possible move (3, 5 and 10), increment values in table.
Count for 20 is 4 Count for 13 is 2
Exercise: How to count score when (10, 5, 5), (5, 5, 10) and (5, 10, 5) are considered as different sequences of moves. Similarly, (5, 3, 3), (3, 5, 3) and (3, 3, 5) are considered different.
This article is contributed by Rajeev Arora. 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.
- Count number of ways to reach a given score in a Matrix
- Count ways to reach a score using 1 and 2 with no consecutive 2s
- Count number of ways to reach destination in a Maze
- Count number of ways to jump to reach end
- Count ways to reach the n'th stair
- Count ways to reach the nth stair using step 1, 2 or 3
- Count ways to reach end from start stone with at most K jumps at each step
- Count ways to reach Nth Stairs by taking 1 and 2 steps with exactly one 3 step
- Maximum score possible after performing given operations on an Array
- Maximum score of deleting an element from an Array based on given condition
- Maximum possible score that can be obtained by constructing a Binary Tree based on given conditions
- Number of ways to reach Nth floor by taking at-most K leaps
- Number of ways to reach the end of matrix with non-zero AND value
- Find the number of ways to reach Kth step in stair case
- Maximum score assigned to a subsequence of numerically consecutive and distinct array elements
- Count of Ways to obtain given Sum from the given Array elements
- Count of ways to split a given number into prime segments
- Count minimum factor jumps required to reach the end of an Array
- Number of ways to get a given sum with n number of m-faced dices
- Reach the numbers by making jumps of two given lengths