Find Missing Observations

Medium Topics Company Tags

You know the results of some rolls of a six-sided die, stored in rolls. Another n results are missing. The average of all the known and missing results is exactly the integer mean.

Return any array of n missing results that makes this average correct. Every result must be an integer from 1 to 6. If no such array exists, return an empty array.

Example 1:

Input: rolls = [2,4]
mean = 3
n = 2

Output: [5,1]

Explanation: The known rolls sum to 6. Four rolls with average 3 must total 12, so the missing rolls must sum to 6. The answer [5,1] works; [3,3] is another valid answer.

Example 2:

Input: rolls = [6,6]
mean = 1
n = 2

Output: []

Explanation: The known rolls already sum to 12, but four rolls with average 1 would total only 4. No missing results can satisfy this requirement.

Example 3:

Input: rolls = [1,1]
mean = 2
n = 3

Output: [6,1,1]

Explanation: Five rolls with average 2 must total 10. The known rolls sum to 2, so the three missing results must sum to 8. The answer [6,1,1] satisfies this.

Constraints:

  • 1 <= rolls.length <= 100000
  • 1 <= n <= 100000
  • 1 <= rolls[i] <= 6
  • 1 <= mean <= 6


Topics

Company Tags

Please upgrade to NeetCode Pro to view company tags.