Data Structures & AlgorithmsVery hardProduct Company SDE · CP practice
Problem: How many ways are there to write 12 as a sum of distinct positive integers (partitions into distinct parts)? Run the DP.
Problem: How many ways are there to write 12 as a sum of distinct positive integers (partitions into distinct parts)? Run the DP.
dp = [1] + [0] * 12
for part in range(1, 13):
for v in range(12, part - 1, -1):
dp[v] += dp[v - part]
print(dp[12])What is printed?- A. 77
- B. 14
- C. 12
- D. 15
Correct answer: D. 15
The downward inner loop uses each part at most once, giving 15 distinct-part partitions of 12. Looping upward allows repeats (p(12) = 77), stopping parts at 11 misses {12} for 14, and dp[11] = 12 is one index short.
More Coding Interview Problems questions
- Problem: What is the number of trailing zeros of 25 40 125? Run the code. python n = 25 40 125 c = 0 while n % 10 == 0: n //= 10 c += 1…
- Problem: How many integer pairs (x, y) with 1 <= x <= 10 and 1 <= y <= 10 satisfy x y being a perfect cube? Run the code. python…
- Problem: What is the longest run of consecutive integers that all have at least one divisor in common with 30 (gcd greater than 1)…
- Problem: How many integers in [1, 500] are divisible by 4 or by 6 but not by 12? Run the code. python print(sum(1 for x in range(1, 501)…
- Problem: What is the sum of the first 12 terms of the sequence an = n^2 + 1 (n from 1 to 12)? Run the code. python print(sum(n n + 1 for n…
- Problem: How many 4-letter words over {x, y, z} contain at least one letter from each of the three symbols? Run the code. python from…