Question #288620

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


Expert's answer

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));
    }
}
LATEST TUTORIALS
APPROVED BY CLIENTS