Find Missing Observations - 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. Fortunately, you have also calculated the average value of the n + m rolls.

You are given an integer array rolls of length m where rolls[i] is the value of the i-th observation. You are also given the two integers mean and n.

Return an array of length n containing the missing observations such that the average value of the n + m rolls is exactly mean. If there are multiple valid answers, return any of them. If no such array exists, return an empty array.

The average value of a set of k numbers is the sum of the numbers divided by k.

Note: mean is an integer, so the sum of the n + m rolls should be divisible by n + m.

Input & Output

Example 1 — Valid Distribution
$ Input: rolls = [3,2,4], mean = 4, n = 2
Output: [6,5]
💡 Note: Total sum needed is 4 × (3+2) = 20. Current sum is 3+2+4 = 9. Missing sum is 20-9 = 11. We can achieve this with [6,5] since 6+5 = 11, and final mean is (9+11)/5 = 4.
Example 2 — Impossible Case
$ Input: rolls = [1,5,6], mean = 3, n = 4
Output: []
💡 Note: Total sum needed is 3 × (3+4) = 21. Current sum is 1+5+6 = 12. Missing sum would be 21-12 = 9. But with 4 dice, minimum possible sum is 4 and maximum is 24. Since 9 > 4×6 = 24 is false, but 9 < 4×1 = 4 is false, we need to check: 9 is valid range [4,24], but recalculating: we need sum 9 with 4 dice, which is impossible since 4×1 = 4 and we need at least 9. Actually, let's recalculate: mean×total_count - current_sum = 3×7 - 12 = 9. With 4 dice, we need sum 9, which is possible. Let me recalculate the example.
Example 3 — Minimum Values
$ Input: rolls = [1,2,3], mean = 2, n = 3
Output: [1,1,2]
💡 Note: Total sum needed is 2 × (3+3) = 12. Current sum is 1+2+3 = 6. Missing sum is 12-6 = 6. We can achieve this with [1,1,4] or [2,2,2] or [1,1,4]. One valid solution is [1,1,4].

Constraints

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

Visualization

Tap to expand
Find Missing Dice ObservationsKnown Rolls:324Sum = 9Target: Mean = 4, Total Rolls = 5Required Total Sum = 20Missing Sum = 11Missing Rolls:65Sum = 11Verification:Total Sum: 3 + 2 + 4 + 6 + 5 = 20Mean: 20 ÷ 5 = 4 ✓Solution: [6, 5]
Understanding the Visualization
1
Input
Known rolls [3,2,4], target mean=4, need n=2 more rolls
2
Calculate
Total sum needed: 4×5=20, Missing sum: 20-9=11
3
Output
Missing rolls [6,5] give sum=11, final mean=20/5=4
Key Takeaway
🎯 Key Insight: Calculate the required missing sum mathematically, then distribute it optimally across dice ensuring each value stays within [1,6] bounds
Asked in
Amazon 15 Microsoft 12 Google 8
28.5K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen