INTA-004

INTA-004
# interview assessment questions
Dice Rolls 

// C++ function to calculate the number of 
// ways to achieve sum x in n no of throws 
#include <bits/stdc++.h> 
using namespace std; 
#define mod 1000000007 
int dp[55][55]; 
  
// Function to calculate recursively the 
// number of ways to get sum in given 
// throws and [1..m] values 
int NoofWays(int face, int throws, int sum) 

    // Base condition 1 
    if (sum == 0 && throws == 0) 
        return 1; 
  
    // Base condition 2 
    if (sum < 0 || throws == 0) 
        return 0; 
  
    // If value already calculated dont 
    // move into re-computation 
    if (dp[throws][sum] != -1) 
        return dp[throws][sum]; 
  
    int ans = 0; 
    for (int i = 1; i <= face; i++) { 
  
        // Recusively moving for sum-i in 
        // throws-1 no of throws left 
        ans += NoofWays(face, throws - 1, sum - i); 
    } 
  
    // Inserting present values in dp 
    return dp[throws][sum] = ans; 

  
// Driver function 
int main() 

    int faces = 6, throws = 3, sum = 12; 
    memset(dp, -1, sizeof dp); 
    cout << NoofWays(faces, throws, sum) << endl; 
    return 0; 



======================================================
You have d dice, and each die has f faces numbered 1, 2, ..., f.
Return the number of possible ways (out of fd total ways) 
modulo 10^9 + 7 to roll the dice so the 
sum of the face up numbers equals target.

Example 1:
Input: d = 1, f = 6, target = 3
Output: 1
Explanation: 
You throw one die with 6 faces.  There is only one way to get a sum of 3.

Example 2:
Input: d = 2, f = 6, target = 7
Output: 6
Explanation: 
You throw two dice, each with 6 faces.  There are 6 ways to get a sum of 7:
1+6, 2+5, 3+4, 4+3, 5+2, 6+1.

Example 3:
Input: d = 2, f = 5, target = 10
Output: 1
Explanation: 
You throw two dice, each with 5 faces.  There is only one way to get a sum of 10: 5+5.

Example 4:
Input: d = 1, f = 2, target = 3
Output: 0
Explanation: 
You throw one die with 2 faces.  There is no way to get a sum of 3.

Example 5:
Input: d = 30, f = 30, target = 500
Output: 222616187
Explanation: 
The answer must be returned modulo 10^9 + 7.

Constraints:
1 <= d, f <= 30

1 <= target <= 1000

Blog Archive