Insert Five
EasyGreedy · Microsoft
Given an integer value, insert exactly one digit 5 somewhere in its decimal representation (before the first digit, between two digits, or after the last digit) and return the largest integer you can obtain.
For a negative number the minus sign stays at the front and the 5 goes among the digits after it. For example, from -12 you can make -512, -152 or -125.
Example 1
- Input:
- value = 7430
- Output:
- 75430
- Explanation
- 7 is larger than 5, so it stays in front. 4 is the first digit smaller than 5, so the 5 goes just before it.
Example 2
- Input:
- value = -264
- Output:
- -2564
- Explanation
- For a negative number we want the digits to be as small as possible. 2 is smaller than 5 so it stays first, and 6 is the first digit larger than 5, so the 5 goes before it.
Example 3
- Input:
- value = 0
- Output:
- 50
- Explanation
- Putting the 5 in front gives 50, which beats 5 (the 5 after the 0, read as
05).
Constraints
-10^9 <= value <= 10^9