Maximum Value after Insertion - Problem
Maximum Value After Insertion

You're given a very large integer n represented as a string and a single digit x. Your goal is to strategically insert the digit x anywhere within the decimal representation of n to maximize the resulting numerical value.

๐ŸŽฏ Key Rules:
โ€ข All digits in n and the digit x are in range [1, 9]
โ€ข The number n can be positive or negative
โ€ข You cannot insert x to the left of the negative sign
โ€ข You must insert x exactly once

Examples:
โ€ข If n = "73" and x = 6 โ†’ Insert between 7 and 3 โ†’ "763"
โ€ข If n = "-55" and x = 2 โ†’ Insert before first 5 โ†’ "-255"

Return the string representing the maximum possible value after insertion.

Input & Output

example_1.py โ€” Positive Number
$ Input: n = "73", x = 6
โ€บ Output: "763"
๐Ÿ’ก Note: We can insert 6 at position 0 (673), position 1 (763), or position 2 (736). The maximum value is 763, achieved by inserting at position 1.
example_2.py โ€” Negative Number
$ Input: n = "-55", x = 2
โ€บ Output: "-255"
๐Ÿ’ก Note: For negative numbers, we want to minimize the absolute value. We can insert 2 at position 1 (-255) or position 2 (-525) or position 3 (-552). The maximum value (least negative) is -255.
example_3.py โ€” Edge Case
$ Input: n = "999", x = 5
โ€บ Output: "9995"
๐Ÿ’ก Note: Since 5 is smaller than all existing digits (9), we append it at the end to get the maximum value 9995.

Constraints

  • 1 โ‰ค n.length โ‰ค 105
  • 1 โ‰ค x โ‰ค 9
  • The digits in n are in the range [1, 9]
  • n is a valid representation of an integer
  • n will not have leading zeros when it represents a positive integer

Visualization

Tap to expand
Strategic Digit Placement VisualizationPositive Number StrategyGoal: Maximize value by placing digit as left as possible73Original: 737636 > 3, so insert here: 763Negative Number StrategyGoal: Minimize absolute value by strategic placement-55Original: -55-2552 < 5, so insert here: -255๐ŸŽฏ Key InsightGreedy approach works because each position's impact decreases from left to right
Understanding the Visualization
1
Assess the Situation
Determine if we're working with positive or negative numbers (different strategies needed)
2
Apply Greedy Strategy
For positive: place digit where it's bigger than current. For negative: place where it's smaller
3
Scan Left to Right
Check each position until we find the optimal placement
4
Insert and Finish
Place the digit at the identified position or append at end if no suitable position found
Key Takeaway
๐ŸŽฏ Key Insight: The greedy strategy works because the impact of each digit position decreases exponentially from left to right, making local optimal choices globally optimal.
Asked in
Amazon 45 Google 38 Microsoft 32 Meta 28
67.2K Views
Medium-High Frequency
~15 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen