Minimize Result by Adding Parentheses to Expression - Problem

Given a mathematical expression in the form "<num1>+<num2>" where both numbers are positive integers, your goal is to minimize the result by strategically adding exactly one pair of parentheses.

The challenge lies in understanding that parentheses can split the numbers, not just group the entire addition. For example, with "247+38", you could place parentheses to create "2(47+3)8" which evaluates to 2 × 50 × 8 = 800.

Rules:

  • The left parenthesis must be placed to the left of the '+' sign
  • The right parenthesis must be placed to the right of the '+' sign
  • The resulting expression must be mathematically valid
  • Return any valid solution that produces the minimum result

Key Insight: Think of this as splitting num1 into left1 + right1 and num2 into left2 + right2, then computing left1 × (right1 + left2) × right2. When left1 or right2 are empty, they default to 1.

Input & Output

example_1.py — Basic Case
$ Input: expression = "247+38"
Output: "(247+38)"
💡 Note: We can place parentheses at positions (0,2) to get "(247+38)" = 1×285×1 = 285. Other placements like "2(47+3)8" = 2×50×8 = 800 give larger results, so the minimum is achieved by wrapping the entire addition.
example_2.py — Split Strategy
$ Input: expression = "12+34"
Output: "1(2+3)4"
💡 Note: Placing parentheses at positions (1,1) gives "1(2+3)4" = 1×5×4 = 20. This is better than "(12+34)" = 46 or "12(+34)" which would be invalid. The key is finding the right split points.
example_3.py — Edge Case
$ Input: expression = "1+1"
Output: "(1+1)"
💡 Note: With single digits, we have limited options. "(1+1)" = 2 is optimal since any other placement like trying to split single digits would still result in the same or worse outcome.

Constraints

  • 3 ≤ expression.length ≤ 10
  • expression consists of digits from '1' to '9' and '+'
  • expression starts and ends with a digit
  • expression contains exactly one '+' sign
  • The original expression and result fit in a 32-bit signed integer

Visualization

Tap to expand
Minimize Result by Adding ParenthesesInput: "247+38"Original: 247 + 38 = 285Goal: Add ( ) to minimize resultStrategy: Try all possible positionsBad placement: "2(47+3)8"= 2 × (47+3) × 8= 2 × 50 × 8= 800 ❌Good placement: "(247+38)"= 1 × (247+38) × 1= 1 × 285 × 1= 285 ✓Algorithm Steps:1Find '+' position and split: "247" + "38"2Try all left positions (0,1,2,3) × all right positions (0,1,2)3For each combo: calculate left_part × (sum_middle) × right_part4Return expression with minimum resultKey Insight:💡 Empty parts outside parentheses default to 1 (multiplicative identity)This makes edge positions (start/end) often optimal for minimization!
Understanding the Visualization
1
Split at Plus
Identify the '+' sign and split the expression into left number and right number
2
Enumerate Positions
Try every possible position for left parenthesis (in num1) and right parenthesis (in num2)
3
Calculate Formula
For each position pair, compute: left_part × (middle_sum) × right_part
4
Find Minimum
Track the position combination that produces the smallest result
Key Takeaway
🎯 Key Insight: The problem transforms addition into multiplication by strategic parentheses placement, where empty outer sections become 1, often making full expression wrapping optimal for minimization.
Asked in
Google 25 Amazon 18 Meta 15 Microsoft 12
28.4K Views
Medium Frequency
~15 min Avg. Time
892 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