Data Structures & AlgorithmsHardAmazon · Microsoft OA

Problem: Break n into at least two positive integers to maximise their product.

Problem: Break n into at least two positive integers to maximise their product.
def brk(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(brk(10), brk(11))
What is printed?
  1. A. 32 48
  2. B. 36 48
  3. C. 36 54
  4. D. 30 54

Correct answer: C. 36 54

Use as many 3s as possible, with a 2 + 2 instead of 3 + 1: 10 = 3 + 3 + 4 → 36; 11 = 3 + 3 + 3 + 2 → 54. The DP compares keeping j whole versus splitting it further.

More Coding Interview Problems questions

Exams that ask this topic