Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dice roll Algo added #1830

Merged
merged 2 commits into from
Nov 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions Dynamic Programming/Dice Roll Problem/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Dice Roll Sum Problem

## Description

The **Dice Roll Sum** problem is a dynamic programming challenge that asks how many ways you can achieve a given target sum by rolling a certain number of dice. Each die has a fixed number of faces (e.g., 6), and each face has an equal probability of appearing. The objective is to calculate the number of distinct ways to reach the target sum using the given dice.

### Problem Statement
Given:
- `n`: the number of dice,
- `target`: the target sum,
- `faces`: the number of faces on each die (default is 6).

Find the number of ways to achieve exactly the target sum by rolling the dice.

### Example

**Input:**
- Number of dice: `2`
- Target sum: `7`

**Output:**
- Number of ways to reach target sum: `6`

**Explanation:**
With 2 dice, there are six ways to achieve a sum of 7:
- (1,6), (2,5), (3,4), (4,3), (5,2), (6,1)

## Solution Approach

We use **dynamic programming** to build up the solution by breaking down the problem into smaller subproblems:
1. Define a 2D DP array `dp[i][j]` where `i` is the number of dice and `j` is the target sum.
2. Initialize `dp[0][0] = 1`, representing one way to achieve a sum of 0 with 0 dice.
3. For each dice count `i`, calculate the possible ways to reach each target sum `j` by considering all face values from 1 up to `faces`.
4. The answer will be stored in `dp[n][target]`, representing the number of ways to reach the target sum with `n` dice.
41 changes: 41 additions & 0 deletions Dynamic Programming/Dice Roll Problem/program.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <stdio.h>
#include <string.h>

#define MAX_DICE 100
#define MAX_SUM 1000
#define FACES 6 // Number of faces on each die, can be adjusted as needed

// DP table to store the number of ways to get each sum with a given number of dice
int dp[MAX_DICE + 1][MAX_SUM + 1];

int diceRollSum(int n, int target) {
// Initialize the DP table with zeros
memset(dp, 0, sizeof(dp));
dp[0][0] = 1; // Base case: 1 way to get sum 0 with 0 dice

for (int i = 1; i <= n; i++) { // For each dice count
for (int j = 1; j <= target; j++) { // For each possible sum
dp[i][j] = 0;
for (int face = 1; face <= FACES; face++) { // For each face value
if (j - face >= 0) { // Ensure sum is non-negative
dp[i][j] += dp[i - 1][j - face];
}
}
}
}

return dp[n][target];
}

int main() {
int n, target;
printf("Enter number of dice: ");
scanf("%d", &n);
printf("Enter target sum: ");
scanf("%d", &target);

int result = diceRollSum(n, target);
printf("Number of ways to reach the target sum %d with %d dice: %d\n", target, n, result);

return 0;
}
Loading