Data Structures & AlgorithmsVery hardGATE CS
In how many ways can 30 be written as a sum of distinct positive integers, each at most 10, if the order of the summands does not matter?
- A. 43
- B. 35
- C. 3590
- D. 39
Correct answer: D. 39
This counts subsets of {1,...,10} with sum 30 (equivalently, by complement in the total 55, sum 25); a subset-sum DP gives 39. Ordinary partitions of 30 into parts at most 10 give 3590, which allow repeated parts.
More Coding Interview Problems questions
- An undirected weighted graph on vertices 0-5 has edges (u, v, w): (0,1,2), (0,2,2), (1,3,3), (2,3,3), (1,4,5), (2,4,5), (3,5,1), (4,5,1)…
- Problem: A delivery app logs the parcel weights of a day in an array. Return the indices of the two parcels whose weights add up exactly…
- Problem: Given an unsorted array of n ≤ 10⁵ integers and a target, report whether two different positions hold values summing to the…
- Problem: A stock tracker gets daily prices and must report the best profit from one buy followed by one later sell (0 if no profit is…
- Problem: Find the largest sum of a non-empty contiguous block of an array (values may be negative). python def best(a): cur = ans = a[0]…
- Problem: Rotate an array to the right by k steps in place, using O(1) extra space. Example: [1, 2, 3, 4, 5], k = 2 → [4, 5, 1, 2, 3]…