- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 Articles
- Largest Number in Python
- Largest Unique Number in Python
- What is the largest 5 digit number?
- Python program to find largest number in a list
- Largest Number By Two Times in Python
- Python program to find the largest number in a list
- Python program to find the second largest number in a list
- Finding the largest 5 digit number within the input number using JavaScript
- Largest Number At Least Twice of Others in Python
- Python - Largest number possible from list of given numbers
- Program to find minimum number of operations required to make one number to another in Python
- Find the sum of the largest 5 -digit number and the smallest 6 -digit number.
- Program to find number of rectangles that can form the largest square in Python
- Python Program to Find the Second Largest Number in a List Using Bubble Sort
- How to find the largest number?

Advertisements