Data Structures & AlgorithmsVery hardProduct Company SDE · DP round

Problem: What is the maximum product of positive integer parts when 10 is split into at least two parts? Run the DP.

Problem: What is the maximum product of positive integer parts when 10 is split into at least two parts? Run the DP.
n = 10
dp = [0] * (n + 1)
dp[1] = 1
for m in range(2, n + 1):
    best = m - 1
    for k in range(1, m):
        best = max(best, k * dp[m - k], k * (m - k))
    dp[m] = best
print(dp[n])
What is printed?
  1. A. 32
  2. B. 36
  3. C. 30
  4. D. 27

Correct answer: B. 36

Splitting into 3 + 3 + 4 gives 36, and 2 + 2 + 3 + 3 also gives 36; parts of size 1 never help, so 36 is the maximum.

More Coding Interview Problems questions

Exams that ask this topic