Answer to Question #288620 in Java | JSP | JSF for Snehal

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


1
Expert's answer
2022-01-19T01:55:04-0500
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));
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog