VOOZH about

URL: https://www.geeksforgeeks.org/dsa/cses-solutions-dice-combinations/

⇱ CSES Solutions - Dice Combinations - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

CSES Solutions - Dice Combinations

Last Updated : 23 Jul, 2025

Your task is to count the number of ways to construct sum N by throwing a dice one or more times. Each throw produces an outcome between 1 and 6.

Examples:

Input: N = 3
Output: 4
Explanation: There are 4 ways to make sum = 3.

  • Using 1 die {3}, sum = 3.
  • Using 2 dice {1, 2}, sum = 1 + 2 = 3.
  • Using 2 dice {2, 1}, sum = 2 + 1 = 3.
  • Using 3 dice {1, 1, 1}, sum = 1 + 1 + 1 = 3.

Input: N = 4
Output: 8
Explanation: There are 8 ways to make sum = 4.

  • Using 1 die {4}, sum = 4.
  • Using 2 dice {1, 3}, sum = 1 + 3 = 4.
  • Using 2 dice {2, 2}, sum = 2 + 2 = 4.
  • Using 2 dice {3, 1}, sum = 3 + 1 = 4.
  • Using 3 dice {1, 1, 2}, sum = 1 + 1 + 2 = 4.
  • Using 3 dice {1, 2, 1}, sum = 1 + 2 + 1 = 4.
  • Using 3 dice {2, 1, 1}, sum = 2 + 1 + 1 = 4.
  • Using 4 dice {1, 1, 1, 1}, sum = 1 + 1 + 1 + 1 = 4.

Approach: To solve the problem, follow the below idea:

The problem can be solved using Dynamic Programming to find the number of ways to construct a particular sum. Maintain a dp[] array such that dp[i] stores the number of ways to construct sum = i. There are only 6 possible sums when we throw a dice: 1, 2, 3, 4, 5 and 6. So, if we want to find the number of ways to construct sum = S after throwing a dice, there are 6 outcomes:

  • The dice outcome was 1: So, the number of ways to construct sum = S, will be equal to the number of ways to construct sum = S - 1.
  • The dice outcome was 2: So, the number of ways to construct sum = S, will be equal to the number of ways to construct sum = S - 2.
  • The dice outcome was 3: So, the number of ways to construct sum = S, will be equal to the number of ways to construct sum = S - 3.
  • The dice outcome was 4: So, the number of ways to construct sum = S, will be equal to the number of ways to construct sum = S - 4.
  • The dice outcome was 5: So, the number of ways to construct sum = S, will be equal to the number of ways to construct sum = S - 5.
  • The dice outcome was 6: So, the number of ways to construct sum = S, will be equal to the number of ways to construct sum = S - 6.

This means to construct a sum S, the total number of ways will be sum of all ways to construct sum from (S - 6) to (S - 1). Now, we can construct the dp[] array as:

dp[i] = ∑(dp[i - j]), where j ranges from 1 to 6.

Step-by-step algorithm:

  • Initialize a dp[] array such that dp[i] stores the number of ways we can construct sum = i.
  • Initialize dp[0] = 1 as there is only 1 way to make sum = 0, that is to not throw any die at all.
  • Iterate i from 1 to X and calculate number of ways to make sum = i.
  • Iterate j over all possible values of the last die to make sum = i and add dp[i – j] to dp[i].
  • After all the iterations, return the final answer as dp[X].

Below is the implementation of the algorithm:


Output
8

Time Complexity: O(N), where N is the sum which we need to construct.
Auxiliary Space: O(N)

Comment
Article Tags:
Article Tags: