Q2- Write a function solution that, given an integer N, returns the maximum possible value obtained by inserting one '5' digit inside the decimal representation of integer N.
Examples:
1. Given N = 268, the function should return 5268.
2. Given N = 670, the function should return 6750.
3. Given N = 0, the function should return 50.
4. Given N = −999, the function should return −5999.
Assume that:
N is an integer within the range [−8,000..8,000].
According to the problem statement,
With all due respect, you haven't mentioned any particular programming language to write a function for this problem statement.
So, I have coded the function using the PYTHON programming language.
Code Snippet:
Function Code in Text Format:
def MaximumPossibleValue(self, N):
if N >= 0:
value = str(N)
for i in range(len(value)):
if '5' > value[i]:
return int(value[:i] + '5' + value[i:])
else:
value = str(N)
for i in range(1, len(value)):
if '5' < value[i]:
return int(value[:i] + '5' + value[i:])
return int(value +
'5')
Explanation
for example if you take 670,
maximum possible value is 6750
it will start looping from 0th index
5 is not greater than 6 so it will move to the next index which 1
5 is not greater than 7 so it will move to the next index which 2
5 is greater than 0 so it will print 67 and append 5 to index number 2 and then it will append the remaining string 0
So the final output is 6750
I hope the function snippet and output explanation will help you out!
Thank you!
Get Answers For Free
Most questions answered within 1 hours.