You have observations of n + m6-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 lengthncontaining the missing observations such that the average value of then + mrolls is exactlymean. 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.
publicclassSolution {
publicint[]missingRolls(int[] rolls, int mean, int n) {
int m = rolls.length;
// Calculate the total sum required for n + m rolls to have the given// meanint totalSum = mean * (n + m);
// Calculate the current sum of the observed rollsint currSum = Arrays.stream(rolls).sum();
// Calculate the sum needed from the missing n rollsint missingSum = totalSum - currSum;
// Check if it is possible to split missingSum into n parts with each// part between 1 and 6if (missingSum < n || missingSum > 6 * n) {
returnnewint[0]; // Return an empty array if not possible }
// Construct the resultint[] ans =newint[n];
Arrays.fill(ans, 1);
missingSum -= n; // We have initially assigned each part as 1for (int i = 0; i < n; i++) {
if (missingSum > 0) {
int add = Math.min(5, missingSum); // We can add at most 5 more to each part ans[i]+= add;
missingSum -= add;
}
}
return ans;
}
}
publicclassSolution {
publicint[]missingRolls(int[] rolls, int mean, int n) {
int m = rolls.length;
// Calculate the total sum required for n + m rolls to have the given// meanint totalSum = mean * (n + m);
// Calculate the current sum of the observed rollsint currSum = Arrays.stream(rolls).sum();
// Calculate the sum needed from the missing n rollsint missingSum = totalSum - currSum;
// Check if it is possible to split missingSum into n parts with each// part between 1 and 6if (missingSum < n || missingSum > 6 * n) {
returnnewint[0]; // Return an empty array if not possible }
// Construct the resultint[] ans =newint[n];
int initialPart = missingSum / n, remaining = missingSum % n;
Arrays.fill(ans, initialPart);
for (int i = 1; i <= remaining; i++) {
ans[i]++;
}
return ans;
}
}
classSolution:
defmissingRolls(self, rolls: List[int], mean: int, n: int) -> List[int]:
m = len(rolls)
# Calculate the total sum needed for the n + m rolls to have the given mean total_sum = mean * (n + m)
# Calculate the current sum of the observed rolls current_sum = sum(rolls)
# Calculate the sum needed from the missing n rolls missing_sum = total_sum - current_sum
# Check if it is possible to split missing_sum into n parts with each part between 1 and 6if missing_sum < n or missing_sum >6* n:
return []
# Construct the result res = [1] * n
missing_sum -= n # We have initially assigned each part as 1for i in range(n):
if missing_sum >0:
add = min(
5, missing_sum
) # We can add at most 5 more to each part res[i] += add
missing_sum -= add
return res