Leetcode 2028: Find Missing Observations

grid47
grid47
Exploring patterns and algorithms
Apr 18, 2024 7 min read

You are given an array of m observations, each representing a die roll from a 6-sided die, and an average value mean of all n + m rolls. Your task is to calculate the missing n observations such that the overall average of all n + m rolls equals the provided mean. If multiple valid answers exist, return any of them. If it is impossible to achieve the desired average, return an empty array.
Problem
Approach
Steps
Complexity
Input: The input consists of an array `rolls` representing `m` observed die rolls, and two integers `mean` and `n`, where `n` is the number of missing rolls.
Example: rolls = [1, 2, 3], mean = 4, n = 3
Constraints:
• 1 <= rolls.length <= 10^5
• 1 <= rolls[i], mean <= 6
Output: Return an array containing the `n` missing observations such that the average of all `n + m` rolls is equal to `mean`. If no such array exists, return an empty array.
Example: Output: [5, 5, 5]
Constraints:
• If multiple solutions exist, any valid array is acceptable.
• If it's impossible to achieve the desired mean, return an empty array.
Goal: The goal is to determine the missing `n` observations whose sum, together with the sum of the observed rolls, results in the exact average of `mean`.
Steps:
• 1. Compute the required sum of all `n + m` rolls by multiplying the mean by `n + m`.
• 2. Subtract the sum of the observed rolls from the required sum to get the sum of the missing rolls.
• 3. Check if the sum of the missing rolls is feasible (i.e., within the possible range of `n` die rolls).
• 4. If feasible, divide the sum by `n` to get the average roll value for the missing rolls and distribute any remainder evenly across the missing rolls.
Goal: Constraints for input values and operations.
Steps:
• 1 <= rolls.length <= 10^5
• 1 <= rolls[i], mean <= 6
Assumptions:
• The value of `mean` is an integer and the sum of all rolls should be divisible by `n + m`.
Input: rolls = [1, 2, 3], mean = 4, n = 3
Explanation: The total sum needed is (1 + 2 + 3 + 5 + 5 + 5) = 24, and the average is 24 / 6 = 4.

Input: rolls = [1, 5, 6], mean = 3, n = 4
Explanation: The total sum needed is (1 + 5 + 6 + 2 + 3 + 2 + 2) = 21, and the average is 21 / 7 = 3.

Input: rolls = [1, 2, 3, 4], mean = 6, n = 4
Explanation: It is impossible to achieve an average of 6 because the sum would be too large for the missing rolls to fit the required range.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus