Skip to content

Instantly share code, notes, and snippets.

@bruteforceboy
Created June 17, 2022 15:44
Show Gist options
  • Save bruteforceboy/4c2741dc78d77dafc2b336dac6b7c846 to your computer and use it in GitHub Desktop.
Save bruteforceboy/4c2741dc78d77dafc2b336dac6b7c846 to your computer and use it in GitHub Desktop.
Get Missing Dice Rolls Problem
public class Solution {
static int[] solution(int[] A, int F, int M) {
int aLength = A.length;
int aSum = 0;
for (int value : A) {
aSum += value; // sum of values in array A
}
int totalSum = M * (F + aLength); // the mean multiplied
// by the total number of values in A and F give you the total sum
int fSum = totalSum - aSum; // now we can deduce the sum of values in F
if (fSum < F || fSum > 6 * F) {
// it's not possible to find an array F satisfying the condition
// if any of those conditions hold
// try to convince yourself
int emptyResult[] = {0};
return emptyResult;
}
int result[] = new int[F];
for (int i = 0; i < F; i++) {
int chosenValue = 1;
for (int value = 1; value <= 6; value++) {
// try to put the current value
// only if you will still be able to place at least
// 1 for other positions
if (fSum - value >= F - i - 1) {
chosenValue = value;
}
}
result[i] = chosenValue;
fSum -= chosenValue;
}
return result;
}
public static void main(String[] args) {
int[] A = {1, 5, 6};
int[] result = solution(A, 4, 3);
for (int value : result) {
System.out.print(value + " ");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment