#2028
Find Missing Observations
specialist · 760 · lc medium +31 · verified · 57.4% accepted · 1,126 likes · top 53%
Description
You recorded n + m rolls of a 6-sided die (faces 1–6), but n values were lost. The m observed values are given in the array rolls, along with the overall mean and the count n of missing rolls. Reconstruct the n missing values so that the mean of all n + m rolls equals mean. Return any valid length-n array, or an empty array if no solution exists.
Example 1:
Input: rolls = [3,2,4,3], mean = 4, n = 2
Output: [6,6]
Explanation: The mean of all n + m rolls is (3 + 2 + 4 + 3 + 6 + 6) / 6 = 4.
Example 2:
Input: rolls = [1,5,6], mean = 3, n = 4
Output: [2,3,2,2]
Explanation: The mean of all n + m rolls is (1 + 5 + 6 + 2 + 3 + 2 + 2) / 7 = 3.
Example 3:
Input: rolls = [1,2,3,4], mean = 6, n = 4
Output: []
Explanation: It is impossible for the mean to be 6 no matter what the 4 missing rolls are.
Code
1
2
3