Skip to main content

Integer Break

MediumGreedy

Description

Given an integer n, break it into at least two positive integers and maximize the product of those integers. Return the result as an integer.

Examples

Input:n = 10
Output:36
Explanation:

10 = 3 + 3 + 4, product = 36

Input:n = 4
Output:4
Explanation:

4 = 2 + 2, product = 2 × 2 = 4. One could also break it as 4 = 1 + 3 with product = 3, or 4 = 1 + 1 + 2 with product = 2, but 2 + 2 gives the maximum product of 4.

Input:n = 7
Output:12
Explanation:

7 = 3 + 4, product = 3 × 4 = 12. Alternative breakdowns like 7 = 2 + 2 + 3 give product = 12, and 7 = 3 + 2 + 2 also gives product = 12. Other options like 7 = 1 + 6 (product = 6) or 7 = 2 + 5 (product = 10) yield smaller products.

Constraints

  • 2 ≤ n ≤ 58

Ready to solve this problem?

Practice solo and sharpen your skills for technical interviews.