Data Structures & AlgorithmsModerateInfosys SP/DSE · Product Company SDE

Problem: Split an integer n ≥ 2 into at least two positive parts to maximise the product of the parts.

Problem: Split an integer n ≥ 2 into at least two positive parts to maximise the product of the parts.
def int_break(n):
    dp = [0, 1] + [0] * (n - 1)
    for i in range(2, n + 1):
        dp[i] = max(max(j, dp[j]) * (i - j) for j in range(1, i))
    return dp[n]
print(int_break(2), int_break(8), int_break(11))
What is printed?
  1. A. 1 16 54
  2. B. 1 18 81
  3. C. 2 18 54
  4. D. 1 18 54

Correct answer: D. 1 18 54

For each first cut j, the left part is either kept whole (j) or split further (dp[j]). 2 must split into 1 + 1 → 1. 8 = 3 + 3 + 2 → 18; 11 = 3 + 3 + 3 + 2 → 54. The math shortcut 'use as many 3s as possible, never a 1' agrees. O(n²) DP.

More Coding Interview Problems questions

Exams that ask this topic