2028. Find Missing Observations LeetCode Solution

In this guide, you will get 2028. Find Missing Observations LeetCode Solution with the best time and space complexity. The solution to Find Missing Observations problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Find Missing Observations solution in C++
  4. Find Missing Observations solution in Java
  5. Find Missing Observations solution in Python
  6. Additional Resources
2028. Find Missing Observations LeetCode Solution image

Problem Statement of Find Missing Observations

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 ith 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 that mean is an integer, so the sum of the n + m rolls should be divisible by n + m.

Example 1:

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.

See also  1267. Count Servers that Communicate LeetCode Solution

Example 2:

Input: rolls = [1,5,6], mean = 3, n = 4
Output: [2,3,2,2]
Explanation: The mean of all n + m rolls is (1 + 5 + 6 + 2 + 3 + 2 + 2) / 7 = 3.

Example 3:

Input: rolls = [1,2,3,4], mean = 6, n = 4
Output: []
Explanation: It is impossible for the mean to be 6 no matter what the 4 missing rolls are.

Constraints:

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

Complexity Analysis

  • Time Complexity: O(n + m)
  • Space Complexity: O(n)

2028. Find Missing Observations LeetCode Solution in C++

class Solution {
 public:
  vector<int> missingRolls(vector<int>& rolls, int mean, int n) {
    const int targetSum = (rolls.size() + n) * mean;
    int missingSum = targetSum - accumulate(rolls.begin(), rolls.end(), 0);
    if (missingSum > n * 6 || missingSum < n)
      return {};

    vector<int> ans(n, missingSum / n);
    missingSum %= n;
    for (int i = 0; i < missingSum; ++i)
      ++ans[i];

    return ans;
  }
};
/* code provided by PROGIEZ */

2028. Find Missing Observations LeetCode Solution in Java

class Solution {
  public int[] missingRolls(int[] rolls, int mean, int n) {
    final int targetSum = (rolls.length + n) * mean;
    int missingSum = targetSum - Arrays.stream(rolls).sum();
    if (missingSum > n * 6 || missingSum < n)
      return new int[] {};

    int[] ans = new int[n];
    Arrays.fill(ans, missingSum / n);
    missingSum %= n;
    for (int i = 0; i < missingSum; ++i)
      ++ans[i];

    return ans;
  }
}
// code provided by PROGIEZ

2028. Find Missing Observations LeetCode Solution in Python

class Solution:
  def missingRolls(self, rolls: list[int], mean: int, n: int) -> list[int]:
    targetSum = (len(rolls) + n) * mean
    missingSum = targetSum - sum(rolls)
    if missingSum > n * 6 or missingSum < n:
      return []

    ans = [missingSum // n] * n
    for i in range(missingSum % n):
      ans[i] += 1

    return ans
# code by PROGIEZ

Additional Resources

See also  96. Unique Binary Search Trees LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.