VOOZH about

URL: https://www.geeksforgeeks.org/dsa/count-number-ways-reach-given-score-game/

⇱ Count ways to reach a score with 3, 5 and 10 - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Count ways to reach a score with 3, 5 and 10

Last Updated : 1 May, 2026

Consider a game where players can score 3, 5, or 10 points in a move. Given a total score of n, the task is to find the number of ways to reach the given score.

Examples:

Input: n = 20
Output: 4
Explanation: 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
Explanation: There are following 2 ways to reach 13: (3, 5, 5), (3, 10)

The given problem is a variation of the coin change problem.

Using Recursion - O(2^n) Time and O(n) Space

The idea is to use a recursive function that explores all possible combinations of scoring points by making choices at each step. For each point value (3, 5, or 10), we have two options: either take that point value or skip it. The function will recursively explore these choices. When the total score reaches exactly 0, we count it as a valid combination and if it goes below 0, return 0.

Recurrence Relation: The final result will be the sum of both cases:

  • countWays(n, i, points) = countWays(n-points[i], i, points) + countWays(n, i+1, points)

Base Cases:

  • countWays(n) = 1, if n = 0.
  • countWays(n) = 0, if n < 0 or i == 3.

Output
4

Using Top-Down DP (Memoization) - O(n) Time and O(n) Space

If we notice carefully, we can observe that the above recursive solution holds the following two properties of Dynamic Programming:

1. Optimal Substructure: Number of ways to make score n at index i, i.e., countWays(n, i, points), depends on the optimal solutions of the subproblems countWays(n-points[i], i, points) and countWays(n, i+1, points). By combining these optimal substructures, we can efficiently calculate the number of ways to make target score at index i.

2. Overlapping Subproblems: While applying a recursive approach in this problem, we notice that certain subproblems are computed multiple times.

  • There are two parameters: n and i that changes in the recursive solution. So we create a 2D matrix of size (n+1)*3 for memoization.
  • We initialize this matrix as -1 to indicate nothing is computed initially.
  • Now we modify our recursive solution to first check if the value is -1, then only make recursive calls. This way, we avoid re-computations of the same subproblems.

Output
4

Using Bottom-Up DP (Tabulation) - O(n) Time and O(n) Space

The idea is to fill the DP table based on previous values. For each point, we either include it or exclude it to compute the number of ways needed for each score. The table is filled in an iterative manner from score i = 1 to i = n and for each point points[j] from j=2 to j=0.

The dynamic programming relation is as follows: 

  • if (i-points[j]) is greater than 0, then dp[i][j] = dp[i-points[j]][j] + dp[i][j+1]
  • else dp[i][j] = dp[i][j+1].

Output
4

Using Space Optimized DP - O(n) Time and O(1) Space

In previous approach of dynamic programming we have derive the relation between states as given below:

  • if (i-points[j]) is greater than 0, then dp[i][j] = dp[i-points[j]][j] + dp[i][j+1]
  • else dp[i][j] = dp[i][j+1].

If we observe that for calculating current dp[i][j] state we only need to store the last 10 scores. There is no need to store all the previous states to compute result.


Output
4
Comment
Article Tags: