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: [6,1,1,1]
💡 Note: Total sum needed is 3 × (3+4) = 21. Current sum is 1+5+6 = 12. Missing sum is 21-12 = 9. With 4 dice needing sum 9, we can use [6,1,1,1] since 6+1+1+1 = 9, and final mean is (12+9)/7 = 3.
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 Observations INPUT Known Rolls (m=3) 3 2 4 Missing Rolls (n=2) ? ? Input Values rolls = [3, 2, 4] mean = 4 n = 2 (missing) m = 3 (known) ALGORITHM STEPS 1 Calculate Total Sum total = mean × (n + m) = 4 × (2 + 3) = 20 2 Sum Known Rolls knownSum = 3 + 2 + 4 = 9 3 Find Missing Sum missingSum = total - knownSum = 20 - 9 = 11 4 Distribute Values avg = 11 / 2 = 5.5 remainder = 11 % 2 = 1 Greedy Distribution Base value: 11/2 = 5 Extra 1 for remainder: result[0] = 5 + 1 = 6 result[1] = 5 FINAL RESULT Missing Observations Found 6 5 Output Array [6, 5] Verification All rolls: [3,2,4,6,5] Total sum: 3+2+4+6+5 = 20 Mean: 20 / 5 = 4 OK - Mean matches! Key Insight: Calculate the required sum for missing dice: missingSum = mean × (n+m) - sum(known rolls). Valid solution exists only if: n ≤ missingSum ≤ 6×n (each die shows 1-6). Distribute using: base = missingSum/n, then add 1 to (missingSum % n) elements for remainder. TutorialsPoint - Find Missing Observations | Optimal Solution
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