-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindMissingObservations.java
More file actions
32 lines (26 loc) · 1.04 KB
/
FindMissingObservations.java
File metadata and controls
32 lines (26 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class Solution {
public int[] missingRolls(int[] rolls, int mean, int n) {
// Calculate the total number of rolls and the total sum required
int m = rolls.length;
int totalSum = (n + m) * mean;
// Calculate the sum of the observed rolls
int observedSum = 0;
for (int roll : rolls) {
observedSum += roll;
}
// Calculate the sum that the missing rolls should add up to
int missingSum = totalSum - observedSum;
// Check if the missing sum is within the feasible range
if (missingSum < n || missingSum > 6 * n) {
return new int[0]; // It's impossible to achieve the mean
}
// Distribute the missing sum across the missing rolls
int[] result = new int[n];
int average = missingSum / n;
int remainder = missingSum % n;
for (int i = 0; i < n; i++) {
result[i] = average + (i < remainder ? 1 : 0);
}
return result;
}
}