Data Structures & AlgorithmsHardGATE CS · CAT · MBA placement
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 selection whose total weight is EXACTLY 8?
- A. 17
- B. 21
- C. 14
- D. 13
Correct answer: C. 14
Initialise dp[0] = 0 and every other dp[c] = −∞, so only exact sums are reachable. Using dp[c] = 0 for all c answers 'weight at most 12', which may leave capacity unused.
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
- 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…
- A directed graph on vertices 0 to 3 has adjacency matrix [[0, 1, 1, 0], [0, 0, 1, 1], [0, 0, 0, 1], [1, 0, 0, 0]] (adj[i][j] = 1 means an…
- For the list [3, 5, 6, 9, 10], how many subsets (including the empty subset) have a bitwise XOR of exactly 0?
- How many subsets of the set {1, 2, ..., 10} (the empty set included) contain no two consecutive integers?