- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Program to find maximum number by adding 5 at any place in Python

Suppose we have a number n, we have to find the maximum number we can get by inserting 5 anywhere in the number.

So, if the input is like n = 834, then the output will be 8534.

To solve this, we will follow these steps −

if n > 0, then

s := n as string

k := blank string

c := False

for each character i in s, do

if i < 5 and c is False, then

k := k concatenate "5" concatenate i

c := True

otherwise,

k := k concatenate i

return k as integer

otherwise,

k := blank string

s := |n| as string

c := False

for each character i in s, do

if i > 5 and c is same as False, then

k := k concatenate "5" concatenate i

c := True

otherwise,

k := k concatenate i

if c is false, then

k := k concatenate "5"

return (-k)

## Example

Let us see the following implementation to get better understanding

def solve(n): if n > 0: s = str(n) k = "" c = False for i in s: if int(i) < 5 and c == False: k += "5" + i c = True else: k += i return int(k) else: k = "" s = str(abs(n)) c = False for i in s: if int(i) > 5 and c == False: k += "5" + i c = True else: k += i if not c: k += "5" return int("-" + k) n = 834 print(solve(n))

## Input

834

## Output

8534

- Related Questions & Answers
- Program to find maximum difference of any number and its next smaller number in Python
- Program to find maximum sum by performing at most k negate operations in Python
- Program to find maximum sum obtained of any permutation in Python
- Program to find maximum absolute sum of any subarray in Python
- Program to find maximum number enemies will be killed to place a bomb in C++?
- Find Maximum number possible by doing at-most K swaps in C++
- Program to find number of minimum steps required to meet all person at any cell in Python
- Program to find island count by adding blocks into grid one by one in Python
- Program to find maximum number of eaten apples in Python
- Program to find maximum additive score by deleting numbers in Python
- C++ Program to Find Maximum Value of any Algebraic Expression
- Program to find maximum number of non-overlapping substrings in Python
- Maximum sum by adding numbers with same number of set bits in C++
- Program to find maximum sum by flipping each row elements in Python
- Program to find maximum value by inserting operators in between numbers in Python

Advertisements