Skip to content

Practice Maximum Value after Insertion

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#1881
LeetCode Problem

Maximum Value after Insertion

Problem Statement

You are given a very large integer n, represented as a string,​​​​​​ and an integer digit x. The digits in n and the digit x are in the inclusive range [1, 9], and n may represent a negative number. You want to maximize n's numerical value by inserting x anywhere in the decimal representation of n​​​​​​. You cannot insert x to the left of the negative sign. Return a string representing the maximum value of n​​​​​​ after the insertion.

Source: Maximum Value after Insertion on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
n = "99", x = 9
Output
"999"

# Example 2

Input
n = "-13", x = 2
Output
"-123"

Constraints

1 <= n.length <= 10^5
1 <= x <= 9
The digits in n​​​ are in the range [1, 9].
n is a valid representation of an integer.
In the case of a negative n,​​​​​​ it will begin with '-'.

Related LeetCode Problems

Practice more problems similar to Maximum Value after Insertion.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.