Data Structures & AlgorithmsVery hardCodeforces Div2 D · GATE CS
The numbers [31, 26, 33, 21, 40] are split into two groups (every number goes to exactly one group). What is the smallest possible absolute difference between the two group sums?
- A. 19
- B. 1
- C. 7
- D. 5
Correct answer: D. 5
Find all achievable subset sums s, then minimise |total − 2s|; this is the subset-sum DP. A greedy split (always put the next largest number in the lighter group) is a good heuristic but is not always optimal.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- Each number in [1, 2, 3, 4, 5, 6, 7] gets a sign '+' or '-', and all are then added. How many sign choices give a total of exactly 4?
- How many NON-EMPTY subsets of [4, 7, 2, 9, 5, 3, 8, 6] have a sum divisible by 3? Subsets are chosen by position.
- Items as (weight, value) are [(3, 5), (4, 6), (5, 9), (7, 13), (2, 8)]. Each item can be used at most once. What is the maximum value of a…
- From the numbers [17, 22, 9, 35, 41, 13, 28], choose a subset (each number used at most once) whose sum is as large as possible without…
- Four workers (rows) and four tasks (columns) have cost matrix [[9, 2, 7, 8], [6, 4, 3, 7], [5, 8, 1, 8], [7, 6, 9, 4]], where cost[i][j]…
- Four cities have symmetric travel costs D = [[0, 27, 24, 21], [27, 0, 30, 17], [24, 30, 0, 1], [21, 17, 1, 0]]. Starting at city 0, you…