- Related Questions & Answers
- Probability of getting a sum on throwing 2 Dices N times in C++
- Maximum score after flipping a Binary Matrix atmost K times in C++
- How to return a string repeated N number of times in C#?
- C++ Program to Emulate N Dice Roller
- Number of Ways to Paint N × 3 Grid in C++
- Maximum number of pieces in N cuts in C++
- Maximum sum after repeatedly dividing N by a divisor in C++
- Maximum number of ones in a N*N matrix with given constraints in C++
- Number of Ways to Paint N × 3 Grid in C++ program
- Print all the sum pairs which occur maximum number of times in C++
- Count number of 1s in the array after N moves in C
- Find the number of players who roll the dice when the dice output sequence is given in C++
- Maximum segment value after putting k breakpoints in a number in C++
- Number of Dice Rolls With Target Sum in Python
- Concatenate a string given number of times in C++ programming

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Given the task is to calculate the maximum number of dots that can be expected after throwing a dice N times having M faces.

The first face of the dice contains 1 dot, the second face has 2 dots and so on. Likewise the M-th face contains M number of dots.

The probability of appearance of each face becomes 1/M.

Let’s now understand what we have to do using an example −

**Input** − M=2, N=3

**Output** − 1.875

**Explanation** − The dice has 2 sides = {1, 2}

If the dice is thrown 3 times then the sample space will be = M^{N} = 2^{3}

{(1, 1, 1), (1, 1, 2), (1, 2, 1), (1, 2, 2,) (2, 1, 1), (2, 1, 2), (2, 2, 1), (2, 2, 2,)} Maximum number in (1, 1, 1) = 1 Maximum number in (1, 1, 2) = 2 Maximum number in (1, 2, 1) = 2 Maximum number in (1, 2, 2) = 2 Maximum number in (2, 1, 1) = 2 Maximum number in (2, 1, 2) = 2 Maximum number in (2, 2, 1) = 2 Maximum number in (2, 2, 2) = 2 Probability of each case = 1/2^{3}= 0.125 Therefore, expectation of maximum number = (1+2+2+2+2+2+2+2) * (0.125) = 1.875

**Input** − M=2, N=2

**Output** − 1.75

The maximum number of cases in which a number can occur can be found using its previous number by using the formula − i

^{N}– (i-1)^{N}.For example if M=4 and N=2, the total number of cases in which maximum = 4 will be 4

^{2}– (4-1)^{2}= 7.So the final answer will be applying this formula on every element from 1 to M −

(i * (i

^{N}– (i - 1)^{N})) / M^{N}and adding them up.In function MaxExpect() initialize a variable max =0 of type double to store the sum.

Then loop from i=M till i>0

Inside the loop apply the above stated formula and keep adding all the resultant values into the variable max.

#include <bits/stdc++.h> using namespace std; double MaxExpect(double M, double N){ double max = 0.0, i; for (i = M; i; i--) /*formula to find maximum number and sum of maximum numbers*/ max += (pow(i / M, N) - pow((i - 1) / M, N)) * i; return max; } int main(){ double M = 2, N = 3; cout << MaxExpect(M, N); return 0; }

If we run the above code we will get the following output −

1.875

Advertisements