Skip to content

Instantly share code, notes, and snippets.

@bowmore
Created November 7, 2017 22:32
Show Gist options
  • Save bowmore/eb243b97b1f1b80772bcd31961565a2d to your computer and use it in GitHub Desktop.
Save bowmore/eb243b97b1f1b80772bcd31961565a2d to your computer and use it in GitHub Desktop.
public static int maxProductMultipleOfNine(int[] numbers) {
if (numbers.length < 2) {
return 0;
}
if (numbers.length == 2 && (numbers[0] * numbers[1] % 9 == 0)) {
return numbers[0] * numbers[1];
}
int max9 = 0, max = 0, max3 = 0, max3_1 = 0;
for (int i = 0; i < numbers.length; i++) {
int candidate = numbers[i];
if (candidate % 3 == 0) {
if (candidate > max3) {
if (max3 > max3_1) {
max3_1 = max3;
}
max3 = candidate;
} else if (candidate > max3_1) {
max3_1 = candidate;
}
}
if (candidate % 9 == 0) {
if (candidate > max9) {
max9 = candidate;
}
} else if (candidate > max) {
max = candidate;
}
}
return Math.max(max9 * max, max3 * max3_1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment