2028. Find Missing Observations - Explanation

Problem Link



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

  1. Calculate the required sum for the n missing dice: nTotal = mean * (n + m) - sum(rolls).
  2. If nTotal < n or nTotal > 6 * n, return an empty array (no valid solution).
  3. For each of the n missing 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 nTotal and decrement the remaining count.
  4. 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 res

Time & Space Complexity

  • Time complexity: O(m+n)O(m + n)
  • Space complexity:
    • O(1)O(1) extra space.
    • O(n)O(n) space for the output array.

Where mm is the size of the array rollsrolls and nn 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

  1. Calculate the required sum for the n missing dice: nTotal = mean * (n + m) - sum(rolls).
  2. If nTotal < n or nTotal > 6 * n, return an empty array.
  3. Compute the base average: avg = nTotal / n.
  4. Compute the remainder: rem = nTotal - (avg * n).
  5. Create the result with (n - rem) dice having value avg and rem dice having value avg + 1.
  6. 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] * rem

Time & Space Complexity

  • Time complexity: O(m+n)O(m + n)
  • Space complexity:
    • O(1)O(1) extra space.
    • O(n)O(n) space for the output array.

Where mm is the size of the array rollsrolls and nn 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.