Dice Combinations

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.

For example, if n =3, there are 4 ways:




Example:

Input:  n = 3
Output: 4

Approach

C++

#include <bits/stdc++.h>
using namespace std;

const int MOD = 1e9 + 7;

int diceCombinations(int n)
{

    vector<intdp(n + 1);
    dp[0] = 1;
    for (int i = 0i < ni++)
    {
        for (int j = 1j <= 6j++)
        {
            if (i + j <= n)
            {
                dp[i + j] = (dp[i + j] + dp[i]) % MOD;
            }
        }
    }
    return dp[n];
}

int main()
{
    int n = 3;

    cout << diceCombinations(n<< "\n";

    return 0;
}


No comments:

Post a Comment