Integer Break - Problem

Given an integer n, break it into the sum of k positive integers, where k >= 2, and maximize the product of those integers.

Return the maximum product you can get.

Input & Output

Example 1 — Basic Case
$ Input: n = 10
Output: 36
💡 Note: Break 10 into 3+3+2+2. Product = 3×3×2×2 = 36. This is optimal because we use mostly 3s with 2s to handle the remainder.
Example 2 — Small Number
$ Input: n = 4
Output: 4
💡 Note: Break 4 into 2+2. Product = 2×2 = 4. Alternative 1+3 gives product 3, and 1+1+2 gives product 2, so 2+2 is optimal.
Example 3 — Edge Case
$ Input: n = 2
Output: 1
💡 Note: Must break into k>=2 parts, so 2 = 1+1. Product = 1×1 = 1. This is the only valid way to break n=2.

Constraints

  • 2 ≤ n ≤ 58

Visualization

Tap to expand
Integer Break - Optimal Solution INPUT n = 10 Goal: Break into k parts (k≥2) Maximize product of parts Possible splits: 10 = 5 + 5 --> 5×5 = 25 10 = 4 + 3 + 3 --> 4×3×3 = 36 10 = 3 + 3 + 4 --> 3×3×4 = 36 10 = 2 + 2 + 3 + 3 --> 36 Input Value n = 10 ALGORITHM STEPS 1 Math Pattern Use 3s as much as possible 2 Handle Remainder r=0: all 3s, r=1: use 4 3 Calculate 10 = 3×3 + 4 (r=1) 4 Compute Product 3^2 × 4 = 9 × 4 = 36 Optimal Strategy n = 10 quotient = 10/3 = 3 remainder = 10%3 = 1 Since r=1: use (3-1)×3s + 4 Result: 3×3×4 = 36 FINAL RESULT Optimal Split: 3 + 3 + 4 3 3 4 × × = 36 Maximum Product Output 36 OK - Verified Key Insight: The number 3 is mathematically optimal for maximizing products. For any n greater than 4, 3×(n-3) is greater than n. When remainder is 1, use 2+2 instead of 3+1 (since 4 is greater than 3). Time: O(1), Space: O(1) - Pure mathematical solution without DP! TutorialsPoint - Integer Break | Optimal Mathematical Solution
Asked in
Google 15 Amazon 12 Facebook 8
182.0K Views
Medium Frequency
~25 min Avg. Time
3.2K 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