Skip to content

Instantly share code, notes, and snippets.

@andresgardiol
Created June 30, 2020 02:17
Show Gist options
  • Save andresgardiol/52399150d2e97f6e81cdd71aa71ec699 to your computer and use it in GitHub Desktop.
Save andresgardiol/52399150d2e97f6e81cdd71aa71ec699 to your computer and use it in GitHub Desktop.
Codility
package southworks.codingchallenge.task1;
public class Problem1 {
public static int[] solution(int[] A, int F, int M) {
int totalRolls = A.length + F;
int rollResultSum = 0;
for (int rollResult : A) {
rollResultSum += rollResult;
}
double aux = M - (double) rollResultSum / (double) totalRolls;
int k = (int) (aux * totalRolls);
if (k > F * 6 || k < F) {
return new int[]{0};
}
if (k == F) {
int[] res = new int[F];
for (int i = 0; i < F; i++) {
res[i] = 1;
}
return res;
}
int[] result = new int[F];
int div = k / F;
int mod = k % F;
int timesToSubtract = F - mod;
k -= timesToSubtract * div;
if (k == 0) {
for (int i = 0; i < F; i++) {
result[i] = div;
}
return result;
} else {
int resto = k / mod;
for (int i = 0; i < timesToSubtract; i++) {
result[i] = div;
}
for (int i = timesToSubtract; i < F; i++) {
result[i] = resto;
}
return result;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment