Data Structures & AlgorithmsHardCodeforces Div2 D · subset sums
Problem: Two piles of stones (values [1, 6, 11, 5]) are to be divided into two groups whose sums differ as little as possible. What is the minimum possible difference? Run the subset-sum code.
Problem: Two piles of stones (values [1, 6, 11, 5]) are to be divided into two groups whose sums differ as little as possible. What is the minimum possible difference? Run the subset-sum code.
a = [1, 6, 11, 5]
S = sum(a)
reach = {0}
for x in a:
reach |= {s + x for s in reach}
print(min(abs(S - 2 * s) for s in reach))What is printed?- A. 0
- B. 2
- C. 1
- D. 5
Correct answer: C. 1
The total is 23. The split 6 + 5 = 11 against 1 + 11 = 12 gives the smallest difference of 1, and 0 is impossible because 23 is odd.
More Coding Interview Problems questions
- Problem: You have 2 eggs and a building with 10 floors. What is the minimum number of drops needed in the worst case to find the highest…
- Problem: How many distinct ways can the string 'leetcode' be segmented into words from the dictionary {'leet', 'code', 'leetcode', 'lee'…
- Problem: Count the connected components of the undirected graph on 6 vertices with edges (0,1), (1,2), (3,4), (5,5) using DFS. Run the…
- Problem: Check whether the directed graph with edges 0->1, 1->2, 2->0, 2->3 contains a cycle using Kahn's algorithm (count the nodes that…
- Problem: Find the minimum cost to reach node 3 from node 0 using at most 2 edges in the directed graph with edges (0,1,5), (1,3,5)…
- Problem: Count the nodes at depth exactly 2 in the rooted tree with children 0: [1, 2], 1: [3], 2: [4, 5], 3: [], 4: [], 5: []. Root 0 has…