2028. Find Missing Observations - Explanation
Prerequisites
Before attempting this problem, you should be comfortable with:
- Basic Math (Mean and Sum) - The solution requires calculating target sums from the given mean and distributing values across dice
- Greedy Distribution - Assigning values to dice while respecting constraints (1-6 per die) uses greedy thinking to maximize or evenly distribute values
- Boundary Validation - Checking whether a valid solution exists requires understanding the min/max possible sums for n dice
1. Math - I
Intuition
We know the target mean and the sum of the existing rolls. From this, we can calculate the total sum needed for all n + m dice, and therefore the sum required for the n missing dice. If this required sum is impossible (less than n or greater than 6 * n), no valid solution exists. Otherwise, we greedily assign values to each die, giving each one as high a value as possible while ensuring the remaining dice can still reach at least 1 each.
Algorithm
- Calculate the required sum for the
nmissing dice:nTotal = mean * (n + m) - sum(rolls). - If
nTotal < nornTotal > 6 * n, return an empty array (no valid solution). - For each of the
nmissing dice:- Assign the maximum possible value while leaving enough for the remaining dice to each have at least
1. - The value is
min(nTotal - (remaining dice) + 1, 6). - Subtract this value from
nTotaland decrement the remaining count.
- Assign the maximum possible value while leaving enough for the remaining dice to each have at least
- Return the constructed result array.
class Solution:
def missingRolls(self, rolls: List[int], mean: int, n: int) -> List[int]:
m = len(rolls)
nTotal = (mean * (n + m)) - sum(rolls)
if nTotal < n or nTotal > n * 6:
return []
res = []
while nTotal:
dice = min(nTotal - n + 1, 6)
res.append(dice)
nTotal -= dice
n -= 1
return resTime & Space Complexity
- Time complexity:
- Space complexity:
- extra space.
- space for the output array.
Where is the size of the array and is the number of missing observations.
2. Math - II
Intuition
Instead of greedily assigning values one at a time, we can distribute the required sum more evenly. First, compute the average value each die should have by dividing the total needed by n. The remainder tells us how many dice need to be one higher than the average. This produces a cleaner distribution where most dice have the same value.
Algorithm
- Calculate the required sum for the
nmissing dice:nTotal = mean * (n + m) - sum(rolls). - If
nTotal < nornTotal > 6 * n, return an empty array. - Compute the base average:
avg = nTotal / n. - Compute the remainder:
rem = nTotal - (avg * n). - Create the result with
(n - rem)dice having valueavgandremdice having valueavg + 1. - Return the result array.
class Solution:
def missingRolls(self, rolls: List[int], mean: int, n: int) -> List[int]:
m = len(rolls)
nTotal = (mean * (n + m)) - sum(rolls)
if nTotal < n or nTotal > n * 6:
return []
avg = nTotal // n
rem = nTotal - (avg * n)
return [avg] * (n - rem) + [avg + 1] * remTime & Space Complexity
- Time complexity:
- Space complexity:
- extra space.
- space for the output array.
Where is the size of the array and is the number of missing observations.
Common Pitfalls
Forgetting to Validate the Required Sum
Before constructing the result, you must check that nTotal (the required sum for missing dice) falls within the valid range [n, 6*n]. If nTotal < n, it is impossible to assign at least 1 to each die. If nTotal > 6*n, it is impossible even if all dice show 6. Failing to return an empty array in these cases leads to incorrect or invalid outputs.
Integer Overflow When Computing Total Sum
When calculating mean * (n + m), both n and m can be large (up to 10^5), and the mean can be up to 6. The product can exceed the range of 32-bit integers in some languages. Ensure you use appropriate data types (e.g., long in Java) or rely on languages with arbitrary precision integers to avoid overflow.
Off-by-One Errors in Greedy Assignment
When greedily assigning values, the formula min(nTotal - remaining + 1, 6) requires careful handling of the remaining count. If you decrement the count at the wrong time or miscalculate how much "room" is left for future dice, you may assign invalid values (less than 1 or greater than 6) or fail to distribute the sum correctly.