Problem Description
Given an array of m observed dice rolls and the mean of all n + m dice rolls, determine the n missing dice roll observations (each between 1 and 6) such that the overall mean is exactly the given mean. If there is no valid distribution of missing dice rolls that can achieve the target mean, return an empty array.
Key Insights
- Calculate the total sum needed from all n + m dice rolls using the mean.
- Determine the sum of the missing n observations by subtracting the sum of the observed rolls from the total required sum.
- Check if the missing sum is feasible given that each missing observation can only be between 1 and 6.
- If feasible, distribute the extra sum (above the minimum possible) evenly across the n observations without exceeding the maximum possible value for a dice roll.
- Return the resulting list of missing observations.
Space and Time Complexity
Time Complexity: O(n + m) – We iterate through the observed rolls to sum them and then iterate through the n missing observations. Space Complexity: O(n) – We store the n missing observations in a list.
Solution
To solve the problem:
- Compute the total sum required for all n + m observations: totalRequired = mean * (n + m).
- Calculate the observed sum from the given rolls.
- The sum missing = totalRequired - observedSum must be distributed among n dice rolls.
- Check if this missing sum is within the possible bounds: it must be at least n (if all dice show 1) and at most 6*n (if all dice show 6). If not, return an empty array.
- Initialize each missing observation with the minimum value 1, then compute the additional sum needed (extraSum = missingSum - n).
- Distribute the extra sum: For each missing observation, add up to 5 (maximum increase from 1 to 6) or the remaining extraSum until it is exhausted.
- Return the resulting list of missing observations.