

- 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
Insert 5 to Make Number Largest in Python
Suppose we have a number n, we have to find the maximum number we can make by inserting 5 anywhere in the number.
So, if the input is like n = 826, then the output will be 8526.
To solve this, we will follow these steps −
- temp := n as a string
- ans := -inf
- for i in range 0 to size of temp, do
- cand := substring of temp from index 0 to i concatenate '5' concatenate substring of temp from index i to end
- if i is same as 0 and temp[0] is same as '-', then
- go for the next iteration
- ans := maximum of ans, and the number cand
- return ans
Let us see the following implementation to get better understanding −
Example
class Solution: def solve(self, n): temp = str(n) ans = float('-inf') for i in range(len(temp) + 1): cand = temp[:i] + '5' + temp[i:] if i == 0 and temp[0] == '-': continue ans = max(ans, int(cand)) return ans ob = Solution() print(ob.solve(826))
Input
826
Output
8526
- Related Questions & Answers
- Largest Number in Python
- Finding the largest 5 digit number within the input number using JavaScript
- Largest Unique Number in Python
- Python Program to Print Largest Even and Largest Odd Number in a List
- Largest Number By Two Times in Python
- Python program to find largest number in a list
- Python program to find the largest number in a list
- Find the largest multiple of 2, 3 and 5 in C++
- Largest Number At Least Twice of Others in Python
- Python program to find the second largest number in a list
- Program to find maximum number by adding 5 at any place in Python
- Insert sequential number in MySQL?
- Python - Largest number possible from list of given numbers
- Largest number with the given set of N digits that is divisible by 2, 3 and 5 in C++
- Program to count number of 5-star reviews required to reach threshold percentage in Python
Advertisements