Integer Break

Given an integer n, split it into the sum of k positive integers with k >= 2, and make the product of those integers as large as possible. Return the largest product.

30:00

Integer Break
medium
Free written solution
Topics
Companies

Given an integer n, split it into the sum of k positive integers with k >= 2, and make the product of those integers as large as possible. Return the largest product.

Example 1:
Input: {"n":2}
Output: 1
Constraints:
  • 2≤n≤582 \leq n \leq 58

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"n":2}

dp[k] will hold the largest product for a rope of length k, cut into at least two pieces. Fill it for k = 2 to 2.

rope of length 2

2

Where should the cuts go so the pieces multiply to the most?

dp[k] = best product for length k

1

·

2

·

Variables
VariableValue
n2
DepthFunction Call
Stack empty
0/3