304 North Cardinal St.
Dorchester Center, MA 02124

# Integer Break LeetCode Solution

## Problem – Integer Break LeetCode Solution

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.

Example 1:

``````Input: n = 2
Output: 1
Explanation: 2 = 1 + 1, 1 × 1 = 1.
``````

Example 2:

``````Input: n = 10
Output: 36
Explanation: 10 = 3 + 3 + 4, 3 × 3 × 4 = 36.
``````

Constraints:

• `2 <= n <= 58`

## Integer Break LeetCode Solution in Java

``````public int integerBreak(int n) {
int[] dp = new int[n + 1];
dp = 1;
for(int i = 2; i <= n; i ++) {
for(int j = 1; j < i; j ++) {
dp[i] = Math.max(dp[i], (Math.max(j,dp[j])) * (Math.max(i - j, dp[i - j])));
}
}
return dp[n];
}
``````

## Integer Break LeetCode Solution in Python

``````class Solution:
def integerBreak(self, n: int) -> int:
case = [0,0,1,2,4,6,9]
if n < 7:
return case[n]
dp = case +  * (n-6)
for i in range(7, n+1):
dp[i] = 3*dp[i-3]
return dp[-1]
``````

## Integer Break LeetCode Solution in C++

``````class Solution {
public:
int integerBreak(int n) {

if (n <= 2)
return 1;

vector<int> maxArr(n+1, 0);

/** For a number i: write i as a sum of integers, then take the product of those integers.
maxArr[i] = maximum of all the possible products */

maxArr = 0;
maxArr = 1; // 2=1+1 so maxArr = 1*1

for (int i=3; i<=n; i++) {
for (int j=1; j<i; j++) {
/** Try to write i as: i = j + S where S=i-j corresponds to either one number or a sum of two or more numbers

Assuming that j+S corresponds to the optimal solution for maxArr[i], we have two cases:
(1) i is the sum of two numbers, i.e. S=i-j is one number, and so maxArr[i]=j*(i-j)
(2) i is the sum of at least three numbers, i.e. S=i-j is a sum of at least 2 numbers,
and so the product of the numbers in this sum for S is maxArr[i-j]
(=maximum product after breaking up i-j into a sum of at least two integers):
maxArr[i] = j*maxArr[i-j]
*/
maxArr[i] = max(maxArr[i], max(j*(i-j), j*maxArr[i-j]));
}
}
return maxArr[n];
}
};
``````
##### Integer Break LeetCode Solution Review:

In our experience, we suggest you solve this Integer Break LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Integer Break LeetCode Solution

Find on Leetcode

##### Conclusion:

I hope this Integer Break LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions