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
given N=192, N=4, output should be 591
Expert's answer
defmaxNumber(n:int, k:int) ->int:
i = 0while k > 0and i < 3:
n_str = str(n)
tmp = int(n_str[i])
if tmp == 9:
i += 1else:
new_n_str = n_str[:i] + str(tmp+1) + n_str[i+1:]
ifint(new_n_str) > n:
n = int(new_n_str)
k -= 1return n
n = 191
k = 4print(maxNumber(n,k))
Finding a professional expert in "partial differential equations" in the advanced level is difficult.
You can find this expert in "Assignmentexpert.com" with confidence.
Exceptional experts! I appreciate your help. God bless you!