Skip to main content
Back to problems
#2028
Medium Algorithms

Find missing observations

Array Math Simulation
57.4% acceptance
Feb 25, 2026
1125
108
You have n + m dice rolls with mean value. n observations went missing. You only have m rolls. Return an array of n values such that all n + m rolls have the given mean. Return empty if impossible.

Solution

Rust
Time O(n)
Space O(n)
LeetCode
solution.rs
impl Solution {
  pub fn missing_rolls(rolls: Vec<i32>, mean: i32, n: i32) -> Vec<i32> {
    let m = rolls.len() as i32;
    let total_needed = mean * (m + n) - rolls.iter().sum::<i32>();
    if total_needed < n || total_needed > 6 * n {
      return vec![];
    }
    let base = total_needed / n;
    let extra = total_needed % n;
    let mut result = vec![base; n as usize];
    for i in 0..extra as usize {
      result[i] += 1;
    }
    result
  }
}