Answer to Question #330741 in Python for Swathi

Question #330741

Write a function solution that given a three digit integer N and Integer K, returns the maximum possible three digit value that can be obtained by performing at most K increases by 1 of any digit in N

1
Expert's answer
2022-04-19T08:07:06-0400
def max_int(n, k):
    d1 = n%10
    d2 = (n//10) % 10
    d3 = (n//100) % 10
    if d3 > 9:
        return
    k3 = min(k, 9-d3)
    d3 += k3
    k -= k3
    k2 = min(k, 9-d2)
    d2 += k2
    k -= k2
    k1 = min(k, 9-d1)
    d1 += k1
    
    return d3*100 + d2*10 + d3


def main():
    n = int(input())
    k = int(input())
    print(max_int(n, k))


if __name__ == "__main__":
    main()

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
APPROVED BY CLIENTS