cs: add «2028. Find Missing Observations»
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
92e7ac3d19
commit
add853b5e4
1 changed files with 22 additions and 0 deletions
22
cs/find-missing-observations.cs
Normal file
22
cs/find-missing-observations.cs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue