Find Missing Observations — LeetCode 2028 Python Solution

MediumArrayMathSimulation
Problem
#2028
Reading time
2 min

The problem

You have observations of n + m 6-sided dice rolls with each face numbered from 1 to 6. n of the observations went missing, and you only have the observations of m rolls.

Example

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.

Python solution

Python
class Solution:
    def missingRolls(self, rolls: List[int], mean: int, n: int) -> List[int]:
        m = len(rolls)
        s = (n + m) * mean - sum(rolls)
        if s > n * 6 or s < n:
            return []
        ans = [s // n] * n
        for i in range(s % n):
            ans[i] += 1
        return ans

Complexity

MeasureComplexity
TimeO(n + m), where n and m are the number of missing numbers and known numbers, respectively
SpaceO(1) auxiliary

Pattern: Math and Number Theory

Find the closed form, the invariant, or the modular identity — and skip the loop entirely. LeetCode 2028. Find Missing Observations is filed here on both counts: the reference solution below belongs to the algorithm family this hub collects, and LeetCode tags it Math.

The math and number theory guide has the Python template for the pattern and the 485 LeetCode problems that use it.

Related problems

Frequently asked questions

How hard is LeetCode 2028. Find Missing Observations?
LeetCode 2028. Find Missing Observations is rated Medium on LeetCode.
What is the time complexity of LeetCode 2028. Find Missing Observations?
The Python solution on this page runs in O(n + m), where n and m are the number of missing numbers and known numbers, respectively.
What is the space complexity of LeetCode 2028. Find Missing Observations?
The Python solution on this page uses O(1) auxiliary space.
What topics does LeetCode 2028. Find Missing Observations cover?
LeetCode 2028. Find Missing Observations is tagged Array, Math and Simulation on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview