Logo
IconChallenge
Icon
IconProblem
IconSolutions
IconSubmissions

Maximum Value after Insertion

XPChallenge Points: 20
levelLevel: Medium

You are given a number n in the form of a string. This number may also be negative.
You are also given a single digit x (from 1 to 9).
Your task is to insert this digit x into the number n at any position (except before the - sign if the number is negative) such that the resulting number becomes as large as possible.

You must return the resulting number as a string.

Example 1:

Input: n = "-72", x = 5

Output: "-572"

Explanation:

To maximize a negative number, insert where the value becomes least negative.

Example 2:

Input: n = "56", x = 7

Output: "756"

Explanation:

Inserting 7 at the beginning creates the highest value.

to Continue
like
dislike

Accepted:

Submission:

IconReport an issue
Icon
IconCode
IconYou need toto run or submitYou need toto run or submit
IconTest Case
IconTest Result