Data Structures & AlgorithmsHardICPC regional · counting DP
Problem: How many integer partitions does 6 have (unordered sums of positive integers equal to 6)? Run the DP.
Problem: How many integer partitions does 6 have (unordered sums of positive integers equal to 6)? Run the DP.
n = 6
p = [1] + [0] * n
for part in range(1, n + 1):
for v in range(part, n + 1):
p[v] += p[v - part]
print(p[n])What is printed?- A. 10
- B. 12
- C. 11
- D. 15
Correct answer: C. 11
The classic partition numbers are p(6) = 11, found by adding parts in a fixed order so each partition counts once.
More Coding Interview Problems questions
- Problem: How many ordered pairs (x, y) with 1 <= x, y <= 10 have x y <= 10? Run the code. python print(sum(1 for x in range(1, 11) for y…
- Problem: How many integers in [1, 1000] contain the digit 0? Run the code. python print(sum(1 for x in range(1, 1001) if '0' in str(x)))…
- Problem: What is the minimum number of perfect squares (1, 4, 9, ...) that sum to 12? Run the DP. python INF = 10 9 dp = [0] + [INF] 12…
- Problem: How many non-decreasing strings of length 3 over the digits {0, 1, 2} exist? Run the code. python from itertools import product…
- Problem: What is the maximum sum of a strictly increasing subsequence of [1, 101, 2, 3, 100, 4, 5]? Run the DP. python a = [1, 101, 2, 3…
- Problem: How many unordered pairs of indices (i < j) of [5, 5, 5, 5] have a sum of 10? Run the code. python a = [5, 5, 5, 5] print(sum(1…