Suppose we have M different expressions, and the answers of these expressions are in range 1 to N (both inclusive) So consider x = max(f(i)) for each i in range 1 through N, we have to find the expected value of x.
So, if the input is like M = 3, N = 3, then the output will be 2.2, because
Sequence | Maximum frequency |
---|---|
111 | 3 |
112 | 2 |
113 | 2 |
122 | 2 |
123 | 1 |
133 | 1 |
222 | 3 |
223 | 2 |
233 | 2 |
333 | 3 |
$$E(x) = \sum P(x) * x = P(1) + 2P(2) + 3P(3) = \frac{1}{10} + 2 * \frac{6}{10} + 3 * \frac{3}{10} = \frac{22}{10}$$
To solve this, we will follow these steps −
Let us see the following implementation to get better understanding −
combination = {} def nCr(n, k_in): k = min(k_in, n - k_in) if n < k or k < 0: return 0 elif (n, k) in combination: return combination[(n, k)] elif k == 0: return 1 elif n == k: return 1 else: a = 1 for cnt in range(k): a *= (n - cnt) a //= (cnt + 1) combination[(n, cnt + 1)] = a return a def solve(M, N): arr = [] for k in range(2, M + 2): a = 1 s = 0 for i in range(M // k + 2): if (M < i * k): break s += a * nCr(N, i) * nCr(N - 1 + M - i * k, M - i * k) a *= -1 arr.append(s) total = arr[-1] diff = [arr[0]] + [arr[cnt + 1] - arr[cnt] for cnt in range(M - 1)] output = sum(diff[cnt] * (cnt + 1) / total for cnt in range(M)) return output M = 3 N = 3 print(solve(M, N))
3, 3
1