cs: add «2028. Find Missing Observations»

Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2024-09-05 10:52:04 +02:00
parent 92e7ac3d19
commit add853b5e4
Signed by: mfocko
SSH key fingerprint: SHA256:icm0fIOSJUpy5+1x23sfr+hLtF9UhY8VpMC7H4WFJP8

View file

@ -0,0 +1,22 @@
public class Solution {
public int[] MissingRolls(int[] rolls, int mean, int n) {
var sum = rolls.Sum();
var remainder = (rolls.Length + n) * mean - sum;
if (remainder < n || remainder > 6 * n) {
// cannot construct such rolls
return [];
}
var (roll, unmatched) = (remainder / n, remainder % n);
var missing = new int[n];
Array.Fill(missing, roll);
for (int i = 0; i < unmatched; ++i) {
++missing[i];
}
return missing;
}
}