Find Missing Observations

Updated on 28 May, 2025
Find Missing Observations header image

Problem Statement

In a series of dice rolls using a standard 6-sided dice, some of the observations have gone missing. In this problem, you have the results from m dice rolls, missing results from n rolls, and the average of all n + m rolls. Given an array rolls of length m representing the observed values and integers mean (the calculated average of all n+m rolls) and n, your task is to compute the missing values such that the overall average is precisely the given mean.

Your function should return an array of integers of length n representing the missing observations. If several valid solutions exist, returning any one of them is acceptable. If it's impossible to achieve the required average, you should return an empty array. The crucial aspect here is adherence to the average with the given constraints of dice roll values (1 to 6).

Examples

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.

Constraints

  • m == rolls.length
  • 1 <= n, m <= 105
  • 1 <= rolls[i], mean <= 6

Approach and Intuition

Understanding the Computation

  1. Calculate Total Sum from Mean: Since the mean is given, multiply the mean by the total number of observations (n + m) to get the total desired sum of all rolls.

  2. Compute Known Sum: Sum the values from the given rolls array to get the total of the observed rolls.

  3. Determine Missing Sum: Subtract the known sum from the total sum to find out how much the missing observations should sum up to.

Determine Possibility and Generate the Missing Values

  1. Check Feasibility: Ensure the sum needed for the missing rolls lies between n*1 and n*6 (since each dice roll is between 1 and 6). If not, it's impossible to achieve the mean, hence return an empty array.

  2. Distribute the Missing Sum:

    • Start by setting each missing value to the minimum possible value (1).
    • Calculate the remaining sum needed after initializing all missing values to 1.
    • Distribute the remaining sum across the n values, ensuring no individual value exceeds 6, following these steps:
      • Increment each roll by one until the desired sum is reached or the maximal value of a roll (6) is achieved.
      • Repeat for each roll until the entire remaining sum is perfectly distributed.

Consider Edge Cases and Constraints

  1. Small values of n and m: For smaller numbers, carefully manage distributions as the range for error is considerably less.

  2. High roll values with low n: If initial rolls are high and n is very low, achieving balance might be rare or impossible.

  3. Validity of given constraints: Verify that provided values (like individual rolls) do not violate the dice's limits (1-6).

By following these steps, the solution can be implemented efficiently, staying within the boundaries of complexity constraints due to the potentially high values of n and m.

Solutions

  • C++
  • Java
  • Python
cpp
class Solution {
public:
    vector<int> findMissing(vector<int>& rolls, int targetMean, int missingCount) {
        int currentSum = accumulate(rolls.begin(), rolls.end(), 0);
        int neededSum = targetMean * (missingCount + rolls.size()) - currentSum;
        if (neededSum > 6 * missingCount || neededSum < missingCount) {
            return {};
        }
        int baseValue = neededSum / missingCount;
        int remainder = neededSum % missingCount;
        vector<int> missingElements(missingCount, baseValue);
        for (int index = 0; index < remainder; index++) {
            missingElements[index]++;
        }
        return missingElements;
    }
};

This C++ solution focuses on the problem of finding the missing observations from a set given the target mean, the number of missing observations, and a list of existing rolls.

  • Start by calculating the currentSum of the provided rolls using the accumulate function, which sums up all elements in the rolls.
  • Compute neededSum, which is the total sum required to achieve the target mean across both the existing rolls and the missing values. Subtract currentSum from this total to determine the sum that the missing observations must equal.
  • Check if neededSum is not achievable (i.e., it requires values smaller than 1 or greater than 6, which are outside the possible dice outcomes). Return an empty array in that case.
  • Calculate a baseValue by dividing neededSum by missingCount to distribute evenly among the missing observations.
  • Compute the remainder to distribute the leftover sum after dividing to get integers.
  • Initialize a vector missingElements with the baseValue. Add one to the first remainder elements of the vector to distribute the remainder evenly among the missing values.
  • Return the missingElements vector filled with the computed values, ensuring all elements are between 1 and 6 inclusive, and their sum yields the needed total to match the targetMean.

This approach cleverly breaks down the mean calculation into manageable components, distributes values appropriately, and ensures the final conditions of the problem (each outcome being between 1 and 6) are met.

java
public class Solution {

    public int[] findMissing(int[] pastRolls, int targetMean, int newRollsCount) {
        int totalSum = 0;
        for (int roll : pastRolls) {
            totalSum += roll;
        }
        int neededSum = targetMean * (newRollsCount + pastRolls.length) - totalSum;
        if (neededSum > 6 * newRollsCount || neededSum < newRollsCount) {
            return new int[0];
        }
        int baseValue = neededSum / newRollsCount;
        int remainder = neededSum % newRollsCount;
        int[] missingRolls = new int[newRollsCount];
        Arrays.fill(missingRolls, baseValue);
        for (int i = 0; i < remainder; i++) {
            missingRolls[i]++;
        }
        return missingRolls;
    }
}

The provided Java solution addresses the issue of finding missing dice rolls that would achieve a specified target mean across all the observed and missing rolls. The methodology for determining these missing values embodies the following sequence:

  1. Calculate the total sum of the previously observed dice rolls.
  2. Compute the needed sum to reach the targeted mean considering both the observed and missing rolls.
  3. Check if the needed sum falls within the valid potential outcomes of the missing rolls (i.e., the sum should not be less than the number of missing rolls nor more than six times this number, as dice rolls yield values between 1 and 6).
  4. Calculate a base value for each missing roll as the integer division of the needed sum by the number of missing rolls. Capture the remainder of this division.
  5. Instantiate an array for the missing rolls with each element initialized to the base value.
  6. Distribute the remainder across the array's elements incrementally to fine-tune the sum to precisely match the needed sum.

This approach ensures proper adjustment of the computed values within the constraints of possible dice outcomes, effectively filling in the array to complete the sequence of rolls that should achieve the desired mean value.

python
class Solution:
    def findMissing(self, rolls: List[int], targetMean: int, m: int) -> List[int]:
        currentSum = sum(rolls)
        totalNeeded = targetMean * (m + len(rolls)) - currentSum
        if totalNeeded > m * 6 or totalNeeded < m:
            return []
        baseValue = totalNeeded // m
        remainder = totalNeeded % m
        result = [baseValue] * m
        for index in range(remainder):
            result[index] += 1
        return result

This Python solution addresses the problem of finding missing observations required to reach a specific target mean over a given set of numbers. The function findMissing in the provided Solution class accomplishes this using the following logic:

  • It first calculates the current sum of the given rolls array.

  • It then determines the total sum needed to achieve the targetMean across all observations (both existing and missing).

  • It checks if the required sum to achieve the target mean is feasible within given constraints:

    • The sum must not exceed the maximum possible sum with the missing values (each missing value can be at most 6),
    • It must also be greater than or equal to the count of missing values m (assuming the smallest value for missing observation is 1).
  • If the total required sum is outside feasible bounds, the function returns an empty list.

  • If within valid bounds, it calculates a base value for each missing observation by dividing the total needed additional sum by m.

  • It distributes the remainder of this division among the missing values to balance out the total sum evenly.

The algorithm efficiently constructs the list of missing observations to exactly meet the required target mean with straightforward calculations and a single distribution pass. This approach minimizes potential errors in sum calculation and ensures accurate completion of the list with feasible values.

Comments

No comments yet.