write a function solution that, given a three-digit integer n and an integer K, returns a maximum possible three-digit value that can be obtained by performing at most K increases by 1 of any digit in N in java 8
public class Main {
public static int max(int n, int k) {
char[] digits = Integer.toString(n).toCharArray();
for (int i = 0; i < digits.length; i++) {
int diff = Math.min('9' - digits[i], k);
digits[i] += diff;
k -= diff;
}
return Integer.parseInt(new String(digits));
}
}
Comments
Leave a comment