[LeetCode] Find Missing Observations

2028. 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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
public:
vector<int> missingRolls(vector<int>& A, int avg, int n) {
long long sum = accumulate(begin(A), end(A), 0ll);
long long req = 1ll * avg * (n + A.size()) - sum;
if(req > 6ll * n or req < n) return {};
vector<int> res(n, 1);
req -= n;
for(int i = 0; i < n and req; i++) {
int padding = min(req, 5ll);
res[i] += padding;
req -= padding;
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/06/22/PS/LeetCode/find-missing-observations/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.