Prepin
Log in

Determine the total number of ways to reach a specified sum using n rolls of dice that have k faces.

Approach / Explanation

This question requires an understanding of combination and probability theories. It asks you to calculate the total number of ways to reach a specific sum using a certain number of throws of a die with a set number of faces. To clarify, consider a die with 'k' faces that you will throw 'n' times. Your objective is to find out how many arrangements of the faces can result in a total sum after 'n' throws that equals a specified number. Here are some important points to assist you in answering the question: Make sure to account for all potential outcomes – each die throw can result in any of its faces. This is a dynamic programming problem. You will begin by solving for the target sum and work downwards by exploring all possibilities. Establish a structure where ways[i][j] represents the number of ways to achieve a sum of i with j throws of the die. Appropriately address base cases, such as scenarios with zero throws, a single throw, etc.

Suggested Answer

To determine the number of ways to achieve a sum of 6 when rolling a standard 6-faced die three times, we can approach the problem as follows: We start by creating a matrix called 'ways' with dimensions (x+1)*(n+1), where x is the target sum and n represents the number of rolls. It is important to note that it is impossible to achieve any sum (other than 0) with zero rolls, so we assign ways[i][0] = 0 for all i > 0. Conversely, for a sum of 0, regardless of the number of rolls, there is exactly one way to achieve this (i.e., by not rolling the die). Our objective is to find the value of ways[x][n], using combinations derived from earlier calculations. To do this, we iterate through each possible sum from 1 to x for every number of rolls from 1 to n. For each combination, we compute ways[i][j] by adding up ways[i-k][j-1] for every k from 1 to the number of die faces, stopping when i-k = 0. Returning to our specific case where the target sum is 6, the number of rolls is 3, and the die has 6 faces, we find through this process that there are 10 distinct ways to achieve a sum of 6 with three rolls of the die.

Alternative Answer

As another example, consider a 4-faced die that we are rolling twice, and we aim to determine how many combinations result in a sum of 5. Using the same method, our matrix will indicate that there are 4 different combinations to achieve a sum of 5 with two rolls of a 4-sided die.

Question Details

Difficulty & Category

Medium

Product

For CandidatesFor RecruitersCompaniesBrowse JobsInterview QuestionsDeep ProfileSkill AssessmentOpportunity Matching
Prepin.ai

© 2026 Prepin LLC · Philadelphia, PA · All rights reserved.